yemi.js 30 KB

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