yemi.js 30 KB

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