123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- var findEventHandlers = function (eventType, jqSelector) {
- var results = [];
- var $ = jQuery; // to avoid conflict between others frameworks like Mootools
- var arrayIntersection = function (array1, array2) {
- return $(array1).filter(function (index, element) {
- return $.inArray(element, $(array2)) !== -1;
- });
- };
- var haveCommonElements = function (array1, array2) {
- return arrayIntersection(array1, array2).length !== 0;
- };
- var addEventHandlerInfo = function (element, event, $elementsCovered) {
- var extendedEvent = event;
- if ($elementsCovered !== void 0 && $elementsCovered !== null) {
- $.extend(extendedEvent, {
- targets: $elementsCovered.toArray()
- });
- }
- var eventInfo;
- var eventsInfo = $.grep(results, function (evInfo, index) {
- return element === evInfo.element;
- });
- if (eventsInfo.length === 0) {
- eventInfo = {
- element: element,
- events: [extendedEvent]
- };
- results.push(eventInfo);
- } else {
- eventInfo = eventsInfo[0];
- eventInfo.events.push(extendedEvent);
- }
- };
- var $elementsToWatch = $(jqSelector);
- if (jqSelector === "*") //* does not include document and we might be interested in handlers registered there
- $elementsToWatch = $elementsToWatch.add(document);
- var $allElements = $("*").add(document);
- $.each($allElements, function (elementIndex, element) {
- var allElementEvents = $._data(element, "events");
- if (allElementEvents !== void 0 && allElementEvents[eventType] !== void 0) {
- var eventContainer = allElementEvents[eventType];
- $.each(eventContainer, function (eventIndex, event) {
- var isDelegateEvent = event.selector !== void 0 && event.selector !== null;
- var $elementsCovered;
- if (isDelegateEvent) {
- $elementsCovered = $(event.selector, element); //only look at children of the element, since those are the only ones the handler covers
- } else {
- $elementsCovered = $(element); //just itself
- }
- if (haveCommonElements($elementsCovered, $elementsToWatch)) {
- addEventHandlerInfo(element, event, $elementsCovered);
- }
- });
- }
- });
- return results;
- };
- window.top.addEventListener('popstate', function (event) {
- window.setTimeout(function () {
- if (!event) return;
- var state = event.state;
- if (state === '') state = '/';
- if (state[0] !== '/') state = '/' + state;
- if (!!state) fastLoad(state, false, true);
- }, 0);
- });
- $(document).ready(function () {
- if(window.location.href === window.top.location.href) {
- window.location.href = '/mc' + window.location.pathname;
- return;
- }
- // window.top.ensureRHS();
- $(document).on('click', '.stag_rhs_toggle', function () {
- var state = window.top.toggleRHS(),
- icon = $(this).find('i');
- if (state === 'collapsed') {
- icon.removeClass().addClass('fa fa-arrow-left');
- } else {
- icon.removeClass().addClass('fa fa-arrow-right');
- }
- });
- var body = $(window.top.document.body),
- icon = $('.stag_rhs_toggle i');
- if (body.is('.stag_rhs_collapsed')) {
- icon.removeClass().addClass('fa fa-arrow-left');
- }
- initFastLoad();
- if(typeof initializeCalendar !== 'undefined') {
- initializeCalendar();
- }
- });
- function enableTimeSpecificFields(_checked, _valueClass, _rangeClass) {
- if(_valueClass) $('.' + _valueClass).prop('disabled', _checked);
- if(_rangeClass) $('.' + _rangeClass).prop('disabled', !_checked);
- }
- function toggleDisabledAsNeeded(_el, _targetValue, _enableClass, _disableClass) {
- if(_el.value === _targetValue) {
- if(_enableClass) $('.' + _enableClass).prop('disabled', false);
- if(_disableClass) $('.' + _disableClass).prop('disabled', true);
- }
- else {
- if(_enableClass) $('.' + _enableClass).prop('disabled', true);
- if(_disableClass) $('.' + _disableClass).prop('disabled', false);
- }
- }
- function toggleVisibilityAsNeeded(_el, _targetValue, _visibleClass, _hiddenClass) {
- if(_el.value === _targetValue) {
- if(_visibleClass) $('.' + _visibleClass).removeClass('d-none');
- if(_hiddenClass) $('.' + _hiddenClass).addClass('d-none');
- }
- else {
- if(_visibleClass) $('.' + _visibleClass).addClass('d-none');
- if(_hiddenClass) $('.' + _hiddenClass).removeClass('d-none');
- }
- }
- var fastCache = {};
- function initFastLoad(_parent = false) {
- var allAs = $('a[href]:not([onclick]):not([href="#"])');
- if (_parent) {
- allAs = _parent.find('a[href]:not([onclick]):not([href="#"])');
- }
- // clear cache
- if(!_parent) {
- fastCache = {};
- }
- else {
- allAs.each(function () {
- if(typeof fastCache[this.href] !== 'undefined') {
- delete fastCache[this.href];
- }
- });
- }
- // find links without event handlers
- allAs.each(function () {
- if (!$(this).closest('[moe]').length) {
- if ($(this).closest('.dropdown-menu[aria-labelledby="practice-management"]').length) {
- enableFastLoad(this, true);
- } else {
- var handlers = findEventHandlers('click', this);
- if (!handlers || !handlers.length) {
- enableFastLoad(this);
- }
- }
- }
- });
- function enableFastLoad(_a, _menuItem = false) {
- $(_a)
- .off('click.fast-load')
- .on('click.fast-load', function () {
- console.log('ALIX: got click!');
- showMask();
- console.log('ALIX: showed mask');
- fastLoad(this.href, true, true);
- if (_menuItem) {
- $(this).closest('.dropdown-menu')
- .removeClass('show')
- .prev('.dropdown-toggle').attr('aria-expanded', 'false');
- }
- return false;
- });
- // console.info('FastLoad enabled for ' + _a.innerText + ' [' + _a.href + ']');
- }
- // fast cache
- allAs = $('a[href]:not([onclick]):not([href="#"])');
- allAs.each(function () {
- var a = this;
- $.get(a.href, function (_data) {
- fastCache[a.href] = _data;
- });
- });
- }
- function onFastLoaded(_data, _href, _history) {
- var targetParent = $('.stag-content');
- _data = '<div>' + _data + '</div>';
- var content = $(_data).find('.stag-content');
- if (content && content.length) {
- content = content.html();
- content += '<script src="/js/yemi.js?_=2"></script>';
- targetParent.html(content);
- window.setTimeout(function() {
- initFastLoad(targetParent);
- }, 50);
- if(typeof initializeCalendar !== 'undefined') {
- initializeCalendar();
- }
- // push state
- if (_history) {
- var target = _href;
- if (target.indexOf('//') !== -1) {
- target = target.split('//')[1];
- if (target.indexOf('/') !== -1) {
- target = target.substr(target.indexOf('/') + 1);
- }
- }
- window.top.history.pushState(target, null, '/mc/' + target);
- }
- } else {
- console.warn('Target page not found: ' + _href);
- window.location.href = _href; // fallback
- }
- // hideMask();
- }
- function fastLoad(_href, _history = true, _useCache = true) {
- if (_useCache && !!fastCache[_href]) {
- onFastLoaded(fastCache[_href], _href, _history);
- } else {
- $.get(_href, function(_data) {
- onFastLoaded(_data, _href, _history);
- });
- }
- }
- function openInRHS(_url) {
- window.top.showRHS();
- var icon = $('.stag_rhs_toggle i');
- icon.removeClass().addClass('fa fa-arrow-right');
- window.top.openInRHS(_url);
- return false;
- }
|