jquery.webForms.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup UnaCore UNA Core
  6. * @{
  7. */
  8. (function($) {
  9. $.fn.addWebForms = function(onLoad) {
  10. if ('undefined' === typeof(glJsLoadOnaddWebForms) || !glJsLoadOnaddWebForms.length) {
  11. $.fn.processWebForms.apply(this);
  12. if(typeof onLoad == 'function')
  13. onLoad();
  14. }
  15. else {
  16. var $this = this;
  17. bx_get_scripts(glJsLoadOnaddWebForms, function () {
  18. $.fn.processWebForms.apply($this);
  19. if(typeof onLoad == 'function')
  20. onLoad();
  21. });
  22. }
  23. return this;
  24. }
  25. $.fn.processWebForms = function() {
  26. // move to the first error
  27. var aErrors = $('.bx-form-warn:visible', this);
  28. if(aErrors.length > 0)
  29. $([document.documentElement, document.body]).animate({
  30. scrollTop: aErrors.first().closest('.bx-form-element-wrapper').offset().top - 50
  31. }, 200);
  32. // switchers
  33. $('.bx-switcher-cont', this).each(function() {
  34. var eSwitcher = $(this);
  35. var eInput = $(this).find('input');
  36. if (eSwitcher.hasClass('bx-form-switcher-processed'))
  37. return;
  38. eSwitcher.addClass('bx-form-switcher-processed');
  39. // the visible control is a button with role=switch: its aria-checked follows the on/off class
  40. var fSyncState = function() {
  41. eSwitcher.find('[role="switch"]').attr('aria-checked', eSwitcher.hasClass('on') ? 'true' : 'false');
  42. };
  43. fSyncState();
  44. eSwitcher.on('click', function() {
  45. var $this = $(this);
  46. if ($this.hasClass('on')) {
  47. $this.find('input').prop('checked', false).trigger('change');
  48. $this.removeClass('on').addClass('off');
  49. } else {
  50. $this.removeClass('off').addClass('on');
  51. $this.find('input').prop('checked', true).trigger('change');
  52. }
  53. fSyncState();
  54. return false;
  55. });
  56. eInput.on('enable', function () {
  57. if (!$(this).prop('checked'))
  58. $(this).prop('checked', true).trigger('change');
  59. if (!eSwitcher.hasClass('on'))
  60. eSwitcher.removeClass('off').addClass('on');
  61. fSyncState();
  62. });
  63. eInput.on('disable', function () {
  64. if ($(this).prop('checked'))
  65. $(this).prop('checked', false).trigger('change');
  66. if (!eSwitcher.hasClass('off'))
  67. eSwitcher.removeClass('on').addClass('off');
  68. fSyncState();
  69. });
  70. });
  71. // collapsable headers
  72. $('.bx-form-collapsable', this).each(function() {
  73. var eFormSection = $(this);
  74. if (eFormSection.hasClass('bx-form-js-processed'))
  75. return;
  76. eFormSection.addClass('bx-form-js-processed');
  77. var eToggle = $('.bx-form-section-title:first', eFormSection).find('a, button').first(); // the caption control, a button now (anchors in older templates)
  78. var fSyncExpanded = function() {
  79. eToggle.attr('aria-expanded', eFormSection.hasClass('bx-form-collapsed') ? 'false' : 'true');
  80. };
  81. fSyncExpanded();
  82. var fCallback = function() {
  83. if (eFormSection.hasClass('bx-form-collapsed')) {
  84. // show
  85. eFormSection.removeClass('bx-form-collapsed');
  86. if (eFormSection.hasClass('bx-form-section-hidden')) {
  87. $('.bx-form-section-content:first', eFormSection).hide();
  88. eFormSection.removeClass('bx-form-section-hidden');
  89. }
  90. $('.bx-form-section-content:first', eFormSection).slideDown(function () {
  91. eFormSection.removeClass('bx-form-section-hidden');
  92. $(this).addWebForms();
  93. $(this).find('.CodeMirror').each(function() {
  94. this.CodeMirror.refresh();
  95. });
  96. //--- fire custom event 'bx_show'
  97. eFormSection.trigger('bx_show');
  98. });
  99. } else {
  100. // hide
  101. eFormSection.addClass('bx-form-collapsed');
  102. $('.bx-form-section-content:first', eFormSection).slideUp(function () {
  103. eFormSection.addClass('bx-form-collapsed bx-form-section-hidden');
  104. //--- fire custom event 'bx_hide'
  105. eFormSection.trigger('bx_hide');
  106. });
  107. }
  108. };
  109. $('.bx-form-section-title:first', eFormSection).click(function() {
  110. fCallback();
  111. fSyncExpanded();
  112. });
  113. });
  114. $("select", this).each(function () {
  115. // Labels selector
  116. var oInput = $(this);
  117. if (oInput.hasClass('bx-form-select-labels')) {
  118. $(".bx-form-select-labels").select2ToTree();
  119. }
  120. if(oInput.hasClass('bx-form-input-rgb-list')) {
  121. oInput.bind('change', function() {
  122. oInput.removeClassWild('bg-*').addClass('bg-' + $(this).val());
  123. });
  124. oInput.removeClassWild('bg-*').addClass('bg-' + $(this).val());
  125. }
  126. });
  127. $(".bx-form-input-wrapper-password", this).each(function() {
  128. var sClassInited = "bx-inited";
  129. if($(this).hasClass(sClassInited))
  130. return;
  131. var sClassFocus = "bx-form-input-focus";
  132. $(this).find("input").bind("focus", function() {
  133. $(this).parents(".bx-form-input-wrapper-password:first").addClass(sClassFocus);
  134. }).bind("blur", function() {
  135. $(this).parents(".bx-form-input-wrapper-password:first").removeClass(sClassFocus);
  136. });
  137. $(this).find(".password-show, .password-hide").on("click", function () {
  138. var oLink = $(this);
  139. var oFld = oLink.parents(".bx-form-input-wrapper-password:first").find("input");
  140. if (oLink.hasClass('password-show')) {
  141. oLink.attr('title', _t('_sys_form_input_password_hide')).attr('aria-label', _t('_sys_form_input_password_hide'));
  142. oFld.attr("type", "text");
  143. }
  144. else {
  145. oLink.attr('title', _t('_sys_form_input_password_show')).attr('aria-label', _t('_sys_form_input_password_show'));
  146. oFld.attr("type", "password");
  147. }
  148. oLink.toggleClass('password-show password-hide');
  149. oLink.find('i').toggleClass('eye eye-slash');
  150. });
  151. $(this).addClass(sClassInited);
  152. });
  153. $("input", this).each(function() {
  154. var oInput = $(this);
  155. var onCreateRange = function (event, ui) {
  156. var eInput = $(this).parent().find('input');
  157. var iSliderWidth = eInput.innerWidth() - 160;
  158. $(this).css('width', iSliderWidth + 'px').css('top', (eInput.innerHeight() - $(this).outerHeight())/2 + 1 + 'px');
  159. eInput.css('padding-right', (iSliderWidth + parseFloat($(this).css('margin-right')) + 10) + 'px');
  160. };
  161. // DoubleRange
  162. if (this.getAttribute("type") == 'doublerange') {
  163. var cur = $(this);
  164. if(!cur.is(':visible') || cur.hasClass('bx-form-doublerange-processed'))
  165. return;
  166. cur.addClass('bx-form-doublerange-processed');
  167. var $slider = $('<div class="bx-def-margin-right"></div>').insertAfter(cur);
  168. var iMin = cur.attr("min") ? parseFloat(cur.attr("min"), 10) : 0;
  169. var iMax = cur.attr("max") ? parseFloat(cur.attr("max"), 10) : 100;
  170. var sRangeDv = cur.attr("range-divider") ? cur.attr("range-divider") : '-';
  171. var funcGetValues = function (e) {
  172. var values = e.val().split(sRangeDv, 2); // get values
  173. if (typeof(values[0]) != 'undefined' && values[0].length)
  174. values[0] = parseFloat(values[0]);
  175. else
  176. values[0] = iMin;
  177. if (typeof(values[1]) != 'undefined' && values[1].length)
  178. values[1] = parseFloat(values[1]);
  179. else
  180. values[1] = iMax;
  181. return values;
  182. };
  183. var onChange = function(e, ui) {
  184. values = ui.values;
  185. cur.val( values[0] + sRangeDv + values[1] );
  186. };
  187. $slider.slider({
  188. range: true,
  189. min: iMin,
  190. max: iMax,
  191. step: parseFloat(cur.attr("step")) ? parseFloat(cur.attr("step")) : 1,
  192. values: funcGetValues(cur),
  193. change: onChange,
  194. slide: onChange,
  195. create: onCreateRange
  196. });
  197. cur.bind('change', function () {
  198. var values = funcGetValues($(this));
  199. $slider.slider("option", "values", values);
  200. });
  201. }
  202. // Single range or slider
  203. else if (this.getAttribute("type") == 'slider') {
  204. var cur = $(this);
  205. if(!cur.is(':visible') || cur.hasClass('bx-form-range-processed'))
  206. return;
  207. cur.addClass('bx-form-range-processed');
  208. var $slider = $('<div class="bx-def-margin-right"></div>').insertAfter(cur)
  209. $slider.css('width', ($slider.parent().innerWidth() - cur.outerWidth() - 50) + 'px');
  210. var iMin = cur.attr("min") ? parseFloat(cur.attr("min"), 10) : 0;
  211. var iMax = cur.attr("max") ? parseFloat(cur.attr("max"), 10) : 100;
  212. var onChange = function(e, ui) {
  213. cur.val( ui.value );
  214. };
  215. $slider.slider({
  216. min: iMin,
  217. max: iMax,
  218. step: parseFloat(cur.attr("step")) ? parseFloat(cur.attr("step")) : 1,
  219. value: cur.val(),
  220. change: onChange,
  221. slide: onChange,
  222. create: onCreateRange
  223. });
  224. cur.bind('change', function () {
  225. $slider.slider("option", "value", $(this).val());
  226. });
  227. }
  228. // Date/Time pickers
  229. if (this.getAttribute("type") == "date" || this.getAttribute("type") == "date_calendar" || this.getAttribute("type") == "datetime" || this.getAttribute("type") == "date_time" || this.getAttribute("type") == "datepicker" || this.getAttribute("type") == "dateselect") {
  230. if ($(this).hasClass('bx-form-datepicker-processed'))
  231. return;
  232. $(this).addClass('bx-form-datepicker-processed');
  233. var sYearMin = '1900';
  234. var sYearMax = '2100';
  235. var m;
  236. if ($(this).attr('min') && (m = $(this).attr('min').match(/^(\d{4})/)))
  237. sYearMin = m[1];
  238. if ($(this).attr('max') && (m = $(this).attr('max').match(/^(\d{4})/)))
  239. sYearMax = m[1];
  240. var onBeforeShow = function(oInput, oInstance) {
  241. $(oInstance.dpDiv).addClass('bx-form-datepicker-modal');
  242. };
  243. var parseDateUtc = function (sDate, sFormat) {
  244. let oDate = moment.utc(sDate);
  245. return new Date(
  246. oDate.year(),
  247. oDate.month(),
  248. oDate.date(),
  249. oDate.hour(),
  250. oDate.minute(),
  251. oDate.second()
  252. );
  253. }
  254. var formatDateUtc = function (oDate, sFormat) {
  255. return moment.utc([
  256. oDate.getFullYear(),
  257. oDate.getMonth(),
  258. oDate.getDate(),
  259. oDate.getHours(),
  260. oDate.getMinutes(),
  261. oDate.getSeconds()
  262. ]).format(sFormat);
  263. }
  264. var bDatePicker = false;
  265. if (this.getAttribute("type") == "date" || this.getAttribute("type") == "date_calendar" || this.getAttribute("type") == "datepicker") { // Date picker
  266. flatpickr(this, {
  267. locale: sLang,
  268. disableMobile: true,
  269. altInput: true,
  270. altFormat: this.getAttribute("data-frmt-date") ? this.getAttribute("data-frmt-date") : "F j, Y",
  271. dateFormat: "YYYY-MM-DD",
  272. minDate: sYearMin,
  273. maxDate: sYearMax,
  274. onOpen: onBeforeShow,
  275. parseDate: parseDateUtc,
  276. formatDate: formatDateUtc,
  277. });
  278. bDatePicker = true;
  279. } else if(this.getAttribute("type") == "datetime" || this.getAttribute("type") == "date_time") { // DateTime picker
  280. var oPickerOptions = {
  281. locale: sLang,
  282. disableMobile: true,
  283. enableTime: true,
  284. altInput: true,
  285. altFormat: this.getAttribute("data-frmt-date") ? this.getAttribute("data-frmt-datetime") : "D MMM YYYY h:mma",
  286. dateFormat: "YYYY-MM-DD\\THH:mm:ssZ",
  287. time_24hr: null === this.getAttribute("data-frmt-24h") || 'on' == this.getAttribute("data-frmt-24h") ? true : false,
  288. minDate: sYearMin,
  289. maxDate: sYearMax,
  290. onOpen: onBeforeShow,
  291. parseDate: function (sDate, sFormat) {
  292. let oDate = moment.utc(sDate);
  293. return oDate.toDate();
  294. },
  295. formatDate: function (oDate, sFormat, locale) {
  296. let o;
  297. if (sFormat.endsWith('Z'))
  298. o = moment.utc(oDate);
  299. else
  300. o = moment(oDate);
  301. return o.format(sFormat);
  302. }
  303. };
  304. if (1 == $(this).attr('data-utc')) {
  305. oPickerOptions.parseDate = parseDateUtc;
  306. oPickerOptions.formatDate = formatDateUtc;
  307. }
  308. flatpickr(this, oPickerOptions);
  309. bDatePicker = true;
  310. } else if(this.getAttribute("type") == "dateselect") { // DateTime selector
  311. moment.locale(sLang);
  312. $(this).combodate({
  313. minYear: sYearMin,
  314. maxYear: sYearMax,
  315. format: 'YYYY-MM-DD',
  316. template: 'DD MMMM YYYY',
  317. firstItem: 'none',
  318. 'smartDays': true,
  319. customClass: 'bx-def-font-inputs bx-form-input-select block w-full py-2 px-3 border border-gray-300 dark:border-gray-700 rounded-md shadow-sm bg-white dark:bg-gray-900 placeholder-gray-500 focus:outline-none focus:placeholder-gray-400 focus:ring-2 focus:text-gray-900 dark:focus:text-gray-100 focus:ring-blue-500 focus:border-opacity-70 focus:ring-opacity-20 focus:border-blue-500 text-sm text-gray-700 dark:text-gray-300 appearance-none',
  320. });
  321. }
  322. if(bDatePicker) {
  323. // flatpickr's altInput is the field the user actually sees and focuses, but it is a bare clone:
  324. // it copies class/placeholder/disabled/required/tabindex and no accessible name, so carry the
  325. // labelling of the original (now hidden) input over to it.
  326. var oAltInput = this._flatpickr ? this._flatpickr.altInput : null;
  327. if (oAltInput) {
  328. var aLabelAttrs = ['aria-label', 'aria-labelledby', 'aria-describedby', 'title'];
  329. for (var iLabelAttr = 0; iLabelAttr < aLabelAttrs.length; iLabelAttr++) {
  330. var sLabelValue = this.getAttribute(aLabelAttrs[iLabelAttr]);
  331. if (sLabelValue)
  332. oAltInput.setAttribute(aLabelAttrs[iLabelAttr], sLabelValue);
  333. }
  334. }
  335. if (typeof window.glOnColorSchemeChange === 'undefined')
  336. window.glOnColorSchemeChange = [];
  337. window.glOnColorSchemeChange.push(function(iCode) {
  338. var sNameL = 'flatpickr.min.css';
  339. var sNameD = 'themes/dark.css';
  340. var oStylesheet = null;
  341. switch(iCode) {
  342. case 1: //--- light
  343. oStylesheet = document.head.querySelector("link[href*='" + sNameD + "']");
  344. if(oStylesheet)
  345. oStylesheet.href = oStylesheet.href.replace(sNameD, sNameL);
  346. else
  347. bx_get_style(sUrlRoot + 'plugins_public/flatpickr/dist/' + sNameL);
  348. break;
  349. case 2: //--- dark
  350. oStylesheet = document.head.querySelector("link[href*='" + sNameL + "']");
  351. if(oStylesheet)
  352. oStylesheet.href = oStylesheet.href.replace(sNameL, sNameD);
  353. else
  354. bx_get_style(sUrlRoot + 'plugins_public/flatpickr/dist/' + sNameD);
  355. break;
  356. }
  357. });
  358. }
  359. if (window.navigator.appVersion.search(/Chrome\/(.*?) /) == -1 || parseInt(window.navigator.appVersion.match(/Chrome\/(\d+)\./)[1], 10) < 24)
  360. if( this.getAttribute("allow_input") == null)
  361. $(this).attr('readonly', 'readonly');
  362. }
  363. // RGB/RGBA pickers
  364. if(this.getAttribute('type') == 'text' && (oInput.hasClass('bx-form-input-rgb') || oInput.hasClass('bx-form-input-rgba')) && !oInput.hasClass('minicolors-input')) {
  365. oInput.attr('autocomplete', 'off');
  366. oInput.minicolors({
  367. control: oInput.attr('data-control') || 'hue',
  368. format: oInput.attr('data-format') || 'rgb',
  369. letterCase: oInput.attr('data-letterCase') || 'lowercase',
  370. opacity: oInput.hasClass('bx-form-input-rgba'),
  371. position: oInput.attr('data-position') || 'bottom left',
  372. theme: 'bootstrap'
  373. });
  374. }
  375. });
  376. $('form', this).addClass('bx-form-processed');
  377. return this;
  378. };
  379. })(jQuery);
  380. $(document).ready(function() {
  381. $(this).addWebForms();
  382. });
  383. (function($) {
  384. $.fn.removeClassWild = function(mask) {
  385. return this.removeClass(function(index, cls) {
  386. var re = mask.replace(/\*/g, '\\S+');
  387. return (cls.match(new RegExp('\\b' + re + '', 'g')) || []).join(' ');
  388. });
  389. };
  390. })(jQuery);
  391. /** @} */