mc.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. var originalLocationPath = window.location.pathname;
  18. var params = window.location.search;
  19. // TODO make it so if param 'xy' is passed in, it doesn't go to parent
  20. var noMcStuffOnLoad = originalLocationPath.indexOf('notes') >= 0;
  21. if(!window.noMc){
  22. if (window.location.pathname === window.top.location.pathname) {
  23. window.top.location.href = '/mc' + window.location.pathname + window.location.search;
  24. return;
  25. }
  26. }
  27. if(window.noMc) return;
  28. // window.top.ensureRHS();
  29. $(document).on('click', '.stag_rhs_toggle', function () {
  30. var state = window.top.toggleRHS(),
  31. icon = $(this).find('i');
  32. if (state === 'collapsed') {
  33. icon.removeClass().addClass('fa fa-arrow-left');
  34. } else {
  35. icon.removeClass().addClass('fa fa-arrow-right');
  36. }
  37. });
  38. var body = $(window.top.document.body),
  39. icon = $('.stag_rhs_toggle i');
  40. if (body.is('.stag_rhs_collapsed')) {
  41. icon.removeClass().addClass('fa fa-arrow-left');
  42. }
  43. initCreateNote();
  44. initQuillEdit();
  45. initFastLoad();
  46. initPrimaryForm();
  47. initPatientPresenceIndicator();
  48. runMCInitializers();
  49. // if(typeof initializeCalendar !== 'undefined') {
  50. // initializeCalendar();
  51. // }
  52. // if(typeof initIntakeEvents !== 'undefined') {
  53. // initIntakeEvents();
  54. // }
  55. // populate history on fresh load
  56. var target = window.top.location.pathname + window.top.location.search;
  57. if (target.indexOf('/mc') === 0) {
  58. target = target.split('/mc')[1];
  59. }
  60. if(noMcStuffOnLoad){
  61. target = originalLocationPath;
  62. }
  63. if(!window.noMc){
  64. fastLoad(target, true, false, true);
  65. }
  66. });
  67. function enableTimeSpecificFields(_checked, _valueClass, _rangeClass) {
  68. if (_valueClass) $('.' + _valueClass).prop('disabled', _checked);
  69. if (_rangeClass) $('.' + _rangeClass).prop('disabled', !_checked);
  70. }
  71. function toggleDisabledAsNeeded(_el, _targetValue, _enableClass, _disableClass) {
  72. if (_el.value === _targetValue) {
  73. if (_enableClass) $('.' + _enableClass).prop('disabled', false);
  74. if (_disableClass) $('.' + _disableClass).prop('disabled', true);
  75. }
  76. else {
  77. if (_enableClass) $('.' + _enableClass).prop('disabled', true);
  78. if (_disableClass) $('.' + _disableClass).prop('disabled', false);
  79. }
  80. }
  81. function toggleVisibilityAsNeeded(_el, _targetValue, _visibleClass, _hiddenClass) {
  82. if (_el.value === _targetValue) {
  83. if (_visibleClass) $('.' + _visibleClass).removeClass('d-none');
  84. if (_hiddenClass) $('.' + _hiddenClass).addClass('d-none');
  85. }
  86. else {
  87. if (_visibleClass) $('.' + _visibleClass).addClass('d-none');
  88. if (_hiddenClass) $('.' + _hiddenClass).removeClass('d-none');
  89. }
  90. }
  91. var fastCache = {};
  92. function initFastLoad(_parent = false) {
  93. var allAs = $('a[href]:not([onclick]):not([href="#"]):not([native])');
  94. if (_parent) {
  95. allAs = _parent.find('a[href]:not([onclick]):not([href="#"]):not([native])');
  96. }
  97. // clear cache
  98. if (!_parent) {
  99. fastCache = {};
  100. }
  101. else {
  102. allAs.each(function () {
  103. if (typeof fastCache[this.href] !== 'undefined') {
  104. delete fastCache[this.href];
  105. }
  106. });
  107. }
  108. // find links without event handlers
  109. allAs.each(function () {
  110. if (!$(this).closest('[moe]').length) {
  111. if ($(this).closest('.dropdown-menu[aria-labelledby="practice-management"]').length) {
  112. enableFastLoad(this, true);
  113. } else {
  114. // var handlers = findEventHandlers('click', this);
  115. // if (!handlers || !handlers.length) {
  116. enableFastLoad(this);
  117. // }
  118. }
  119. }
  120. });
  121. function enableFastLoad(_a, _menuItem = false) {
  122. $(_a)
  123. .off('click.fast-load')
  124. .on('click.fast-load', function () {
  125. fastLoad(this.href, true, true);
  126. $('.dropdown-menu[aria-labelledby="practice-management"]')
  127. .removeClass('show')
  128. .prev('.dropdown-toggle').attr('aria-expanded', 'false');
  129. return false;
  130. });
  131. // console.info('FastLoad enabled for ' + _a.innerText + ' [' + _a.href + ']');
  132. }
  133. // fast cache
  134. // allAs = $('a[href]:not([onclick]):not([href="#"])');
  135. // allAs.each(function () {
  136. // var a = this;
  137. // $.get(a.href, function (_data) {
  138. // fastCache[a.href] = _data;
  139. // });
  140. // });
  141. }
  142. function onFastLoaded(_data, _href, _history) {
  143. var targetParent = $('.stag-content');
  144. if (!Number.isInteger(_data)) {
  145. _data = '<div>' + _data + '</div>';
  146. var content = $(_data).find('.stag-content');
  147. if (content && content.length) {
  148. targetParent.html(content.html());
  149. hideMask();
  150. hideMoeFormMask();
  151. targetParent.append('<script src="/js/yemi.js?_=7"></script>');
  152. window.setTimeout(function () {
  153. initCreateNote();
  154. initQuillEdit();
  155. initFastLoad(targetParent);
  156. initPrimaryForm();
  157. initPatientPresenceIndicator();
  158. runMCInitializers();
  159. if (window.top.currentMcUrl.split('?')[0] !== window.top.location.href.split('?')[0]) {
  160. $(window).scrollTop(0);
  161. }
  162. window.top.currentMcUrl = window.top.location.href;
  163. /*let activeLeftNavLink = $('#sidebarMenu .nav-item>.nav-link.active').first();
  164. if(activeLeftNavLink.length) {
  165. activeLeftNavLink[0].scrollIntoView({
  166. behavior: "smooth",
  167. block: "nearest",
  168. inline: "nearest"
  169. });
  170. }*/
  171. }, 0);
  172. }
  173. else {
  174. targetParent.html('<p class="text-danger p-3 small">Error on page: <b>' + _href + '</b></p>');
  175. hideMask();
  176. hideMoeFormMask();
  177. }
  178. } else {
  179. // fallback
  180. let msg = 'Unable to open page: ';
  181. switch (_data) {
  182. case 403:
  183. msg = 'You do not have access to this page: ';
  184. break;
  185. case 404:
  186. msg = 'Page not found: ';
  187. break;
  188. case 500:
  189. msg = 'Error on page: ';
  190. break;
  191. case 0: // not logged in - refresh top level window to go to login
  192. window.top.location = '/';
  193. break;
  194. }
  195. console.warn('MC: Target page failed: ' + _href);
  196. targetParent.html('<p class="text-danger p-3 small"><b>' + _data + '</b> - ' + msg + '<b>' + _href + '</b></p>');
  197. hideMask();
  198. hideMoeFormMask();
  199. }
  200. $('html, body').removeClass('no-scroll');
  201. }
  202. function fastLoad(_href, _history = true, _useCache = true, _replaceState = false) {
  203. let domPro = $(window.top.document.body).attr('data-pro-uid'),
  204. lsPro = window.top.localStorage.currentProUid;
  205. if(lsPro && domPro && lsPro !== domPro) {
  206. console.warn('ALIX Looks like you have a session as another pro on another tab. Refreshing window.top...');
  207. window.top.location.href = _href;
  208. return false;
  209. }
  210. showMask();
  211. if (_href === '') _href = '/';
  212. // push state
  213. if (_history && window.parent === window.top) {
  214. var target = _href;
  215. if (target.indexOf('//') !== -1) {
  216. target = target.split('//')[1];
  217. if (target.indexOf('/') !== -1) {
  218. target = target.substr(target.indexOf('/') + 1);
  219. }
  220. }
  221. if (target[0] === '/') target = target.substr(1);
  222. if (_replaceState) {
  223. window.top.history.replaceState(target, null, '/mc/' + target);
  224. }
  225. else {
  226. window.top.history.pushState(target, null, '/mc/' + target);
  227. }
  228. }
  229. // dont show top nav if in iframe
  230. if(window !== window.top && window.parent !== window.top) {
  231. $('body').addClass('in-iframe');
  232. }
  233. if (_useCache && !!fastCache[_href]) {
  234. onFastLoaded(fastCache[_href], _href, _history);
  235. } else {
  236. let cleanedHREF = _href;
  237. if(cleanedHREF.length > 1 && cleanedHREF[cleanedHREF.length - 1] === '/') {
  238. cleanedHREF = cleanedHREF.substr(0, cleanedHREF.length - 1);
  239. }
  240. if(cleanedHREF.length > 2) {
  241. cleanedHREF = cleanedHREF.replace('/?', '?');
  242. }
  243. $.get(cleanedHREF, function (_data) {
  244. onFastLoaded(_data, _href, _history);
  245. }).fail(function (_jqXhr) {
  246. onFastLoaded(_jqXhr.status, _href, _history);
  247. });
  248. }
  249. }
  250. function initPrimaryForm(_form = false) {
  251. var primaryForm = _form ? _form : $('.primary-form:visible');
  252. if (primaryForm.length) {
  253. primaryForm = primaryForm.first();
  254. var rte = primaryForm.find('[contenteditable="true"]').first();
  255. if (rte.length) {
  256. rte.focus().select();
  257. }
  258. else {
  259. if (primaryForm.find('[autofocus]:visible').length) {
  260. primaryForm.find('[autofocus]:visible').first().focus().select();
  261. }
  262. else {
  263. primaryForm.find('input:not([type="hidden"]):visible, textarea:visible, select:visible').first().focus().select();
  264. }
  265. }
  266. }
  267. }
  268. function openInRHS(_url) {
  269. window.top.showRHS();
  270. var icon = $('.stag_rhs_toggle i');
  271. icon.removeClass().addClass('fa fa-arrow-right');
  272. window.top.openInRHS(_url);
  273. return false;
  274. }
  275. function initCreateNote() {
  276. $(document)
  277. .off('click.create-note', '.create-auto-note-trigger')
  278. .on('click.create-note', '.create-auto-note-trigger', function () {
  279. createNewNote($(this).attr('data-patient-uid'), $(this).attr('data-hcp-uid'), $(this).attr('data-effective-date'));
  280. });
  281. if ($('select[name="hasMcpDoneOnboardingVisit"]').length) {
  282. $('select[name="hasMcpDoneOnboardingVisit"]').trigger('change');
  283. }
  284. }
  285. function createNewNote(_patientUid, _hcpUid, _date) {
  286. hideMoeFormMask();
  287. showMask();
  288. $.post('/api/note/createUsingFreeTextHtml', {
  289. clientUid: _patientUid,
  290. hcpProUid: _hcpUid,
  291. effectiveDateEST: _date,
  292. }, function (_data) {
  293. hideMask();
  294. if (!_data.success) {
  295. toastr.error(_data.message);
  296. }
  297. else {
  298. fastLoad('/patients/view/' + _patientUid + '/notes/view/' + _data.data, true, false);
  299. }
  300. }, 'json');
  301. }
  302. function initQuillEdit(_selector = '.note-content[auto-edit]') {
  303. $(document)
  304. .off('click.enable-edit', '.note-content:not([auto-edit]):not(.readonly)')
  305. .on('click.enable-edit', '.note-content:not([auto-edit]):not(.readonly)', function () {
  306. $(this).attr('auto-edit', 1);
  307. initQuillEdit();
  308. initPrimaryForm();
  309. initPatientPresenceIndicator();
  310. });
  311. if (!$(_selector).length) return;
  312. var noteUid = $(_selector).attr('data-note-uid');
  313. var qe = new Quill(_selector, {
  314. theme: 'snow',
  315. modules: stagQuillConfig
  316. });
  317. var toolbar = $(qe.container).prev('.ql-toolbar');
  318. var saveButton = $('<button class="btn btn-sm btn-primary w-auto px-3 py-0 text-sm text-white save-note-content">Save</button>');
  319. toolbar.append(saveButton);
  320. saveButton.on('click', function () {
  321. $.post('/api/note/putFreeTextHtml', {
  322. uid: noteUid,
  323. freeTextHtml: qe.root.innerHTML,
  324. }, function (_data) {
  325. if (!_data.success) {
  326. toastr.error(_data.message);
  327. }
  328. else {
  329. // toastr.success('Note saved');
  330. // saveButton.prop('disabled', true);
  331. fastLoad(window.top.location.pathname.substr(3), false, false);
  332. }
  333. }, 'json');
  334. });
  335. // give a unique id to this editor instance
  336. var editorID = Math.ceil(Math.random() * 99999);
  337. // add button for new shortcut
  338. var newSCButton = $('<button class="btn btn-sm btn-default w-auto px-2 ml-2 border py-0 ' +
  339. 'text-sm add-shortcut" data-editor-id="' + editorID + '">+ Shortcut</button>');
  340. toolbar.append(newSCButton);
  341. // qe.on('text-change', function() {
  342. // saveButton.prop('disabled', false);
  343. // });
  344. $('.ql-editor[contenteditable]')
  345. .attr('data-editor-id', editorID)
  346. .attr('with-shortcuts', 1);
  347. }
  348. var patientPresenceTimer = false;
  349. function initPatientPresenceIndicator() {
  350. return false;
  351. if (patientPresenceTimer !== false) {
  352. window.clearInterval(patientPresenceTimer);
  353. patientPresenceTimer = false;
  354. console.log('Cancelled previous timer!');
  355. }
  356. var elem = $('.patient-presence-indicator[data-patient-uid]');
  357. if (elem.length) {
  358. var patientUid = elem.attr('data-patient-uid');
  359. patientPresenceTimer = window.setInterval(function () {
  360. var elem = $('.patient-presence-indicator[data-patient-uid]');
  361. if (elem.length) {
  362. var patientUid = elem.attr('data-patient-uid');
  363. $.get('/patients/' + patientUid + '/presence', function (_data) {
  364. if (_data.online) {
  365. elem.addClass('online');
  366. }
  367. else {
  368. elem.removeClass('online');
  369. }
  370. }, 'json');
  371. }
  372. }, 15000); // once in 15 seconds
  373. }
  374. }
  375. // not really the place for this!
  376. // find a better place to put this
  377. window.saveVisitForm = function(_trigger, _silent = false, _close = false) {
  378. let form = $(_trigger).closest('form');
  379. if (!_silent && !form[0].checkValidity()) {
  380. form[0].reportValidity();
  381. return false;
  382. }
  383. // add [data-name] values to payload
  384. let dataField = form.find('[name="data"]').first();
  385. let parsed = null;
  386. if(dataField.val()) {
  387. parsed = JSON.parse(dataField.val());
  388. }
  389. form.find('[data-name]').each(function() {
  390. if(!parsed) parsed = {};
  391. let keys = $(this).attr('data-name').split('->');
  392. let currentNode = parsed;
  393. for (let i = 0; i < keys.length; i++) {
  394. if(i !== keys.length - 1) {
  395. if(typeof currentNode[keys[i]] === 'undefined') {
  396. currentNode[keys[i]] = {};
  397. }
  398. currentNode = currentNode[keys[i]];
  399. }
  400. else {
  401. if($(this).is(':checkbox')) {
  402. currentNode[keys[i]] = $(this).prop('checked');
  403. }
  404. else {
  405. currentNode[keys[i]] = $(this).val();
  406. }
  407. }
  408. }
  409. });
  410. if(parsed) {
  411. dataField.val(JSON.stringify(parsed));
  412. }
  413. let closeOnSave = false, noteSection = form.closest('.note-section');
  414. if($(_trigger).closest('[visit-moe]').is('[close-on-save]')) {
  415. closeOnSave = true;
  416. }
  417. // disallow-if-value-same-as
  418. let compareWith = false;
  419. if(form.find('.disallow-if-value-same-as')) {
  420. compareWith = $.trim(form.find('.disallow-if-value-same-as').text());
  421. if(compareWith && parsed) {
  422. if(!parsed.value) {
  423. alert('Value cannot be empty!');
  424. return false;
  425. }
  426. let newValue = $('<div/>').html(parsed.value).text().replace(/[^a-zA-Z0-9]/g, '');
  427. if(newValue === '') {
  428. alert('Value cannot be empty!');
  429. return false;
  430. }
  431. if(newValue === compareWith) {
  432. alert('New value should be different from the previous value!');
  433. return false;
  434. }
  435. }
  436. }
  437. if(!_silent) showMask();
  438. $.post(form.attr('url'), form.serialize(), _data => {
  439. if(!hasResponseError(_data)) {
  440. if(!_silent) {
  441. hideMask();
  442. if(typeof updateAllSegmentsInResponse !== 'undefined' && noteSection.length) {
  443. updateAllSegmentsInResponse(_data);
  444. if (closeOnSave) {
  445. noteSection.removeClass('edit');
  446. let segmentUid = form.find('[name="segmentUid"]').first();
  447. segmentUid = segmentUid.length ? segmentUid.val() : false;
  448. if (segmentUid) {
  449. window.setTimeout(() => {
  450. $('.note-tree-node>a[data-segment-uid="' + segmentUid + '"]').trigger('click');
  451. }, 250);
  452. }
  453. }
  454. }
  455. if($(_trigger).closest('[visit-moe]').closest('.stag-popup').length) {
  456. refreshDynamicStagPopup();
  457. }
  458. }
  459. if(_close) {
  460. closeStagPopup();
  461. }
  462. }
  463. }, 'json');
  464. return false;
  465. };
  466. window.initSegmentMoes = function(_parent) {
  467. $('body')
  468. .off('mousedown.visit-moe-outside-click')
  469. .on('mousedown.visit-moe-outside-click', function (e) {
  470. if ($(e.target).closest('[visit-moe]').length ||
  471. $(e.target).closest('#create-shortcut-form').length ||
  472. $(e.target).is('#create-shortcut-form') ||
  473. $(e.target).is('.stag-shortcuts .sc') ||
  474. $(e.target).closest('.ui-datepicker').length) {
  475. return;
  476. }
  477. $('[visit-moe] [url]:not([show])').hide();
  478. });
  479. _parent.find('[visit-moe] [submit]')
  480. .off('click.visit-moe-submit')
  481. .on('click.visit-moe-submit', function() {
  482. saveVisitForm(this);
  483. return false;
  484. });
  485. _parent.find('[visit-moe]>a[start]')
  486. .off('click.visit-moe-show')
  487. .on('click.visit-moe-show', function () {
  488. $('[visit-moe] [url]:not([show])').hide();
  489. $(this)
  490. .closest('[visit-moe]')
  491. .find('form[url]')
  492. .show();
  493. return false;
  494. });
  495. _parent.find('[visit-moe] [cancel]')
  496. .off('click.visit-moe-cancel')
  497. .on('click.visit-moe-cancel', function() {
  498. $(this).closest('[visit-moe]').find('[url]:not([show])').hide();
  499. if($(this).closest('[visit-moe]').is('[close-on-cancel]')) {
  500. $(this).closest('.note-section').removeClass('edit');
  501. }
  502. return false;
  503. });
  504. };