yemi.js 29 KB

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