mc.js 12 KB

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