mc.js 20 KB

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