yemi.js 29 KB

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