mc.js 12 KB

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