yemi.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. if (typeof focusOn == 'undefined') {
  2. var focusOn = 'globalSearch';
  3. }
  4. var ajaxGoing = false;
  5. var showMask = function (_text = '') {
  6. $('body').css('opacity', 0.6);
  7. $('#mask').show();
  8. if(!!_text) {
  9. $('<div class="mask-text-addition" />').text('Initializing note. Please wait...').appendTo('body');
  10. }
  11. }
  12. var hideMask = function () {
  13. $('body').css('opacity', 1);
  14. $('#mask').hide();
  15. $('.mask-text-addition').remove();
  16. }
  17. var showMoeFormMask = function () {
  18. $('#moe-form-mask').show();
  19. }
  20. var hideMoeFormMask = function () {
  21. $('#moe-form-mask').hide();
  22. $('#create-shortcut-form').hide();
  23. }
  24. $(document).ready(function () {
  25. hideMask();
  26. });
  27. $(document).ready(function () {
  28. $("input[type=number]").keydown(function (e) {
  29. // Allow: backspace, delete, tab, escape, enter and .
  30. if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
  31. // Allow: Ctrl+A, Command+A
  32. (e.keyCode == 65 && (e.ctrlKey === true || e.metaKey === true)) ||
  33. // Allow: home, end, left, right, down, up
  34. (e.keyCode >= 35 && e.keyCode <= 40)) {
  35. // let it happen, don't do anything
  36. return;
  37. }
  38. // Ensure that it is a number and stop the keypress
  39. if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
  40. e.preventDefault();
  41. }
  42. });
  43. });
  44. $(function () {
  45. $('input[type=checkbox][forceCb]').on('click', function () {
  46. var name = $(this).attr('forceCb');
  47. var code = $(this).attr('code');
  48. var form = $(this).closest('form');
  49. var hiddenField = $(form).find('input[code=\'' + code + '\']');
  50. var value = $(this).prop('checked') ? 'on' : 'off';
  51. console.log('name', name, 'code', code, 'value', value);
  52. $(hiddenField).val(value);
  53. });
  54. });
  55. var doAjax = function (url, data, pre, post, onSuccess, onFailure, suppressErrorMessage, onHttpFailure, shouldHideMask, immediatelyHideMaskOnReply) {
  56. console.log(data);
  57. if (ajaxGoing) {
  58. console.log('ajax stopped!');
  59. //return; TODO: fix and re-enable return
  60. }
  61. ajaxGoing = true;
  62. if (!shouldHideMask) {
  63. showMask();
  64. }
  65. jQuery.ajax(url, {
  66. dataType: 'json',
  67. data: data,
  68. type: 'POST',
  69. beforeSend: function () {
  70. if (pre) {
  71. pre();
  72. }
  73. }
  74. })
  75. .done(function (response, b) {
  76. console.log(response);
  77. var success = response.success;
  78. if (success) {
  79. if (onSuccess) {
  80. onSuccess(response.data);
  81. }
  82. } else {
  83. if (onFailure) {
  84. onFailure(response.message);
  85. }
  86. if (!suppressErrorMessage) {
  87. //toast the error message
  88. //alert(response.message);
  89. toastr.error(response.message); // , toastr.success("message") ... .warning(), .error()
  90. }
  91. hideMask();
  92. }
  93. if (immediatelyHideMaskOnReply) {
  94. hideMask();
  95. }
  96. ajaxGoing = false;
  97. })
  98. .fail(function (jqXHR, textStatus) {
  99. hideMask();
  100. toastr.error('Unable to process');
  101. if (onHttpFailure) {
  102. onHttpFailure(textStatus);
  103. }
  104. ajaxGoing = false;
  105. })
  106. .always(function () {
  107. if (post) {
  108. post();
  109. }
  110. ajaxGoing = false;
  111. });
  112. };
  113. var justLog = false; //THIS IS FOR TEST MODE, FORMS WILL NOT TRIGGER REFRESH/REDIR
  114. var pageReload = function () {
  115. setTimeout(function () {
  116. hideMoeFormMask();
  117. fastReload();
  118. }, 500);
  119. };
  120. var fastReload = function() {
  121. var targetLocation = window.top.location.pathname;
  122. if(targetLocation.indexOf('/mc') === 0) {
  123. targetLocation = targetLocation.substr(3);
  124. }
  125. if(targetLocation === '' || targetLocation[0] !== '/') targetLocation = '/' + targetLocation;
  126. fastLoad(targetLocation, false, false);
  127. }
  128. if (typeof String.prototype.startsWith != 'function') {
  129. // see below for better implementation!
  130. String.prototype.startsWith = function (str) {
  131. return this.indexOf(str) === 0;
  132. };
  133. }
  134. $(function () {
  135. $('[addressLine1]').each(function () {
  136. var moe = $(this).closest('[moe]');
  137. var a = {};
  138. a.addressLine1 = $(this);
  139. a.addressLine2 = $(moe).find('[addressLine2]');
  140. a.addressCity = $(moe).find('[addressCity]');
  141. a.addressState = $(moe).find('[addressState]');
  142. a.addressPostcode = $(moe).find('[addressPostcode]');
  143. var getAddress = function () {
  144. var address = {};
  145. for (var prop in a) {
  146. var val = $(a[prop]).val();
  147. address[prop] = val;
  148. }
  149. return address;
  150. }
  151. var getFormattedAddress = function (address) {
  152. var x = '<p>';
  153. x += address.addressLine1 + (address.addressLine2 ? ', ' + address.addressLine2 : '') + '<br/>' + address.addressCity + ', ' + address.addressState + ' ' + address.addressPostcode;
  154. x += '</p>';
  155. return x;
  156. }
  157. var suggestAddress = function () {
  158. var address = getAddress();
  159. //var doAjax = function (url, data, pre, post, onSuccess, onFailure, suppressErrorMessage, onHttpFailure, shouldHideMask)
  160. var onSuccess = function (data) {
  161. console.log('SUCCESS!!!', data);
  162. }
  163. var onFailure = function (message) {
  164. if (message.startsWith('Invalid:: INVALID ADDRESS - SUGGESTED:')) {
  165. var suggestionText = message.substring(message.indexOf('{'));
  166. var suggestion = JSON.parse(suggestionText);
  167. console.log('SUGGESTION!!!', suggestion);
  168. $('#myModal').attr('title', 'Address suggestion');
  169. $('#myModal').html('<h3>Currently set address:</h3>' + getFormattedAddress(address) + '<h3>Suggestion:</h3>' + getFormattedAddress(suggestion));
  170. $('#myModal').dialog({
  171. height: 400,
  172. width: 500,
  173. modal: true,
  174. buttons: [{
  175. text: 'Use suggestion',
  176. click: function () {
  177. for (var prop in a) {
  178. $(a[prop]).val(suggestion[prop]);
  179. }
  180. $(this).dialog('close');
  181. }
  182. },
  183. {
  184. text: 'Keep original',
  185. click: function () {
  186. $(this).dialog('close');
  187. }
  188. }
  189. ]
  190. });
  191. } else if (message.startsWith('Invalid:: INVALID ADDRESS')) {
  192. console.log('NO SUGGESTION!!!');
  193. $('#myModal').attr('title', 'No address found');
  194. $('#myModal').html('<h3>Currently set address:</h3>' + getFormattedAddress(address) + '<h3>No suggestion!</h3>');
  195. $('#myModal').dialog({
  196. height: 400,
  197. width: 500,
  198. modal: true,
  199. buttons: [{
  200. text: 'Erase address fields',
  201. click: function () {
  202. for (var prop in a) {
  203. $(a[prop]).val('');
  204. }
  205. $(this).dialog('close');
  206. }
  207. },
  208. {
  209. text: 'Keep original',
  210. click: function () {
  211. $(this).dialog('close');
  212. }
  213. }
  214. ]
  215. });
  216. }
  217. }
  218. doAjax('/api/service/verifyAddress', address, null, null, onSuccess, onFailure, true, null, false, true);
  219. }
  220. var isAddressComplete = function () {
  221. var address = getAddress();
  222. return address.addressLine1 && ((address.addressCity && address.addressState) || address.addressPostcode) ? true : false;
  223. }
  224. for (var prop in a) {
  225. var part = a[prop];
  226. $(part).on('change', function () {
  227. console.log(getAddress());
  228. var isComplete = isAddressComplete();
  229. if (isComplete) {
  230. suggestAddress();
  231. }
  232. });
  233. $(part).attr('autocomplete', 'off');
  234. }
  235. });
  236. });
  237. var initMoes = function() {
  238. $('[moe]').each(function () {
  239. if($(this).is('[initialized]')) { // skip to next moe
  240. return true;
  241. }
  242. var moe = $(this);
  243. moe.isProcessing = false;
  244. var info = moe.find('[info]')[0]; // OPTIONAL
  245. var start = moe.find('[start]')[0]; // OPTIONAL
  246. var form = moe.find('[url]')[0]; // REQUIRED
  247. var url = $(form).attr('url'); // REQUIRED
  248. var redir = $(form).attr('redir'); // OPTIONAL
  249. var submit = moe.find('[submit]'); // REQUIRED
  250. var cancel = moe.find('[cancel]')[0]; // OPTIONAL
  251. // ajax load - OPTIONAL
  252. var moeParent = $(form).closest('[moe-parent][url]');
  253. if ($(this).attr('formOff') != null) {
  254. $(form).find(':input').attr('disabled', true);
  255. var formOn = $(this).find('[formOn]');
  256. $(formOn).attr('disabled', false);
  257. $(submit).hide();
  258. $(formOn).click(function () {
  259. $(form).find(':input').attr('disabled', false);
  260. $(submit).show();
  261. $(formOn).hide();
  262. return false;
  263. });
  264. }
  265. var realForm = form;
  266. //init set display inline so toggle remembers inline state
  267. var formToggle = false;
  268. var infoToggle = false;
  269. if (start) {
  270. $(start).click(function () {
  271. $('.dropdown-menu[aria-labelledby="practice-management"]')
  272. .removeClass('show')
  273. .prev('.dropdown-toggle').attr('aria-expanded', 'false');
  274. if ($(realForm).attr('show') == null) {
  275. if (!formToggle && $(realForm).attr('liner') != null) {
  276. $(realForm).css('display', 'inline');
  277. formToggle = true;
  278. } else {
  279. var isRealFormVisible = $(realForm).is(':visible');
  280. if(isRealFormVisible){
  281. hideMoeFormMask();
  282. $(realForm).toggle(100);
  283. }else{
  284. if($(realForm).is('form')) $(realForm)[0].reset();
  285. showMoeFormMask();
  286. setTimeout(function() {
  287. $('[moe]>form:visible').hide();
  288. $(realForm).toggle(100);
  289. initPrimaryForm($(realForm));
  290. }, 100);
  291. }
  292. }
  293. }
  294. if (!infoToggle && info && $(info).attr('show') == null) {
  295. if ($(info).attr('liner') != null) {
  296. $(info).css('display', 'inline');
  297. infoToggle = true;
  298. } else {
  299. $(info).toggle(100);
  300. }
  301. }
  302. if ($(start).attr('show') == null) {
  303. $(start).toggle(100);
  304. }
  305. var focusOnStart = $(realForm).find('[focusOnStart]');
  306. if (focusOnStart) {
  307. $(focusOnStart).focus();
  308. }
  309. return false;
  310. });
  311. $(start).attr('href', '#');
  312. }
  313. if (cancel) {
  314. $(cancel).click(function (e) {
  315. e.preventDefault();
  316. e.stopImmediatePropagation();
  317. if ($(realForm).attr('show') == null) {
  318. $(realForm).hide(100);
  319. }
  320. if (info && $(info).attr('show') == null) {
  321. $(info).show(100);
  322. }
  323. if (start && $(start).attr('show') == null) {
  324. $(start).show(100);
  325. }
  326. hideMoeFormMask();
  327. });
  328. }
  329. $(submit).click(function (e) {
  330. e.preventDefault();
  331. e.stopImmediatePropagation();
  332. if (moe.isProcessing) {
  333. return false;
  334. }
  335. if ($(submit).attr('confirm')) {
  336. var question = $(submit).attr('confirm');
  337. var cont = confirm(question);
  338. if (cont) {} else {
  339. console.log("ABORTED!");
  340. return;
  341. }
  342. }
  343. // trigger validation
  344. if(!$(form)[0].checkValidity()) {
  345. $(form)[0].reportValidity();
  346. return;
  347. }
  348. moe.isProcessing = true;
  349. var data = {};
  350. var formData = $(form).serializeArray();
  351. formData.forEach(function (field) {
  352. if (data[field.name]) {
  353. if (data[field.name] instanceof Array) {
  354. data[field.name].push(field.value);
  355. } else {
  356. var arr = [];
  357. arr.push(data[field.name]);
  358. arr.push(field.value);
  359. data[field.name] = arr;
  360. }
  361. } else {
  362. data[field.name] = field.value;
  363. }
  364. });
  365. console.log(data);
  366. //var doAjax = function (url, data, pre, post, onSuccess, onFailure, suppressErrorMessage, onHttpFailure, shouldHideMask, immediatelyHideMaskOnReply)
  367. doAjax(url, data, null, function () {
  368. moe.isProcessing = false;
  369. }, function (data) {
  370. if (justLog) { // this is to test!
  371. console.log('RETURNED', data);
  372. } else if (redir) {
  373. if (redir == "back") {
  374. window.top.history.back();
  375. } else {
  376. if (redir.indexOf('[data]') > -1) {
  377. redir = redir.replace('[data]', data);
  378. }
  379. fastLoad(redir, true, false);
  380. }
  381. } else if (moeParent.length) {
  382. showMask();
  383. $.get(moeParent.attr('url'), function(_data) {
  384. moeParent.html(_data);
  385. hideMask();
  386. hideMoeFormMask();
  387. initMoes();
  388. });
  389. } else {
  390. pageReload();
  391. }
  392. }, function (errorMessage) {
  393. }, false);
  394. });
  395. moe.attr('initialized', 1); // mark as initialized
  396. });
  397. }
  398. jQuery(document).ready(function () {
  399. var $ = jQuery;
  400. $('body').mousedown(function(e){
  401. if($(e.target).closest('[moe]').length || $(e.target).closest('#create-shortcut-form').length || $(e.target).is('#create-shortcut-form')){
  402. return;
  403. }
  404. $('[moe] [url]:not([show])').hide();
  405. hideMoeFormMask();
  406. });
  407. initMoes();
  408. $('table[info] input').each(function () {
  409. $(this).prop('readonly', true);
  410. });
  411. //...for checkboxes readonly
  412. $('input[type=checkbox][readonly],input[type=radio][readonly]').each(function () {
  413. var x = this;
  414. var isChecked = $(x).attr('checked');
  415. $(x).change(function () {
  416. $(x).attr('checked', isChecked);
  417. });
  418. });
  419. $('[show-if]').each(function () { //TODO show-if="isOpen" show-if="isTimeSpecific" show-if="refillFrequency=MONTH"
  420. var x = this;
  421. var sel = $(x).attr('show-if');
  422. var selParts = sel.split('=');
  423. var selName = selParts[0];
  424. var selValue = selParts[1];
  425. var useOpposite = selName[0] == '!';
  426. if (useOpposite) {
  427. selName = selName.slice(1);
  428. }
  429. var form = $(x).closest('form');
  430. var conditionFields = $(form).find('[name=' + selName + ']');
  431. function hideX() {
  432. $(x).hide();
  433. }
  434. function showX() {
  435. $(x).show();
  436. }
  437. var go = function () {
  438. if (selValue) {
  439. var value = $(conditionFields).val();
  440. if (value == selValue) {
  441. if (useOpposite) {
  442. hideX()
  443. } else {
  444. showX()
  445. }
  446. } else {
  447. if (useOpposite) {
  448. showX()
  449. } else {
  450. hideX()
  451. }
  452. }
  453. } else {
  454. var isChecked = $(conditionFields).prop('checked');
  455. if (isChecked) {
  456. if (useOpposite) {
  457. hideX()
  458. } else {
  459. showX()
  460. }
  461. } else {
  462. if (useOpposite) {
  463. showX()
  464. } else {
  465. hideX()
  466. }
  467. }
  468. }
  469. };
  470. go();
  471. $(conditionFields).change(function () {
  472. go();
  473. });
  474. });
  475. });
  476. //now goTo is a plugin...
  477. (function ($) {
  478. $.fn.goTo = function (x) {
  479. $('html, body').animate({
  480. scrollTop: ($(this).offset().top - x) + 'px'
  481. }, 1);
  482. return this; // for chaining...
  483. };
  484. })(jQuery);
  485. $(document).ready(function () {
  486. $(".expander").on("click", function () {
  487. var expandedID = $(this).attr("id");
  488. if ($(this).text() == "-") {
  489. $(this).text("+");
  490. } else {
  491. $(this).text("-");
  492. }
  493. $("." + expandedID).toggle();
  494. return false;
  495. });
  496. });
  497. $(document).ready(function () {
  498. $('[showMap]').each(function () {
  499. var mapper = $(this);
  500. var adr = mapper.attr('showMap');
  501. mapper.on('click', function () {
  502. showAddr(adr);
  503. return false;
  504. });
  505. });
  506. });
  507. $(document).ready(function () {
  508. $('[dateRanger]').each(function () {
  509. var dr = $(this);
  510. var rangeTypeSelect = dr.find('select')[0];
  511. var date1Input = dr.find('[date1]')[0];
  512. var date2Input = dr.find('[date2]')[0];
  513. var d1Val = '';
  514. var d2Val = '';
  515. var d1 = function (enable) {
  516. if (enable) {
  517. var hasVal = $(date1Input).val();
  518. if (!hasVal) {
  519. $(date1Input).val(d1Val);
  520. }
  521. $(date1Input).show();
  522. } else {
  523. d1Val = $(date1Input).val();
  524. $(date1Input).val('');
  525. $(date1Input).hide();
  526. }
  527. };
  528. var d2 = function (enable) {
  529. if (enable) {
  530. var hasVal = $(date2Input).val();
  531. if (!hasVal) {
  532. $(date2Input).val(d2Val);
  533. }
  534. $(date2Input).show();
  535. } else {
  536. d2Val = $(date2Input).val();
  537. $(date2Input).val('');
  538. $(date2Input).hide();
  539. }
  540. };
  541. var adjustFields = function () {
  542. var rangeType = $(rangeTypeSelect).val();
  543. if (rangeType == 'all') {
  544. d1();
  545. d2();
  546. } else if (rangeType == 'on-or-before') {
  547. d1(true);
  548. d2();
  549. } else if (rangeType == 'on-or-after') {
  550. d1(true);
  551. d2();
  552. } else if (rangeType == 'between') {
  553. d1(true);
  554. d2(true);
  555. } else if (rangeType == 'on') {
  556. d1(true);
  557. d2();
  558. } else if (rangeType == 'not-on') {
  559. d1(true);
  560. d2();
  561. } else if (rangeType == 'not-in-between') {
  562. d1(true);
  563. d2(true);
  564. }
  565. };
  566. adjustFields();
  567. $(rangeTypeSelect).change(function () {
  568. adjustFields();
  569. });
  570. });
  571. $('[numRanger]').each(function () {
  572. var nr = $(this);
  573. var rangeTypeSelect = nr.find('select')[0];
  574. var num1Input = nr.find('[num1]')[0];
  575. var num2Input = nr.find('[num2]')[0];
  576. var n1 = function (enable) {
  577. if (enable) {
  578. $(num1Input).show();
  579. } else {
  580. $(num1Input).hide();
  581. }
  582. };
  583. var n2 = function (enable) {
  584. if (enable) {
  585. $(num2Input).show();
  586. } else {
  587. $(num2Input).hide();
  588. }
  589. };
  590. var adjustFields = function () {
  591. var rangeType = $(rangeTypeSelect).val();
  592. if (rangeType == 'all') {
  593. n1();
  594. n2();
  595. } else if (rangeType == 'less-than') {
  596. n1(true);
  597. n2();
  598. } else if (rangeType == 'greater-than') {
  599. n1(true);
  600. n2();
  601. } else if (rangeType == 'equal-to') {
  602. n1(true);
  603. n2();
  604. } else if (rangeType == 'between') {
  605. n1(true);
  606. n2(true);
  607. } else if (rangeType == 'not-equal-to') {
  608. n1(true);
  609. n2();
  610. } else if (rangeType == 'not-in-between') {
  611. n1(true);
  612. n2(true);
  613. }
  614. };
  615. adjustFields('all');
  616. $(rangeTypeSelect).change(function () {
  617. adjustFields();
  618. });
  619. });
  620. });
  621. $(document).ready(function () {
  622. $('[minzero]').on('change', function () {
  623. var val = $(this).val();
  624. val = parseFloat(val);
  625. if (val < 0) {
  626. alert('This number cannot be less than zero.');
  627. $(this).val('');
  628. $(this).focus();
  629. }
  630. });
  631. });
  632. var showAddr = function (adr) {
  633. window.open('http://192.241.155.210/geo.php?adr=' + adr, new Date().getTime(), "height=400,width=520");
  634. };
  635. $(document).ready(function () {
  636. var globalSearch = function () {
  637. var substring = $('#globalSearch').val();
  638. console.log("SUBSTRING", substring);
  639. if (substring.length > 2) {
  640. $("#results").show();
  641. $("#results").load("/global-search?substring=" + encodeURIComponent(substring));
  642. } else {
  643. $("#results").hide();
  644. }
  645. };
  646. $("#globalSearch").on('keyup', function (evnt) {
  647. globalSearch();
  648. });
  649. $("#globalSearch").on('focus', function (evnt) {
  650. globalSearch();
  651. });
  652. $("#globalSearch").on('blur', function (evt) {
  653. setTimeout(function () {
  654. $("#results").hide();
  655. }, 500);
  656. });
  657. });
  658. $('a[aller]').attr('href', '#');
  659. var selectAll = true;
  660. $('a[aller]').click(function () {
  661. $('input[type=checkbox][aller]').each(function () {
  662. if (!$(this).is(':disabled')) {
  663. $(this).prop('checked', selectAll);
  664. }
  665. });
  666. selectAll = !selectAll;
  667. return false;
  668. });
  669. $(function () {
  670. $('.showOnLoad').show();
  671. });
  672. $(function () {
  673. var i = 0;
  674. function pulsate() {
  675. $(".urgentIndicator").
  676. animate({
  677. opacity: 0.2
  678. }, 200, 'linear').
  679. animate({
  680. opacity: 1
  681. }, 200, 'linear', pulsate);
  682. }
  683. pulsate();
  684. });
  685. $(function () {
  686. $('[remote-searcher]').each(function () {
  687. var me = this;
  688. $(me).hide();
  689. var selections = [];
  690. var isMulti = typeof $(me).attr('multiple') == 'string';
  691. $(me).find('[rid]').each(function () {
  692. selections.push({
  693. id: $(this).attr('rid'),
  694. display: $(this).attr('display')
  695. });
  696. });
  697. if (!isMulti && selections[0]) {
  698. selections = [selections[0]];
  699. }
  700. $(me).html('');
  701. var url = $(me).attr('remote-searcher');
  702. var charMin = $(me).attr('char-min');
  703. var name = $(me).attr('name');
  704. $(me).append('<select multiple style="display:none;" name="' + name + '"></select><span choices></span><input type="text" style="border:none;margin:5px;width:100%;outline:none;display:none;"/></span>');
  705. $(me).append('<div style="margin-top:2px;background:white;border:1px lightgray solid;display:none;width:99%;position:absolute;z-index:999"></div>');
  706. var choices = $(me).find('span[choices]');
  707. var selectField = $(me).find('select');
  708. var textField = $(me).find('input[type=text]');
  709. var resultDiv = $(me).find('div');
  710. var setResultMask = function () {
  711. $(resultDiv).html('<div style="background-image: url(/icons/vanillaspin.gif); background-repeat: no-repeat; width:100%; height:60px;background-position: center;"></div>');
  712. }
  713. var fillSelected = function (selected) {
  714. $(selectField).html('');
  715. $(choices).html('');
  716. selected.forEach(function (choice, index) {
  717. if (isMulti || (!isMulti && index == 0)) {
  718. $(selectField).append('<option selected value="' + choice.id + '">' + choice.display + '</option>');
  719. $(choices).append('<button style="margin:2px;" rid="' + choice.id + '" index="' + index + '">' + choice.display + ' x</button>');
  720. }
  721. });
  722. $(choices).find('button').on('click', function () {
  723. var btn = this;
  724. var rid = $(btn).attr('rid');
  725. var index = $(btn).attr('index');
  726. selections.splice(index, 1);
  727. fillSelected(selected);
  728. return false;
  729. });
  730. }
  731. fillSelected(selections);
  732. var setValue = function (val, display) {
  733. if (!isMulti) {
  734. selections = [];
  735. }
  736. // get rid of earliers
  737. for (var i = 0; i < selections.length; i++) {
  738. var sel = selections[i];
  739. if (sel.id == parseInt(val)) {
  740. selections.splice(i, 1);
  741. }
  742. }
  743. selections.push({
  744. id: val,
  745. display: display
  746. });
  747. fillSelected(selections);
  748. $(textField).val('');
  749. $(textField).hide();
  750. }
  751. var getMatches = function () {
  752. var substring = $(textField).val();
  753. if (charMin > substring.length) {
  754. return;
  755. }
  756. setResultMask();
  757. $(resultDiv).show();
  758. //url, data, pre, post, onSuccess, onFailure, suppressErrorMessage, onHttpFailure, shouldHideMask
  759. doAjax(url, {
  760. substring: substring
  761. }, null, null, function (matches) {
  762. $(resultDiv).find('[rid]').each(function () {
  763. $(this).off()
  764. });
  765. $(resultDiv).html('');
  766. matches.forEach(function (match) {
  767. if (typeof match == 'string') {
  768. match = {
  769. id: match,
  770. display: match
  771. }
  772. }
  773. if (typeof match == 'object' && !match.id) {
  774. match.id = match.display;
  775. }
  776. $(resultDiv).append('<a href="#" class="searcher-result" display="' + match.display + '" rid="' + match.id + '">' + match.display + '</a>');
  777. });
  778. $(resultDiv).find('[rid]').each(function () {
  779. var result = this;
  780. $(result).mousedown('click', function () {
  781. var val = $(result).attr('rid');
  782. var display = $(result).attr('display');
  783. setValue(val, display);
  784. //$(textField).hide();
  785. return false;
  786. });
  787. });
  788. }, null, true, null, true);
  789. };
  790. $(textField).on('keyup', function () {
  791. getMatches();
  792. });
  793. $(textField).on('blur', function () {
  794. $(textField).val('');
  795. $(textField).hide();
  796. if ($(resultDiv).is(':visible')) {
  797. $(resultDiv).hide();
  798. };
  799. });
  800. $(textField).on('focus', function () {
  801. getMatches();
  802. });
  803. $(me).on('click', function () {
  804. $(textField).show();
  805. $(textField).focus();
  806. });
  807. $(me).show();
  808. });
  809. });
  810. $(document).ready(function () {
  811. // setInterval(function () {
  812. // doAjax('/api/session/test', null, null, null, null, function () {
  813. // window.location.reload(true);
  814. // }, true, null, true);
  815. // }, 10000);
  816. });
  817. $(document).ready(function () {
  818. if (focusOn) {
  819. $('#' + focusOn).focus();
  820. }
  821. });
  822. $(function () {
  823. $('[setMaskOnClick]').click(function () {
  824. showMask();
  825. });
  826. });
  827. $(function () {
  828. $('input[type=file][ajaxload]').each(function () {
  829. var me = this;
  830. var name = $(me).attr('ajaxload');
  831. $(me).wrap('<span class="ajaxload"></span>');
  832. $(me).closest('span').append('<input type="hidden" name="' + name + '"/>');
  833. $(me).on('change', function (event) {
  834. console.log("file received.");
  835. var fileField = me;
  836. var files = event.target.files;
  837. var data = new FormData();
  838. $.each(files, function (key, value) {
  839. data.append(key, value);
  840. });
  841. $.ajax({
  842. url: '/api/systemFile/upload',
  843. type: 'POST',
  844. data: data,
  845. cache: false,
  846. dataType: 'json',
  847. processData: false, // Don't process the files
  848. contentType: false, // Set content type to false as jQuery will tell the server its a query string request
  849. success: function (data, textStatus, jqXHR) {
  850. var systemFileID = data.data;
  851. console.log("UPLOAD WORKED::", data);
  852. $(me).closest('span').find('input[type=hidden]').val(systemFileID);
  853. },
  854. error: function (jqXHR, textStatus, errorThrown) {
  855. console.log('ERRORS: ' + textStatus);
  856. }
  857. });
  858. });
  859. });
  860. });