yemi.js 32 KB

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