yemi.js 29 KB

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