mc.js 21 KB

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