mc.js 11 KB

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