yemi.js 29 KB

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