yemi.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. setTimeout(function() {
  273. $(realForm).find('input:not([type="hidden"]), textarea, select').first().select().focus();
  274. }, 0);
  275. }
  276. }
  277. }
  278. if (!infoToggle && info && $(info).attr('show') == null) {
  279. if ($(info).attr('liner') != null) {
  280. $(info).css('display', 'inline');
  281. infoToggle = true;
  282. } else {
  283. $(info).toggle(100);
  284. }
  285. }
  286. if ($(start).attr('show') == null) {
  287. $(start).toggle(100);
  288. }
  289. var focusOnStart = $(realForm).find('[focusOnStart]');
  290. if (focusOnStart) {
  291. $(focusOnStart).focus();
  292. }
  293. return false;
  294. });
  295. $(start).attr('href', '#');
  296. }
  297. if (cancel) {
  298. $(cancel).click(function (e) {
  299. e.preventDefault();
  300. e.stopImmediatePropagation();
  301. if ($(realForm).attr('show') == null) {
  302. $(realForm).hide(100);
  303. }
  304. if (info && $(info).attr('show') == null) {
  305. $(info).show(100);
  306. }
  307. if (start && $(start).attr('show') == null) {
  308. $(start).show(100);
  309. }
  310. hideMoeFormMask();
  311. });
  312. }
  313. $(submit).click(function (e) {
  314. e.preventDefault();
  315. e.stopImmediatePropagation();
  316. if (moe.isProcessing) {
  317. return false;
  318. }
  319. if ($(submit).attr('confirm')) {
  320. var question = $(submit).attr('confirm');
  321. var cont = confirm(question);
  322. if (cont) {} else {
  323. console.log("ABORTED!");
  324. return;
  325. }
  326. }
  327. moe.isProcessing = true;
  328. var data = {};
  329. var formData = $(form).serializeArray();
  330. formData.forEach(function (field) {
  331. if (data[field.name]) {
  332. if (data[field.name] instanceof Array) {
  333. data[field.name].push(field.value);
  334. } else {
  335. var arr = [];
  336. arr.push(data[field.name]);
  337. arr.push(field.value);
  338. data[field.name] = arr;
  339. }
  340. } else {
  341. data[field.name] = field.value;
  342. }
  343. });
  344. console.log(data);
  345. //var doAjax = function (url, data, pre, post, onSuccess, onFailure, suppressErrorMessage, onHttpFailure, shouldHideMask, immediatelyHideMaskOnReply)
  346. doAjax(url, data, null, function () {
  347. moe.isProcessing = false;
  348. }, function (data) {
  349. if (justLog) { // this is to test!
  350. console.log('RETURNED', data);
  351. } else if (redir) {
  352. if (redir == "back") {
  353. window.history.back();
  354. } else {
  355. var host = window.location.host;
  356. if (redir.indexOf('[data]') > -1) {
  357. redir = redir.replace('[data]', data)
  358. window.location.href = '/' + redir;
  359. } else {
  360. window.location.href = '/' + redir;
  361. }
  362. }
  363. } else {
  364. pageReload();
  365. }
  366. }, function (errorMessage) {
  367. }, false);
  368. });
  369. });
  370. $('table[info] input').each(function () {
  371. $(this).prop('readonly', true);
  372. });
  373. //...for checkboxes readonly
  374. $('input[type=checkbox][readonly],input[type=radio][readonly]').each(function () {
  375. var x = this;
  376. var isChecked = $(x).attr('checked');
  377. $(x).change(function () {
  378. $(x).attr('checked', isChecked);
  379. });
  380. });
  381. $('[show-if]').each(function () { //TODO show-if="isOpen" show-if="isTimeSpecific" show-if="refillFrequency=MONTH"
  382. var x = this;
  383. var sel = $(x).attr('show-if');
  384. var selParts = sel.split('=');
  385. var selName = selParts[0];
  386. var selValue = selParts[1];
  387. var useOpposite = selName[0] == '!';
  388. if (useOpposite) {
  389. selName = selName.slice(1);
  390. }
  391. var form = $(x).closest('form');
  392. var conditionFields = $(form).find('[name=' + selName + ']');
  393. function hideX() {
  394. $(x).hide();
  395. }
  396. function showX() {
  397. $(x).show();
  398. }
  399. var go = function () {
  400. if (selValue) {
  401. var value = $(conditionFields).val();
  402. if (value == selValue) {
  403. if (useOpposite) {
  404. hideX()
  405. } else {
  406. showX()
  407. }
  408. } else {
  409. if (useOpposite) {
  410. showX()
  411. } else {
  412. hideX()
  413. }
  414. }
  415. } else {
  416. var isChecked = $(conditionFields).prop('checked');
  417. if (isChecked) {
  418. if (useOpposite) {
  419. hideX()
  420. } else {
  421. showX()
  422. }
  423. } else {
  424. if (useOpposite) {
  425. showX()
  426. } else {
  427. hideX()
  428. }
  429. }
  430. }
  431. };
  432. go();
  433. $(conditionFields).change(function () {
  434. go();
  435. });
  436. });
  437. });
  438. //now goTo is a plugin...
  439. (function ($) {
  440. $.fn.goTo = function (x) {
  441. $('html, body').animate({
  442. scrollTop: ($(this).offset().top - x) + 'px'
  443. }, 1);
  444. return this; // for chaining...
  445. };
  446. })(jQuery);
  447. $(document).ready(function () {
  448. $(".expander").on("click", function () {
  449. var expandedID = $(this).attr("id");
  450. if ($(this).text() == "-") {
  451. $(this).text("+");
  452. } else {
  453. $(this).text("-");
  454. }
  455. $("." + expandedID).toggle();
  456. return false;
  457. });
  458. });
  459. $(document).ready(function () {
  460. $('[showMap]').each(function () {
  461. var mapper = $(this);
  462. var adr = mapper.attr('showMap');
  463. mapper.on('click', function () {
  464. showAddr(adr);
  465. return false;
  466. });
  467. });
  468. });
  469. $(document).ready(function () {
  470. $('[dateRanger]').each(function () {
  471. var dr = $(this);
  472. var rangeTypeSelect = dr.find('select')[0];
  473. var date1Input = dr.find('[date1]')[0];
  474. var date2Input = dr.find('[date2]')[0];
  475. var d1Val = '';
  476. var d2Val = '';
  477. var d1 = function (enable) {
  478. if (enable) {
  479. var hasVal = $(date1Input).val();
  480. if (!hasVal) {
  481. $(date1Input).val(d1Val);
  482. }
  483. $(date1Input).show();
  484. } else {
  485. d1Val = $(date1Input).val();
  486. $(date1Input).val('');
  487. $(date1Input).hide();
  488. }
  489. };
  490. var d2 = function (enable) {
  491. if (enable) {
  492. var hasVal = $(date2Input).val();
  493. if (!hasVal) {
  494. $(date2Input).val(d2Val);
  495. }
  496. $(date2Input).show();
  497. } else {
  498. d2Val = $(date2Input).val();
  499. $(date2Input).val('');
  500. $(date2Input).hide();
  501. }
  502. };
  503. var adjustFields = function () {
  504. var rangeType = $(rangeTypeSelect).val();
  505. if (rangeType == 'all') {
  506. d1();
  507. d2();
  508. } else if (rangeType == 'on-or-before') {
  509. d1(true);
  510. d2();
  511. } else if (rangeType == 'on-or-after') {
  512. d1(true);
  513. d2();
  514. } else if (rangeType == 'between') {
  515. d1(true);
  516. d2(true);
  517. } else if (rangeType == 'on') {
  518. d1(true);
  519. d2();
  520. } else if (rangeType == 'not-on') {
  521. d1(true);
  522. d2();
  523. } else if (rangeType == 'not-in-between') {
  524. d1(true);
  525. d2(true);
  526. }
  527. };
  528. adjustFields();
  529. $(rangeTypeSelect).change(function () {
  530. adjustFields();
  531. });
  532. });
  533. $('[numRanger]').each(function () {
  534. var nr = $(this);
  535. var rangeTypeSelect = nr.find('select')[0];
  536. var num1Input = nr.find('[num1]')[0];
  537. var num2Input = nr.find('[num2]')[0];
  538. var n1 = function (enable) {
  539. if (enable) {
  540. $(num1Input).show();
  541. } else {
  542. $(num1Input).hide();
  543. }
  544. };
  545. var n2 = function (enable) {
  546. if (enable) {
  547. $(num2Input).show();
  548. } else {
  549. $(num2Input).hide();
  550. }
  551. };
  552. var adjustFields = function () {
  553. var rangeType = $(rangeTypeSelect).val();
  554. if (rangeType == 'all') {
  555. n1();
  556. n2();
  557. } else if (rangeType == 'less-than') {
  558. n1(true);
  559. n2();
  560. } else if (rangeType == 'greater-than') {
  561. n1(true);
  562. n2();
  563. } else if (rangeType == 'equal-to') {
  564. n1(true);
  565. n2();
  566. } else if (rangeType == 'between') {
  567. n1(true);
  568. n2(true);
  569. } else if (rangeType == 'not-equal-to') {
  570. n1(true);
  571. n2();
  572. } else if (rangeType == 'not-in-between') {
  573. n1(true);
  574. n2(true);
  575. }
  576. };
  577. adjustFields('all');
  578. $(rangeTypeSelect).change(function () {
  579. adjustFields();
  580. });
  581. });
  582. });
  583. $(document).ready(function () {
  584. $('[minzero]').on('change', function () {
  585. var val = $(this).val();
  586. val = parseFloat(val);
  587. if (val < 0) {
  588. alert('This number cannot be less than zero.');
  589. $(this).val('');
  590. $(this).focus();
  591. }
  592. });
  593. });
  594. var showAddr = function (adr) {
  595. window.open('http://192.241.155.210/geo.php?adr=' + adr, new Date().getTime(), "height=400,width=520");
  596. };
  597. $(document).ready(function () {
  598. var globalSearch = function () {
  599. var substring = $('#globalSearch').val();
  600. console.log("SUBSTRING", substring);
  601. if (substring.length > 2) {
  602. $("#results").show();
  603. $("#results").load("/global-search?substring=" + encodeURIComponent(substring));
  604. } else {
  605. $("#results").hide();
  606. }
  607. };
  608. $("#globalSearch").on('keyup', function (evnt) {
  609. globalSearch();
  610. });
  611. $("#globalSearch").on('focus', function (evnt) {
  612. globalSearch();
  613. });
  614. $("#globalSearch").on('blur', function (evt) {
  615. setTimeout(function () {
  616. $("#results").hide();
  617. }, 500);
  618. });
  619. });
  620. $('a[aller]').attr('href', '#');
  621. var selectAll = true;
  622. $('a[aller]').click(function () {
  623. $('input[type=checkbox][aller]').each(function () {
  624. if (!$(this).is(':disabled')) {
  625. $(this).prop('checked', selectAll);
  626. }
  627. });
  628. selectAll = !selectAll;
  629. return false;
  630. });
  631. $(function () {
  632. $('.showOnLoad').show();
  633. });
  634. $(function () {
  635. var i = 0;
  636. function pulsate() {
  637. $(".urgentIndicator").
  638. animate({
  639. opacity: 0.2
  640. }, 200, 'linear').
  641. animate({
  642. opacity: 1
  643. }, 200, 'linear', pulsate);
  644. }
  645. pulsate();
  646. });
  647. $(function () {
  648. $('[remote-searcher]').each(function () {
  649. var me = this;
  650. $(me).hide();
  651. var selections = [];
  652. var isMulti = typeof $(me).attr('multiple') == 'string';
  653. $(me).find('[rid]').each(function () {
  654. selections.push({
  655. id: $(this).attr('rid'),
  656. display: $(this).attr('display')
  657. });
  658. });
  659. if (!isMulti && selections[0]) {
  660. selections = [selections[0]];
  661. }
  662. $(me).html('');
  663. var url = $(me).attr('remote-searcher');
  664. var charMin = $(me).attr('char-min');
  665. var name = $(me).attr('name');
  666. $(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>');
  667. $(me).append('<div style="margin-top:2px;background:white;border:1px lightgray solid;display:none;width:99%;position:absolute;z-index:999"></div>');
  668. var choices = $(me).find('span[choices]');
  669. var selectField = $(me).find('select');
  670. var textField = $(me).find('input[type=text]');
  671. var resultDiv = $(me).find('div');
  672. var setResultMask = function () {
  673. $(resultDiv).html('<div style="background-image: url(/icons/vanillaspin.gif); background-repeat: no-repeat; width:100%; height:60px;background-position: center;"></div>');
  674. }
  675. var fillSelected = function (selected) {
  676. $(selectField).html('');
  677. $(choices).html('');
  678. selected.forEach(function (choice, index) {
  679. if (isMulti || (!isMulti && index == 0)) {
  680. $(selectField).append('<option selected value="' + choice.id + '">' + choice.display + '</option>');
  681. $(choices).append('<button style="margin:2px;" rid="' + choice.id + '" index="' + index + '">' + choice.display + ' x</button>');
  682. }
  683. });
  684. $(choices).find('button').on('click', function () {
  685. var btn = this;
  686. var rid = $(btn).attr('rid');
  687. var index = $(btn).attr('index');
  688. selections.splice(index, 1);
  689. fillSelected(selected);
  690. return false;
  691. });
  692. }
  693. fillSelected(selections);
  694. var setValue = function (val, display) {
  695. if (!isMulti) {
  696. selections = [];
  697. }
  698. // get rid of earliers
  699. for (var i = 0; i < selections.length; i++) {
  700. var sel = selections[i];
  701. if (sel.id == parseInt(val)) {
  702. selections.splice(i, 1);
  703. }
  704. }
  705. selections.push({
  706. id: val,
  707. display: display
  708. });
  709. fillSelected(selections);
  710. $(textField).val('');
  711. $(textField).hide();
  712. }
  713. var getMatches = function () {
  714. var substring = $(textField).val();
  715. if (charMin > substring.length) {
  716. return;
  717. }
  718. setResultMask();
  719. $(resultDiv).show();
  720. //url, data, pre, post, onSuccess, onFailure, suppressErrorMessage, onHttpFailure, shouldHideMask
  721. doAjax(url, {
  722. substring: substring
  723. }, null, null, function (matches) {
  724. $(resultDiv).find('[rid]').each(function () {
  725. $(this).off()
  726. });
  727. $(resultDiv).html('');
  728. matches.forEach(function (match) {
  729. if (typeof match == 'string') {
  730. match = {
  731. id: match,
  732. display: match
  733. }
  734. }
  735. if (typeof match == 'object' && !match.id) {
  736. match.id = match.display;
  737. }
  738. $(resultDiv).append('<a href="#" class="searcher-result" display="' + match.display + '" rid="' + match.id + '">' + match.display + '</a>');
  739. });
  740. $(resultDiv).find('[rid]').each(function () {
  741. var result = this;
  742. $(result).mousedown('click', function () {
  743. var val = $(result).attr('rid');
  744. var display = $(result).attr('display');
  745. setValue(val, display);
  746. //$(textField).hide();
  747. return false;
  748. });
  749. });
  750. }, null, true, null, true);
  751. };
  752. $(textField).on('keyup', function () {
  753. getMatches();
  754. });
  755. $(textField).on('blur', function () {
  756. $(textField).val('');
  757. $(textField).hide();
  758. if ($(resultDiv).is(':visible')) {
  759. $(resultDiv).hide();
  760. };
  761. });
  762. $(textField).on('focus', function () {
  763. getMatches();
  764. });
  765. $(me).on('click', function () {
  766. $(textField).show();
  767. $(textField).focus();
  768. });
  769. $(me).show();
  770. });
  771. });
  772. $(document).ready(function () {
  773. // setInterval(function () {
  774. // doAjax('/api/session/test', null, null, null, null, function () {
  775. // window.location.reload(true);
  776. // }, true, null, true);
  777. // }, 10000);
  778. });
  779. $(document).ready(function () {
  780. if (focusOn) {
  781. $('#' + focusOn).focus();
  782. }
  783. });
  784. $(function () {
  785. $('[setMaskOnClick]').click(function () {
  786. showMask();
  787. });
  788. });
  789. $(function () {
  790. $('input[type=file][ajaxload]').each(function () {
  791. var me = this;
  792. var name = $(me).attr('ajaxload');
  793. $(me).wrap('<span class="ajaxload"></span>');
  794. $(me).closest('span').append('<input type="hidden" name="' + name + '"/>');
  795. $(me).on('change', function (event) {
  796. console.log("file received.");
  797. var fileField = me;
  798. var files = event.target.files;
  799. var data = new FormData();
  800. $.each(files, function (key, value) {
  801. data.append(key, value);
  802. });
  803. $.ajax({
  804. url: '/api/systemFile/upload',
  805. type: 'POST',
  806. data: data,
  807. cache: false,
  808. dataType: 'json',
  809. processData: false, // Don't process the files
  810. contentType: false, // Set content type to false as jQuery will tell the server its a query string request
  811. success: function (data, textStatus, jqXHR) {
  812. var systemFileID = data.data;
  813. console.log("UPLOAD WORKED::", data);
  814. $(me).closest('span').find('input[type=hidden]').val(systemFileID);
  815. },
  816. error: function (jqXHR, textStatus, errorThrown) {
  817. console.log('ERRORS: ' + textStatus);
  818. }
  819. });
  820. });
  821. });
  822. });