123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- 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');
- }
- initCreateNote();
- initQuillEdit();
- initFastLoad();
- initPrimaryForm();
- if(typeof initializeCalendar !== 'undefined') {
- initializeCalendar();
- }
- if(typeof initIntakeEvents !== 'undefined') {
- initIntakeEvents();
- }
- // populate history on fresh load
- var target = window.top.location.pathname;
- if (target.indexOf('/mc') === 0) {
- target = target.split('/mc')[1];
- }
- // window.top.history.pushState(target, null, '/mc' + target);
- fastLoad(target, true, false);
- });
- 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 () {
- fastLoad(this.href, true, true);
- $('.dropdown-menu[aria-labelledby="practice-management"]')
- .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?_=4"></script>';
- targetParent.html(content);
- window.setTimeout(function() {
- initCreateNote();
- initQuillEdit();
- initFastLoad(targetParent);
- initPrimaryForm();
- }, 50);
- if(typeof initializeCalendar !== 'undefined') {
- initializeCalendar();
- }
- if(typeof initIntakeEvents !== 'undefined') {
- initIntakeEvents();
- }
- } else {
- // fallback
- console.warn('MC: Target page failed: ' + _href);
- targetParent.html('<p class="text-danger p-3 small">Target page not found or returned error: <b>' + _href + '</b></p>');
- }
- hideMask();
- }
- function fastLoad(_href, _history = true, _useCache = true) {
- showMask();
- // 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);
- }
- }
- if(target[0] === '/') target = target.substr(1);
- window.top.history.pushState(target, null, '/mc/' + target);
- }
- if (_useCache && !!fastCache[_href]) {
- onFastLoaded(fastCache[_href], _href, _history);
- } else {
- $.get(_href, function(_data) {
- onFastLoaded(_data, _href, _history);
- }).fail(function() {
- onFastLoaded('error', _href, _history);
- });
- }
- }
- function initPrimaryForm() {
- var primaryForm = $('.primary-form:visible');
- if (primaryForm.length) {
- var rte = primaryForm.first().find('[contenteditable="true"]').first();
- if(rte.length) {
- rte.focus().select();
- }
- else {
- primaryForm.first().find('input, textarea, select').first().focus().select();
- }
- }
- }
- 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;
- }
- function initCreateNote() {
- $(document)
- .off('click.create-note', '.create-auto-note-trigger')
- .on('click.create-note', '.create-auto-note-trigger', function() {
- showMask();
- var clientUid = $(this).attr('data-patient-uid');
- $.post('/api/note/createUsingFreeTextHtml', {
- clientUid: clientUid,
- hcpProUid: $(this).attr('data-hcp-uid'),
- effectiveDateEST: $(this).attr('data-effective-date'),
- }, function(_data) {
- hideMask();
- if (!_data.success) {
- toastr.error(_data.message);
- }
- else {
- fastLoad('/patients/view/' + clientUid + '/notes/view/' + _data.data, true, false);
- }
- }, 'json');
- });
- }
- function initQuillEdit(_selector = '.note-content[auto-edit]') {
- $(document)
- .off('click.enable-edit', '.note-content:not([auto-edit])')
- .on('click.enable-edit', '.note-content:not([auto-edit])', function() {
- $(this).attr('auto-edit', 1);
- initQuillEdit();
- initPrimaryForm();
- });
- if(!$(_selector).length) return;
- var noteUid = $(_selector).attr('data-note-uid');
- var qe = new Quill(_selector, {
- theme: 'snow'
- });
- var toolbar = $(qe.container).prev('.ql-toolbar');
- var saveButton = $('<button class="btn btn-sm btn-primary w-auto px-3 py-0 text-sm text-white save-note-content">Save</button>');
- toolbar.append(saveButton);
- saveButton.on('click', function() {
- $.post('/api/note/putFreeTextHtml', {
- uid: noteUid,
- freeTextHtml: qe.root.innerHTML,
- }, function(_data) {
- if (!_data.success) {
- toastr.error(_data.message);
- }
- else {
- // toastr.success('Note saved');
- // saveButton.prop('disabled', true);
- fastLoad(window.top.location.pathname.substr(3), false, false);
- }
- }, 'json');
- });
- // qe.on('text-change', function() {
- // saveButton.prop('disabled', false);
- // });
- }
|