yemi.js 29 KB

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