mc.js 12 KB

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