yemi.js 29 KB

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