yemi.js 31 KB

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