mc.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. initPrimaryForm();
  88. initFastLoad();
  89. if(typeof initializeCalendar !== 'undefined') {
  90. initializeCalendar();
  91. }
  92. });
  93. function enableTimeSpecificFields(_checked, _valueClass, _rangeClass) {
  94. if(_valueClass) $('.' + _valueClass).prop('disabled', _checked);
  95. if(_rangeClass) $('.' + _rangeClass).prop('disabled', !_checked);
  96. }
  97. function toggleDisabledAsNeeded(_el, _targetValue, _enableClass, _disableClass) {
  98. if(_el.value === _targetValue) {
  99. if(_enableClass) $('.' + _enableClass).prop('disabled', false);
  100. if(_disableClass) $('.' + _disableClass).prop('disabled', true);
  101. }
  102. else {
  103. if(_enableClass) $('.' + _enableClass).prop('disabled', true);
  104. if(_disableClass) $('.' + _disableClass).prop('disabled', false);
  105. }
  106. }
  107. function toggleVisibilityAsNeeded(_el, _targetValue, _visibleClass, _hiddenClass) {
  108. if(_el.value === _targetValue) {
  109. if(_visibleClass) $('.' + _visibleClass).removeClass('d-none');
  110. if(_hiddenClass) $('.' + _hiddenClass).addClass('d-none');
  111. }
  112. else {
  113. if(_visibleClass) $('.' + _visibleClass).addClass('d-none');
  114. if(_hiddenClass) $('.' + _hiddenClass).removeClass('d-none');
  115. }
  116. }
  117. var fastCache = {};
  118. function initFastLoad(_parent = false) {
  119. var allAs = $('a[href]:not([onclick]):not([href="#"])');
  120. if (_parent) {
  121. allAs = _parent.find('a[href]:not([onclick]):not([href="#"])');
  122. }
  123. // clear cache
  124. if(!_parent) {
  125. fastCache = {};
  126. }
  127. else {
  128. allAs.each(function () {
  129. if(typeof fastCache[this.href] !== 'undefined') {
  130. delete fastCache[this.href];
  131. }
  132. });
  133. }
  134. // find links without event handlers
  135. allAs.each(function () {
  136. if (!$(this).closest('[moe]').length) {
  137. if ($(this).closest('.dropdown-menu[aria-labelledby="practice-management"]').length) {
  138. enableFastLoad(this, true);
  139. } else {
  140. var handlers = findEventHandlers('click', this);
  141. if (!handlers || !handlers.length) {
  142. enableFastLoad(this);
  143. }
  144. }
  145. }
  146. });
  147. function enableFastLoad(_a, _menuItem = false) {
  148. $(_a)
  149. .off('click.fast-load')
  150. .on('click.fast-load', function () {
  151. console.log('ALIX: got click!');
  152. showMask();
  153. console.log('ALIX: showed mask');
  154. fastLoad(this.href, true, true);
  155. $('.dropdown-menu[aria-labelledby="practice-management"]')
  156. .removeClass('show')
  157. .prev('.dropdown-toggle').attr('aria-expanded', 'false');
  158. return false;
  159. });
  160. // console.info('FastLoad enabled for ' + _a.innerText + ' [' + _a.href + ']');
  161. }
  162. // fast cache
  163. allAs = $('a[href]:not([onclick]):not([href="#"])');
  164. allAs.each(function () {
  165. var a = this;
  166. $.get(a.href, function (_data) {
  167. fastCache[a.href] = _data;
  168. });
  169. });
  170. }
  171. function onFastLoaded(_data, _href, _history) {
  172. var targetParent = $('.stag-content');
  173. _data = '<div>' + _data + '</div>';
  174. var content = $(_data).find('.stag-content');
  175. if (content && content.length) {
  176. content = content.html();
  177. content += '<script src="/js/yemi.js?_=3"></script>';
  178. targetParent.html(content);
  179. window.setTimeout(function() {
  180. initPrimaryForm();
  181. initFastLoad(targetParent);
  182. }, 50);
  183. if(typeof initializeCalendar !== 'undefined') {
  184. initializeCalendar();
  185. }
  186. // push state
  187. if (_history) {
  188. var target = _href;
  189. if (target.indexOf('//') !== -1) {
  190. target = target.split('//')[1];
  191. if (target.indexOf('/') !== -1) {
  192. target = target.substr(target.indexOf('/') + 1);
  193. }
  194. }
  195. window.top.history.pushState(target, null, '/mc/' + target);
  196. }
  197. } else {
  198. console.warn('Target page not found: ' + _href);
  199. window.location.href = _href; // fallback
  200. }
  201. // hideMask();
  202. }
  203. function fastLoad(_href, _history = true, _useCache = true) {
  204. if (_useCache && !!fastCache[_href]) {
  205. onFastLoaded(fastCache[_href], _href, _history);
  206. } else {
  207. $.get(_href, function(_data) {
  208. onFastLoaded(_data, _href, _history);
  209. });
  210. }
  211. }
  212. function initPrimaryForm() {
  213. if($('.primary-form:visible').length) {
  214. $('.primary-form:visible').first().find('input, textarea, select').first().focus().select();
  215. }
  216. }
  217. function openInRHS(_url) {
  218. window.top.showRHS();
  219. var icon = $('.stag_rhs_toggle i');
  220. icon.removeClass().addClass('fa fa-arrow-right');
  221. window.top.openInRHS(_url);
  222. return false;
  223. }