mc.js 13 KB

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