mc.js 13 KB

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