yemi.js 30 KB

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