mc.js 12 KB

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