mc.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. window.top.addEventListener('popstate', function (event) {
  2. window.setTimeout(function () {
  3. hideMask();
  4. hideMoeFormMask();
  5. if (!event || (!event.state && event.state !== '')) {
  6. console.error('ALIX No state!', event, event.state);
  7. return;
  8. }
  9. var state = event.state;
  10. if (state === '') state = '/';
  11. if (state[0] !== '/') state = '/' + state;
  12. if (!!state) fastLoad(state, false, true);
  13. }, 0);
  14. });
  15. $(document).ready(function () {
  16. if(window.location.pathname === window.top.location.pathname) {
  17. window.top.location.href = '/mc' + window.location.pathname;
  18. return;
  19. }
  20. // window.top.ensureRHS();
  21. $(document).on('click', '.stag_rhs_toggle', function () {
  22. var state = window.top.toggleRHS(),
  23. icon = $(this).find('i');
  24. if (state === 'collapsed') {
  25. icon.removeClass().addClass('fa fa-arrow-left');
  26. } else {
  27. icon.removeClass().addClass('fa fa-arrow-right');
  28. }
  29. });
  30. var body = $(window.top.document.body),
  31. icon = $('.stag_rhs_toggle i');
  32. if (body.is('.stag_rhs_collapsed')) {
  33. icon.removeClass().addClass('fa fa-arrow-left');
  34. }
  35. initCreateNote();
  36. initQuillEdit();
  37. initFastLoad();
  38. initPrimaryForm();
  39. initPatientPresenceIndicator();
  40. runMCInitializers();
  41. // if(typeof initializeCalendar !== 'undefined') {
  42. // initializeCalendar();
  43. // }
  44. // if(typeof initIntakeEvents !== 'undefined') {
  45. // initIntakeEvents();
  46. // }
  47. // populate history on fresh load
  48. var target = window.top.location.pathname;
  49. if (target.indexOf('/mc') === 0) {
  50. target = target.split('/mc')[1];
  51. }
  52. fastLoad(target, true, false, true);
  53. });
  54. function enableTimeSpecificFields(_checked, _valueClass, _rangeClass) {
  55. if(_valueClass) $('.' + _valueClass).prop('disabled', _checked);
  56. if(_rangeClass) $('.' + _rangeClass).prop('disabled', !_checked);
  57. }
  58. function toggleDisabledAsNeeded(_el, _targetValue, _enableClass, _disableClass) {
  59. if(_el.value === _targetValue) {
  60. if(_enableClass) $('.' + _enableClass).prop('disabled', false);
  61. if(_disableClass) $('.' + _disableClass).prop('disabled', true);
  62. }
  63. else {
  64. if(_enableClass) $('.' + _enableClass).prop('disabled', true);
  65. if(_disableClass) $('.' + _disableClass).prop('disabled', false);
  66. }
  67. }
  68. function toggleVisibilityAsNeeded(_el, _targetValue, _visibleClass, _hiddenClass) {
  69. if(_el.value === _targetValue) {
  70. if(_visibleClass) $('.' + _visibleClass).removeClass('d-none');
  71. if(_hiddenClass) $('.' + _hiddenClass).addClass('d-none');
  72. }
  73. else {
  74. if(_visibleClass) $('.' + _visibleClass).addClass('d-none');
  75. if(_hiddenClass) $('.' + _hiddenClass).removeClass('d-none');
  76. }
  77. }
  78. var fastCache = {};
  79. function initFastLoad(_parent = false) {
  80. var allAs = $('a[href]:not([onclick]):not([href="#"]):not([native])');
  81. if (_parent) {
  82. allAs = _parent.find('a[href]:not([onclick]):not([href="#"]):not([native])');
  83. }
  84. // clear cache
  85. if(!_parent) {
  86. fastCache = {};
  87. }
  88. else {
  89. allAs.each(function () {
  90. if(typeof fastCache[this.href] !== 'undefined') {
  91. delete fastCache[this.href];
  92. }
  93. });
  94. }
  95. // find links without event handlers
  96. allAs.each(function () {
  97. if (!$(this).closest('[moe]').length) {
  98. if ($(this).closest('.dropdown-menu[aria-labelledby="practice-management"]').length) {
  99. enableFastLoad(this, true);
  100. } else {
  101. var handlers = findEventHandlers('click', this);
  102. if (!handlers || !handlers.length) {
  103. enableFastLoad(this);
  104. }
  105. }
  106. }
  107. });
  108. function enableFastLoad(_a, _menuItem = false) {
  109. $(_a)
  110. .off('click.fast-load')
  111. .on('click.fast-load', function () {
  112. fastLoad(this.href, true, true);
  113. $('.dropdown-menu[aria-labelledby="practice-management"]')
  114. .removeClass('show')
  115. .prev('.dropdown-toggle').attr('aria-expanded', 'false');
  116. return false;
  117. });
  118. // console.info('FastLoad enabled for ' + _a.innerText + ' [' + _a.href + ']');
  119. }
  120. // fast cache
  121. // allAs = $('a[href]:not([onclick]):not([href="#"])');
  122. // allAs.each(function () {
  123. // var a = this;
  124. // $.get(a.href, function (_data) {
  125. // fastCache[a.href] = _data;
  126. // });
  127. // });
  128. }
  129. function onFastLoaded(_data, _href, _history) {
  130. var targetParent = $('.stag-content');
  131. _data = '<div>' + _data + '</div>';
  132. var content = $(_data).find('.stag-content');
  133. if (content && content.length) {
  134. targetParent.html(content.html());
  135. hideMask();
  136. hideMoeFormMask();
  137. targetParent.append('<script src="/js/yemi.js?_=7"></script>');
  138. window.setTimeout(function() {
  139. initCreateNote();
  140. initQuillEdit();
  141. initFastLoad(targetParent);
  142. initPrimaryForm();
  143. initPatientPresenceIndicator();
  144. runMCInitializers();
  145. $(window).scrollTop(0);
  146. }, 50);
  147. // if(typeof initializeCalendar !== 'undefined') {
  148. // initializeCalendar();
  149. // }
  150. // if(typeof initIntakeEvents !== 'undefined') {
  151. // initIntakeEvents();
  152. // }
  153. } else {
  154. // fallback
  155. console.warn('MC: Target page failed: ' + _href);
  156. targetParent.html('<p class="text-danger p-3 small">Target page not found or returned error: <b>' + _href + '</b></p>');
  157. hideMask();
  158. hideMoeFormMask();
  159. }
  160. $('html, body').removeClass('no-scroll');
  161. }
  162. function fastLoad(_href, _history = true, _useCache = true, _replaceState = false) {
  163. showMask();
  164. if(_href === '') _href = '/';
  165. // push state
  166. if (_history) {
  167. var target = _href;
  168. if (target.indexOf('//') !== -1) {
  169. target = target.split('//')[1];
  170. if (target.indexOf('/') !== -1) {
  171. target = target.substr(target.indexOf('/') + 1);
  172. }
  173. }
  174. if(target[0] === '/') target = target.substr(1);
  175. if(_replaceState) {
  176. window.top.history.replaceState(target, null, '/mc/' + target);
  177. console.log('ALIX replaceState: [' + target + ']');
  178. }
  179. else {
  180. window.top.history.pushState(target, null, '/mc/' + target);
  181. console.log('ALIX pushState: [' + target + ']');
  182. }
  183. }
  184. if (_useCache && !!fastCache[_href]) {
  185. onFastLoaded(fastCache[_href], _href, _history);
  186. } else {
  187. $.get(_href, function(_data) {
  188. onFastLoaded(_data, _href, _history);
  189. }).fail(function() {
  190. onFastLoaded('error', _href, _history);
  191. });
  192. }
  193. }
  194. function initPrimaryForm(_form = false) {
  195. var primaryForm = _form ? _form : $('.primary-form:visible');
  196. if (primaryForm.length) {
  197. primaryForm = primaryForm.first();
  198. var rte = primaryForm.find('[contenteditable="true"]').first();
  199. if(rte.length) {
  200. rte.focus().select();
  201. }
  202. else {
  203. if(primaryForm.find('[autofocus]:visible').length) {
  204. primaryForm.find('[autofocus]:visible').first().focus().select();
  205. }
  206. else {
  207. primaryForm.find('input:not([type="hidden"]):visible, textarea:visible, select:visible').first().focus().select();
  208. }
  209. }
  210. }
  211. }
  212. function openInRHS(_url) {
  213. window.top.showRHS();
  214. var icon = $('.stag_rhs_toggle i');
  215. icon.removeClass().addClass('fa fa-arrow-right');
  216. window.top.openInRHS(_url);
  217. return false;
  218. }
  219. function initCreateNote() {
  220. $(document)
  221. .off('click.create-note', '.create-auto-note-trigger')
  222. .on('click.create-note', '.create-auto-note-trigger', function() {
  223. createNewNote($(this).attr('data-patient-uid'), $(this).attr('data-hcp-uid'), $(this).attr('data-effective-date'));
  224. });
  225. if($('select[name="hasMcpDoneOnboardingVisit"]').length) {
  226. $('select[name="hasMcpDoneOnboardingVisit"]')[0].onchange();
  227. }
  228. }
  229. function createNewNote(_patientUid, _hcpUid, _date) {
  230. hideMoeFormMask();
  231. showMask();
  232. $.post('/api/note/createUsingFreeTextHtml', {
  233. clientUid: _patientUid,
  234. hcpProUid: _hcpUid,
  235. effectiveDateEST: _date,
  236. }, function(_data) {
  237. hideMask();
  238. if (!_data.success) {
  239. toastr.error(_data.message);
  240. }
  241. else {
  242. fastLoad('/patients/view/' + _patientUid + '/notes/view/' + _data.data, true, false);
  243. }
  244. }, 'json');
  245. }
  246. function initQuillEdit(_selector = '.note-content[auto-edit]') {
  247. $(document)
  248. .off('click.enable-edit', '.note-content:not([auto-edit]):not(.readonly)')
  249. .on('click.enable-edit', '.note-content:not([auto-edit]):not(.readonly)', function() {
  250. $(this).attr('auto-edit', 1);
  251. initQuillEdit();
  252. initPrimaryForm();
  253. initPatientPresenceIndicator();
  254. });
  255. if(!$(_selector).length) return;
  256. var noteUid = $(_selector).attr('data-note-uid');
  257. var qe = new Quill(_selector, {
  258. theme: 'snow',
  259. modules: {
  260. keyboard: {
  261. bindings: {
  262. handleEnter: {
  263. key: 13,
  264. handler: function() {
  265. if(!$('.stag-shortcuts:visible').length) return true;
  266. }
  267. }
  268. }
  269. }
  270. }
  271. });
  272. var toolbar = $(qe.container).prev('.ql-toolbar');
  273. var saveButton = $('<button class="btn btn-sm btn-primary w-auto px-3 py-0 text-sm text-white save-note-content">Save</button>');
  274. toolbar.append(saveButton);
  275. saveButton.on('click', function() {
  276. $.post('/api/note/putFreeTextHtml', {
  277. uid: noteUid,
  278. freeTextHtml: qe.root.innerHTML,
  279. }, function(_data) {
  280. if (!_data.success) {
  281. toastr.error(_data.message);
  282. }
  283. else {
  284. // toastr.success('Note saved');
  285. // saveButton.prop('disabled', true);
  286. fastLoad(window.top.location.pathname.substr(3), false, false);
  287. }
  288. }, 'json');
  289. });
  290. // give a unique id to this editor instance
  291. var editorID = Math.ceil(Math.random() * 99999);
  292. // add button for new shortcut
  293. var newSCButton = $('<button class="btn btn-sm btn-default w-auto px-2 ml-2 border py-0 ' +
  294. 'text-sm add-shortcut" data-editor-id="' + editorID + '">+ Shortcut</button>');
  295. toolbar.append(newSCButton);
  296. // qe.on('text-change', function() {
  297. // saveButton.prop('disabled', false);
  298. // });
  299. $('.ql-editor[contenteditable]')
  300. .attr('data-editor-id', editorID)
  301. .attr('with-shortcuts', 1);
  302. }
  303. var patientPresenceTimer = false;
  304. function initPatientPresenceIndicator() {
  305. if(patientPresenceTimer !== false) {
  306. window.clearInterval(patientPresenceTimer);
  307. patientPresenceTimer = false;
  308. console.log('Cancelled previous timer!');
  309. }
  310. var elem = $('.patient-presence-indicator[data-patient-uid]');
  311. if(elem.length) {
  312. var patientUid = elem.attr('data-patient-uid');
  313. patientPresenceTimer = window.setInterval(function() {
  314. var elem = $('.patient-presence-indicator[data-patient-uid]');
  315. if(elem.length) {
  316. var patientUid = elem.attr('data-patient-uid');
  317. $.get('/patients/' + patientUid + '/presence', function(_data) {
  318. if(_data.online) {
  319. elem.addClass('online');
  320. }
  321. else {
  322. elem.removeClass('online');
  323. }
  324. }, 'json');
  325. }
  326. }, 15000); // once in 15 seconds
  327. }
  328. }