mc.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. window.top.currentMcUrl = '';
  2. window.top.addEventListener('popstate', function (event) {
  3. window.setTimeout(function () {
  4. hideMask();
  5. hideMoeFormMask();
  6. if (!event || (!event.state && event.state !== '')) {
  7. // console.error('ALIX No state!', event, event.state);
  8. return;
  9. }
  10. var state = event.state;
  11. if (state === '') state = '/';
  12. if (state[0] !== '/') state = '/' + state;
  13. if (!!state) fastLoad(state, false, true);
  14. }, 0);
  15. });
  16. $(document).ready(function () {
  17. var originalLocationPath = window.location.pathname;
  18. var params = window.location.search;
  19. // TODO make it so if param 'xy' is passed in, it doesn't go to parent
  20. var noMcStuffOnLoad = originalLocationPath.indexOf('notes') >= 0;
  21. if(!window.noMc){
  22. if (window.location.pathname === window.top.location.pathname) {
  23. window.top.location.href = '/mc' + window.location.pathname + window.location.search;
  24. return;
  25. }
  26. }
  27. if(window.noMc) return;
  28. // window.top.ensureRHS();
  29. $(document).on('click', '.stag_rhs_toggle', function () {
  30. var state = window.top.toggleRHS(),
  31. icon = $(this).find('i');
  32. if (state === 'collapsed') {
  33. icon.removeClass().addClass('fa fa-arrow-left');
  34. } else {
  35. icon.removeClass().addClass('fa fa-arrow-right');
  36. }
  37. });
  38. var body = $(window.top.document.body),
  39. icon = $('.stag_rhs_toggle i');
  40. if (body.is('.stag_rhs_collapsed')) {
  41. icon.removeClass().addClass('fa fa-arrow-left');
  42. }
  43. initCreateNote();
  44. initQuillEdit();
  45. initFastLoad();
  46. initPrimaryForm();
  47. initPatientPresenceIndicator();
  48. runMCInitializers();
  49. initFileInputs();
  50. // if(typeof initializeCalendar !== 'undefined') {
  51. // initializeCalendar();
  52. // }
  53. // if(typeof initIntakeEvents !== 'undefined') {
  54. // initIntakeEvents();
  55. // }
  56. // populate history on fresh load
  57. var target = window.top.location.pathname + window.top.location.search;
  58. if (target.indexOf('/mc') === 0) {
  59. target = target.split('/mc')[1];
  60. }
  61. if(noMcStuffOnLoad){
  62. target = originalLocationPath;
  63. }
  64. if(!window.noMc){
  65. fastLoad(target, true, false, true);
  66. }
  67. });
  68. function enableTimeSpecificFields(_checked, _valueClass, _rangeClass) {
  69. if (_valueClass) $('.' + _valueClass).prop('disabled', _checked);
  70. if (_rangeClass) $('.' + _rangeClass).prop('disabled', !_checked);
  71. }
  72. function toggleDisabledAsNeeded(_el, _targetValue, _enableClass, _disableClass) {
  73. if (_el.value === _targetValue) {
  74. if (_enableClass) $('.' + _enableClass).prop('disabled', false);
  75. if (_disableClass) $('.' + _disableClass).prop('disabled', true);
  76. }
  77. else {
  78. if (_enableClass) $('.' + _enableClass).prop('disabled', true);
  79. if (_disableClass) $('.' + _disableClass).prop('disabled', false);
  80. }
  81. }
  82. function toggleVisibilityAsNeeded(_el, _targetValue, _visibleClass, _hiddenClass) {
  83. if (_el.value === _targetValue) {
  84. if (_visibleClass) $('.' + _visibleClass).removeClass('d-none');
  85. if (_hiddenClass) $('.' + _hiddenClass).addClass('d-none');
  86. }
  87. else {
  88. if (_visibleClass) $('.' + _visibleClass).addClass('d-none');
  89. if (_hiddenClass) $('.' + _hiddenClass).removeClass('d-none');
  90. }
  91. }
  92. var fastCache = {};
  93. function initFastLoad(_parent = false) {
  94. var allAs = $('a[href]:not([onclick]):not([href="#"]):not([native])');
  95. if (_parent) {
  96. allAs = _parent.find('a[href]:not([onclick]):not([href="#"]):not([native])');
  97. }
  98. // clear cache
  99. if (!_parent) {
  100. fastCache = {};
  101. }
  102. else {
  103. allAs.each(function () {
  104. if (typeof fastCache[this.href] !== 'undefined') {
  105. delete fastCache[this.href];
  106. }
  107. });
  108. }
  109. // find links without event handlers
  110. allAs.each(function () {
  111. if (!$(this).closest('[moe]').length) {
  112. if ($(this).closest('.dropdown-menu').length) {
  113. enableFastLoad(this, true);
  114. } else {
  115. // var handlers = findEventHandlers('click', this);
  116. // if (!handlers || !handlers.length) {
  117. enableFastLoad(this);
  118. // }
  119. }
  120. }
  121. });
  122. function enableFastLoad(_a, _menuItem = false) {
  123. $(_a)
  124. .off('click.fast-load')
  125. .on('click.fast-load', function () {
  126. fastLoad(this.href, true, true);
  127. $('.dropdown-menu')
  128. .removeClass('show')
  129. .prev('.dropdown-toggle').attr('aria-expanded', 'false');
  130. return false;
  131. });
  132. // console.info('FastLoad enabled for ' + _a.innerText + ' [' + _a.href + ']');
  133. }
  134. // fast cache
  135. // allAs = $('a[href]:not([onclick]):not([href="#"])');
  136. // allAs.each(function () {
  137. // var a = this;
  138. // $.get(a.href, function (_data) {
  139. // fastCache[a.href] = _data;
  140. // });
  141. // });
  142. }
  143. function onFastLoaded(_data, _href, _history, _target = null) {
  144. if (!Number.isInteger(_data)) {
  145. _data = '<div>' + _data + '</div>';
  146. // do for each element in _target
  147. let responseError = false;
  148. let originalTarget = _target;
  149. _target = _target ? _target : '.stag-content';
  150. _target = _target.split(',').map(_x => $.trim(_x));
  151. for (let i = 0; i < _target.length; i++) {
  152. let t = _target[i];
  153. let targetElement = $(t).first();
  154. if(targetElement.length) {
  155. let sourceElement = $(_data).find(t).first();
  156. if (sourceElement && sourceElement.length) {
  157. targetElement.html(sourceElement.html());
  158. initFastLoad(targetElement);
  159. console.log('ALIX loaded element: ' + t);
  160. }
  161. else {
  162. responseError = true;
  163. }
  164. }
  165. else {
  166. responseError = true;
  167. }
  168. }
  169. if (!responseError) {
  170. hideMask();
  171. hideMoeFormMask();
  172. window.setTimeout(function () {
  173. initCreateNote();
  174. initQuillEdit();
  175. initPrimaryForm();
  176. initPatientPresenceIndicator();
  177. initFileInputs();
  178. initMoes();
  179. runMCInitializers(originalTarget);
  180. if (window.top.currentMcUrl.split('?')[0] !== window.top.location.href.split('?')[0]) {
  181. $(window).scrollTop(0);
  182. }
  183. window.top.currentMcUrl = window.top.location.href;
  184. }, 0);
  185. }
  186. else {
  187. $('.stag-content').first().html('<p class="text-danger p-3 small">Error on page: <b>' + _href + '</b></p>');
  188. hideMask();
  189. hideMoeFormMask();
  190. }
  191. } else {
  192. // fallback
  193. let msg = 'Unable to open page: ';
  194. switch (_data) {
  195. case 403:
  196. msg = 'You do not have access to this page: ';
  197. break;
  198. case 404:
  199. msg = 'Page not found: ';
  200. break;
  201. case 500:
  202. msg = 'Error on page: ';
  203. break;
  204. case 0: // not logged in - refresh top level window to go to login
  205. window.top.location = '/';
  206. break;
  207. }
  208. console.warn('MC: Target page failed: ' + _href);
  209. $('.stag-content').first().html('<p class="text-danger p-3 small"><b>' + _data + '</b> - ' + msg + '<b>' + _href + '</b></p>');
  210. hideMask();
  211. hideMoeFormMask();
  212. }
  213. $('html, body').removeClass('no-scroll');
  214. }
  215. var fastReload = function(_target = null) {
  216. var targetLocation = window.top.location.pathname + window.top.location.search;
  217. if(targetLocation.indexOf('/mc') === 0) {
  218. targetLocation = targetLocation.substr(3);
  219. }
  220. if(targetLocation === '' || targetLocation[0] !== '/') targetLocation = '/' + targetLocation;
  221. fastLoad(targetLocation, false, false, false, _target);
  222. return false;
  223. }
  224. function fastLoad(_href, _history = true, _useCache = true, _replaceState = false, _target = null) {
  225. let domPro = $(window.top.document.body).attr('data-pro-uid'),
  226. lsPro = window.top.localStorage.currentProUid;
  227. if(lsPro && domPro && lsPro !== domPro) {
  228. console.warn('ALIX Looks like you have a session as another pro on another tab. Refreshing window.top...');
  229. window.top.location.href = _href;
  230. return false;
  231. }
  232. showMask();
  233. if (_href === '') _href = '/';
  234. // push state
  235. if (_history && window.parent === window.top) {
  236. var target = _href;
  237. if (target.indexOf('//') !== -1) {
  238. target = target.split('//')[1];
  239. if (target.indexOf('/') !== -1) {
  240. target = target.substr(target.indexOf('/') + 1);
  241. }
  242. }
  243. if (target[0] === '/') target = target.substr(1);
  244. if (_replaceState) {
  245. window.top.history.replaceState(target, null, '/mc/' + target);
  246. }
  247. else {
  248. window.top.history.pushState(target, null, '/mc/' + target);
  249. }
  250. }
  251. // dont show top nav if in iframe
  252. if(window !== window.top && window.parent !== window.top) {
  253. $('body').addClass('in-iframe');
  254. }
  255. if (_useCache && !!fastCache[_href]) {
  256. onFastLoaded(fastCache[_href], _href, _history, _target);
  257. } else {
  258. let cleanedHREF = _href;
  259. if(cleanedHREF.length > 1 && cleanedHREF[cleanedHREF.length - 1] === '/') {
  260. cleanedHREF = cleanedHREF.substr(0, cleanedHREF.length - 1);
  261. }
  262. if(cleanedHREF.length > 2) {
  263. cleanedHREF = cleanedHREF.replace('/?', '?');
  264. }
  265. if(cleanedHREF.length > 0 && cleanedHREF[0] === '?') cleanedHREF = '/' + cleanedHREF;
  266. $.get(cleanedHREF, function (_data) {
  267. onFastLoaded(_data, _href, _history, _target);
  268. }).fail(function (_jqXhr) {
  269. onFastLoaded(_jqXhr.status, _href, _history, _target);
  270. });
  271. }
  272. }
  273. function initPrimaryForm(_form = false) {
  274. var primaryForm = _form ? _form : $('.primary-form:visible');
  275. if (primaryForm.length) {
  276. primaryForm = primaryForm.first();
  277. var rte = primaryForm.find('[contenteditable="true"]').first();
  278. if (rte.length) {
  279. rte.focus().select();
  280. }
  281. else {
  282. if (primaryForm.find('[autofocus]:visible').length) {
  283. primaryForm.find('[autofocus]:visible').first().focus().select();
  284. }
  285. else {
  286. primaryForm.find('input:not([type="hidden"]):visible, textarea:visible, select:visible').first().focus().select();
  287. }
  288. }
  289. }
  290. }
  291. function openInRHS(_url) {
  292. window.top.showRHS();
  293. var icon = $('.stag_rhs_toggle i');
  294. icon.removeClass().addClass('fa fa-arrow-right');
  295. window.top.openInRHS(_url);
  296. return false;
  297. }
  298. function initCreateNote() {
  299. $(document)
  300. .off('click.create-note', '.create-auto-note-trigger')
  301. .on('click.create-note', '.create-auto-note-trigger', function () {
  302. createNewNote($(this).attr('data-patient-uid'), $(this).attr('data-hcp-uid'), $(this).attr('data-effective-date'));
  303. });
  304. if ($('select[name="hasMcpDoneOnboardingVisit"]').length) {
  305. $('select[name="hasMcpDoneOnboardingVisit"]').trigger('change');
  306. }
  307. }
  308. function createNewNote(_patientUid, _hcpUid, _date) {
  309. hideMoeFormMask();
  310. showMask();
  311. $.post('/api/note/createUsingFreeTextHtml', {
  312. clientUid: _patientUid,
  313. hcpProUid: _hcpUid,
  314. effectiveDateEST: _date,
  315. }, function (_data) {
  316. hideMask();
  317. if (!_data.success) {
  318. toastr.error(_data.message);
  319. }
  320. else {
  321. fastLoad('/patients/view/' + _patientUid + '/notes/view/' + _data.data, true, false);
  322. }
  323. }, 'json');
  324. }
  325. function initQuillEdit(_selector = '.note-content[auto-edit]') {
  326. $(document)
  327. .off('click.enable-edit', '.note-content:not([auto-edit]):not(.readonly)')
  328. .on('click.enable-edit', '.note-content:not([auto-edit]):not(.readonly)', function () {
  329. $(this).attr('auto-edit', 1);
  330. initQuillEdit();
  331. initPrimaryForm();
  332. initPatientPresenceIndicator();
  333. });
  334. if (!$(_selector).length) return;
  335. var noteUid = $(_selector).attr('data-note-uid');
  336. var qe = new Quill(_selector, {
  337. theme: 'snow',
  338. modules: stagQuillConfig
  339. });
  340. var toolbar = $(qe.container).prev('.ql-toolbar');
  341. var saveButton = $('<button class="btn btn-sm btn-primary w-auto px-3 py-0 text-sm text-white save-note-content">Save</button>');
  342. toolbar.append(saveButton);
  343. saveButton.on('click', function () {
  344. $.post('/api/note/putFreeTextHtml', {
  345. uid: noteUid,
  346. freeTextHtml: qe.root.innerHTML,
  347. }, function (_data) {
  348. if (!_data.success) {
  349. toastr.error(_data.message);
  350. }
  351. else {
  352. // toastr.success('Note saved');
  353. // saveButton.prop('disabled', true);
  354. fastLoad(window.top.location.pathname.substr(3), false, false);
  355. }
  356. }, 'json');
  357. });
  358. // give a unique id to this editor instance
  359. var editorID = Math.ceil(Math.random() * 99999);
  360. // add button for new shortcut
  361. var newSCButton = $('<button class="btn btn-sm btn-default w-auto px-2 ml-2 border py-0 ' +
  362. 'text-sm add-shortcut" data-editor-id="' + editorID + '">+ Shortcut</button>');
  363. toolbar.append(newSCButton);
  364. // qe.on('text-change', function() {
  365. // saveButton.prop('disabled', false);
  366. // });
  367. $('.ql-editor[contenteditable]')
  368. .attr('data-editor-id', editorID)
  369. .attr('with-shortcuts', 1);
  370. }
  371. function initQuillRTE(_selector, _config = false, _shortcuts = false) {
  372. if(!_config) _config = stagQuillConfig;
  373. $(_selector).each(function() {
  374. $(this).wrap(
  375. $('<div class="border-left border-right rte-holder"/>')
  376. .attr('data-shortcuts', '')
  377. );
  378. // give a unique id to this editor instance
  379. var editorID = Math.ceil(Math.random() * 99999),
  380. fieldName = this.name;
  381. var ti = $('<input type="hidden" />')
  382. .val(this.value)
  383. .attr('name', this.name)
  384. .insertBefore(this);
  385. var ce = $('<div data-editor-id="' + editorID + '" data-field="' + this.name + '"/>')
  386. .html(this.value)
  387. .insertBefore(this);
  388. $(this).remove();
  389. var qe = new Quill('[data-editor-id="' + editorID + '"]', {
  390. theme: 'snow',
  391. modules: _config ? _config : stagQuillConfig
  392. });
  393. var toolbar = $(qe.container).prev('.ql-toolbar');
  394. // add button for new shortcut
  395. if(_shortcuts) {
  396. var newSCButton = $('<button class="btn bg-white btn-sm btn-default text-primary w-auto px-2 border py-0 ' +
  397. 'text-sm add-shortcut" data-editor-id="' + editorID + '">+ Shortcut</button>');
  398. toolbar.append(newSCButton);
  399. }
  400. qe.on('text-change', function() {
  401. ti.val(qe.root.innerHTML);
  402. });
  403. $(qe.container)
  404. .find('.ql-editor[contenteditable]')
  405. .attr('data-field', fieldName)
  406. .attr('data-editor-id', editorID)
  407. .attr('with-shortcuts', 1);
  408. });
  409. }
  410. var patientPresenceTimer = false;
  411. function initFileInputs() {
  412. $(document)
  413. .off('change', 'input[type="file"]')
  414. .on('change', 'input[type="file"]', function(_e) {
  415. if(_e.target.files && _e.target.files.length) {
  416. $(this).attr('selected-file', _e.target.files[0].name);
  417. }
  418. else {
  419. $(this).attr('selected-file', 'No file chosen');
  420. }
  421. return false;
  422. });
  423. $('input[type="file"]').attr('selected-file', 'No file chosen');
  424. }
  425. function initPatientPresenceIndicator() {
  426. return false;
  427. if (patientPresenceTimer !== false) {
  428. window.clearInterval(patientPresenceTimer);
  429. patientPresenceTimer = false;
  430. console.log('Cancelled previous timer!');
  431. }
  432. var elem = $('.patient-presence-indicator[data-patient-uid]');
  433. if (elem.length) {
  434. var patientUid = elem.attr('data-patient-uid');
  435. patientPresenceTimer = window.setInterval(function () {
  436. var elem = $('.patient-presence-indicator[data-patient-uid]');
  437. if (elem.length) {
  438. var patientUid = elem.attr('data-patient-uid');
  439. $.get('/patients/' + patientUid + '/presence', function (_data) {
  440. if (_data.online) {
  441. elem.addClass('online');
  442. }
  443. else {
  444. elem.removeClass('online');
  445. }
  446. }, 'json');
  447. }
  448. }, 15000); // once in 15 seconds
  449. }
  450. }
  451. // not really the place for this!
  452. // find a better place to put this
  453. window.fillJsonDataField = function(form, field = "data") {
  454. // add [data-name] values to payload
  455. if(form.is('[data-field-name]')) {
  456. field = form.attr('data-field-name');
  457. }
  458. let dataField = form.find('[name="' + field + '"]').first();
  459. let parsed = null;
  460. if(dataField.val()) {
  461. parsed = JSON.parse(dataField.val());
  462. }
  463. form.find('[data-name]').each(function() {
  464. if(!parsed) parsed = {};
  465. let keys = $(this).attr('data-name').split('->');
  466. let currentNode = parsed;
  467. for (let i = 0; i < keys.length; i++) {
  468. if(i !== keys.length - 1) {
  469. if(typeof currentNode[keys[i]] === 'undefined') {
  470. currentNode[keys[i]] = {};
  471. }
  472. currentNode = currentNode[keys[i]];
  473. }
  474. else {
  475. if($(this).is(':radio')) {
  476. if($(this).prop('checked')) {
  477. currentNode[keys[i]] = this.value;
  478. }
  479. }
  480. else if($(this).is(':checkbox')) {
  481. currentNode[keys[i]] = $(this).prop('checked');
  482. }
  483. else {
  484. currentNode[keys[i]] = $(this).val();
  485. }
  486. }
  487. }
  488. });
  489. if(parsed) {
  490. dataField.val(JSON.stringify(parsed));
  491. }
  492. return parsed;
  493. }
  494. window.saveVisitForm = function(_trigger, _silent = false, _close = false, _doneCallback = null) {
  495. let form = $(_trigger).closest('form');
  496. if (!_silent && !form[0].checkValidity()) {
  497. form[0].reportValidity();
  498. return false;
  499. }
  500. let parsed = fillJsonDataField(form);
  501. let closeOnSave = false, noteSection = form.closest('.note-section');
  502. if($(_trigger).closest('[visit-moe]').is('[close-on-save]')) {
  503. closeOnSave = true;
  504. }
  505. // disallow-if-value-same-as
  506. let compareWith = false;
  507. if(form.find('.disallow-if-value-same-as')) {
  508. compareWith = $.trim(form.find('.disallow-if-value-same-as').text());
  509. if(compareWith && parsed) {
  510. if(!parsed.value) {
  511. alert('Value cannot be empty!');
  512. return false;
  513. }
  514. let newValue = $('<div/>').html(parsed.value).text().replace(/[^a-zA-Z0-9]/g, '');
  515. if(newValue === '') {
  516. alert('Value cannot be empty!');
  517. return false;
  518. }
  519. if(newValue === compareWith) {
  520. alert('New value should be different from the previous value!');
  521. return false;
  522. }
  523. }
  524. }
  525. if(!_silent) showMask();
  526. $.post(form.attr('url'), form.serialize(), _data => {
  527. if(!hasResponseError(_data)) {
  528. if(typeof window.updateAllSegmentsInResponse !== 'undefined') {
  529. window.updateAllSegmentsInResponse(_data, true, _silent);
  530. // if /api/visitPoint/upsertChildReview - refresh subjective_general_v2
  531. // if /api/visitPoint/upsertChildPlan - refresh plan_free_text_v2
  532. if(form.attr('url') === '/api/visitPoint/upsertChildReview') {
  533. $('.visit-segment[data-segment-template-name="subjective_general_v2"]').find('.refresh-segment').trigger('click');
  534. }
  535. else if(form.attr('url') === '/api/visitPoint/upsertChildPlan') {
  536. $('.visit-segment[data-segment-template-name="plan_free_text_v2"]').find('.refresh-segment').trigger('click');
  537. }
  538. }
  539. if(typeof window.refreshRHSSidebar !== 'undefined') {
  540. window.refreshRHSSidebar();
  541. }
  542. if(!_silent) {
  543. hideMask();
  544. if(noteSection.length) {
  545. if (closeOnSave) {
  546. noteSection.removeClass('edit');
  547. let segmentUid = form.find('[name="segmentUid"]').first();
  548. segmentUid = segmentUid.length ? segmentUid.val() : false;
  549. if (segmentUid) {
  550. window.setTimeout(() => {
  551. $('.note-tree-node>a[data-segment-uid="' + segmentUid + '"]').trigger('click');
  552. }, 250);
  553. }
  554. }
  555. }
  556. if($(_trigger).closest('[visit-moe]').closest('.stag-popup').length) {
  557. refreshDynamicStagPopup();
  558. }
  559. }
  560. if(_close) {
  561. closeStagPopup();
  562. }
  563. if(!!_doneCallback) {
  564. _doneCallback();
  565. }
  566. }
  567. }, 'json');
  568. return false;
  569. };
  570. window.initSegmentMoes = function(_parent) {
  571. $('body')
  572. .off('mousedown.visit-moe-outside-click')
  573. .on('mousedown.visit-moe-outside-click', function (e) {
  574. if ($(e.target).closest('[visit-moe]').length ||
  575. $(e.target).closest('#create-shortcut-form').length ||
  576. $(e.target).is('#create-shortcut-form') ||
  577. $(e.target).is('.stag-shortcuts .sc') ||
  578. $(e.target).closest('.ui-datepicker').length) {
  579. return;
  580. }
  581. $('[visit-moe] [url]:not([show])').hide();
  582. });
  583. _parent.find('[visit-moe] [submit]')
  584. .off('click.visit-moe-submit')
  585. .on('click.visit-moe-submit', function() {
  586. saveVisitForm(this);
  587. return false;
  588. });
  589. _parent.find('[visit-moe]>a[start]')
  590. .off('click.visit-moe-show')
  591. .on('click.visit-moe-show', function () {
  592. $('[visit-moe] [url]:not([show])').hide();
  593. $(this)
  594. .closest('[visit-moe]')
  595. .find('form[url]')
  596. .show();
  597. return false;
  598. });
  599. _parent.find('[visit-moe] [cancel]')
  600. .off('click.visit-moe-cancel')
  601. .on('click.visit-moe-cancel', function() {
  602. $(this).closest('[visit-moe]').find('[url]:not([show])').hide();
  603. if($(this).closest('[visit-moe]').is('[close-on-cancel]')) {
  604. $(this).closest('.note-section').removeClass('edit');
  605. }
  606. return false;
  607. });
  608. $(document)
  609. .off('keydown.visit-moe-escape')
  610. .on('keydown.visit-moe-escape', function (e) {
  611. if(e.which === 27) {
  612. if(!isEventConsumed(e)) {
  613. let visibleMoes = $('[visit-moe] [url]:not([show]):visible');
  614. if (visibleMoes.length) {
  615. visibleMoes.hide();
  616. markEventAsConsumed(e);
  617. return false;
  618. }
  619. }
  620. }
  621. });
  622. };
  623. window.isEventConsumed = function(_e) {
  624. return _e && _e.originalEvent && _e.originalEvent.stagCosumed
  625. };
  626. window.markEventAsConsumed = function(_e) {
  627. if(_e && _e.originalEvent) {
  628. _e.originalEvent.stagCosumed = true;
  629. }
  630. };