yemi.js 30 KB

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