jquery.webForms.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. eSwitcher.on('click', function() {
  40. var $this = $(this);
  41. if ($this.hasClass('on')) {
  42. $this.find('input').prop('checked', false).trigger('change');
  43. $this.removeClass('on').addClass('off');
  44. } else {
  45. $this.removeClass('off').addClass('on');
  46. $this.find('input').prop('checked', true).trigger('change');
  47. }
  48. return false;
  49. });
  50. eInput.on('enable', function () {
  51. if (!$(this).prop('checked'))
  52. $(this).prop('checked', true).trigger('change');
  53. if (!eSwitcher.hasClass('on'))
  54. eSwitcher.removeClass('off').addClass('on');
  55. });
  56. eInput.on('disable', function () {
  57. if ($(this).prop('checked'))
  58. $(this).prop('checked', false).trigger('change');
  59. if (!eSwitcher.hasClass('off'))
  60. eSwitcher.removeClass('on').addClass('off');
  61. });
  62. });
  63. // collapsable headers
  64. $('.bx-form-collapsable', this).each(function() {
  65. var eFormSection = $(this);
  66. if (eFormSection.hasClass('bx-form-js-processed'))
  67. return;
  68. eFormSection.addClass('bx-form-js-processed');
  69. var fCallback = function() {
  70. if (eFormSection.hasClass('bx-form-collapsed')) {
  71. // show
  72. eFormSection.removeClass('bx-form-collapsed');
  73. if (eFormSection.hasClass('bx-form-section-hidden')) {
  74. $('.bx-form-section-content:first', eFormSection).hide();
  75. eFormSection.removeClass('bx-form-section-hidden');
  76. }
  77. $('.bx-form-section-content:first', eFormSection).slideDown(function () {
  78. eFormSection.removeClass('bx-form-section-hidden');
  79. $(this).addWebForms();
  80. //--- fire custom event 'bx_show'
  81. eFormSection.trigger('bx_show');
  82. });
  83. } else {
  84. // hide
  85. eFormSection.addClass('bx-form-collapsed');
  86. $('.bx-form-section-content:first', eFormSection).slideUp(function () {
  87. eFormSection.addClass('bx-form-collapsed bx-form-section-hidden');
  88. //--- fire custom event 'bx_hide'
  89. eFormSection.trigger('bx_hide');
  90. });
  91. }
  92. };
  93. $('.bx-form-section-title:first', eFormSection).click(fCallback);
  94. });
  95. $("select", this).each(function () {
  96. // Labels selector
  97. var oInput = $(this);
  98. if (oInput.hasClass('bx-form-select-labels')) {
  99. $(".bx-form-select-labels").select2ToTree();
  100. }
  101. if(oInput.hasClass('bx-form-input-rgb-list')) {
  102. oInput.bind('change', function() {
  103. oInput.removeClassWild('bg-*').addClass('bg-' + $(this).val());
  104. });
  105. oInput.removeClassWild('bg-*').addClass('bg-' + $(this).val());
  106. }
  107. });
  108. $(".bx-form-input-wrapper-password", this).each(function() {
  109. var sClassInited = "bx-inited";
  110. if($(this).hasClass(sClassInited))
  111. return;
  112. var sClassFocus = "bx-form-input-focus";
  113. $(this).find("input").bind("focus", function() {
  114. $(this).parents(".bx-form-input-wrapper-password:first").addClass(sClassFocus);
  115. }).bind("blur", function() {
  116. $(this).parents(".bx-form-input-wrapper-password:first").removeClass(sClassFocus);
  117. });
  118. $(this).find("a").on("click", function () {
  119. var oIcon = $(this).find("i");
  120. var oLink = oIcon.parents("a:first");
  121. var oFld = $(this).parents(".bx-form-input-wrapper-password:first").find("input");
  122. if (oIcon.hasClass("eye")) {
  123. oIcon.removeClass("eye").addClass("eye-slash");
  124. oLink.attr('title', _t('_sys_form_input_password_hide'));
  125. oFld.attr("type", "text");
  126. }
  127. else {
  128. oIcon.addClass("eye").removeClass("eye-slash");
  129. oLink.attr('title', _t('_sys_form_input_password_show'));
  130. oFld.attr("type", "password");
  131. }
  132. });
  133. $(this).addClass(sClassInited);
  134. });
  135. $("input", this).each(function() {
  136. var oInput = $(this);
  137. var onCreateRange = function (event, ui) {
  138. var eInput = $(this).parent().find('input');
  139. var iSliderWidth = eInput.innerWidth() - 160;
  140. $(this).css('width', iSliderWidth + 'px').css('top', (eInput.innerHeight() - $(this).outerHeight())/2 + 1 + 'px');
  141. eInput.css('padding-right', (iSliderWidth + parseFloat($(this).css('margin-right')) + 10) + 'px');
  142. };
  143. // DoubleRange
  144. if (this.getAttribute("type") == 'doublerange') {
  145. var cur = $(this);
  146. if(!cur.is(':visible') || cur.hasClass('bx-form-doublerange-processed'))
  147. return;
  148. cur.addClass('bx-form-doublerange-processed');
  149. var $slider = $('<div class="bx-def-margin-right"></div>').insertAfter(cur);
  150. var iMin = cur.attr("min") ? parseFloat(cur.attr("min"), 10) : 0;
  151. var iMax = cur.attr("max") ? parseFloat(cur.attr("max"), 10) : 100;
  152. var sRangeDv = cur.attr("range-divider") ? cur.attr("range-divider") : '-';
  153. var funcGetValues = function (e) {
  154. var values = e.val().split(sRangeDv, 2); // get values
  155. if (typeof(values[0]) != 'undefined' && values[0].length)
  156. values[0] = parseFloat(values[0]);
  157. else
  158. values[0] = iMin;
  159. if (typeof(values[1]) != 'undefined' && values[1].length)
  160. values[1] = parseFloat(values[1]);
  161. else
  162. values[1] = iMax;
  163. return values;
  164. };
  165. var onChange = function(e, ui) {
  166. values = ui.values;
  167. cur.val( values[0] + sRangeDv + values[1] );
  168. };
  169. $slider.slider({
  170. range: true,
  171. min: iMin,
  172. max: iMax,
  173. step: parseFloat(cur.attr("step")) ? parseFloat(cur.attr("step")) : 1,
  174. values: funcGetValues(cur),
  175. change: onChange,
  176. slide: onChange,
  177. create: onCreateRange
  178. });
  179. cur.bind('change', function () {
  180. var values = funcGetValues($(this));
  181. $slider.slider("option", "values", values);
  182. });
  183. }
  184. // Single range or slider
  185. else if (this.getAttribute("type") == 'slider') {
  186. var cur = $(this);
  187. if(!cur.is(':visible') || cur.hasClass('bx-form-range-processed'))
  188. return;
  189. cur.addClass('bx-form-range-processed');
  190. var $slider = $('<div class="bx-def-margin-right"></div>').insertAfter(cur)
  191. $slider.css('width', ($slider.parent().innerWidth() - cur.outerWidth() - 50) + 'px');
  192. var iMin = cur.attr("min") ? parseFloat(cur.attr("min"), 10) : 0;
  193. var iMax = cur.attr("max") ? parseFloat(cur.attr("max"), 10) : 100;
  194. var onChange = function(e, ui) {
  195. cur.val( ui.value );
  196. };
  197. $slider.slider({
  198. min: iMin,
  199. max: iMax,
  200. step: parseFloat(cur.attr("step")) ? parseFloat(cur.attr("step")) : 1,
  201. value: cur.val(),
  202. change: onChange,
  203. slide: onChange,
  204. create: onCreateRange
  205. });
  206. cur.bind('change', function () {
  207. $slider.slider("option", "value", $(this).val());
  208. });
  209. }
  210. // Date/Time pickers
  211. 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") {
  212. if ($(this).hasClass('bx-form-datepicker-processed'))
  213. return;
  214. $(this).addClass('bx-form-datepicker-processed');
  215. var sYearMin = '1900';
  216. var sYearMax = '2100';
  217. var m;
  218. if ($(this).attr('min') && (m = $(this).attr('min').match(/^(\d{4})/)))
  219. sYearMin = m[1];
  220. if ($(this).attr('max') && (m = $(this).attr('max').match(/^(\d{4})/)))
  221. sYearMax = m[1];
  222. var onBeforeShow = function(oInput, oInstance) {
  223. $(oInstance.dpDiv).addClass('bx-form-datepicker-modal');
  224. };
  225. var parseDateUtc = function (sDate, sFormat) {
  226. let oDate = moment.utc(sDate);
  227. return new Date(
  228. oDate.year(),
  229. oDate.month(),
  230. oDate.date(),
  231. oDate.hour(),
  232. oDate.minute(),
  233. oDate.second()
  234. );
  235. }
  236. var formatDateUtc = function (oDate, sFormat) {
  237. return moment.utc([
  238. oDate.getFullYear(),
  239. oDate.getMonth(),
  240. oDate.getDate(),
  241. oDate.getHours(),
  242. oDate.getMinutes(),
  243. oDate.getSeconds()
  244. ]).format(sFormat);
  245. }
  246. var bDatePicker = false;
  247. if (this.getAttribute("type") == "date" || this.getAttribute("type") == "date_calendar" || this.getAttribute("type") == "datepicker") { // Date picker
  248. flatpickr(this, {
  249. locale: sLang,
  250. disableMobile: true,
  251. altInput: true,
  252. altFormat: this.getAttribute("data-frmt-date") ? this.getAttribute("data-frmt-date") : "F j, Y",
  253. dateFormat: "YYYY-MM-DD",
  254. minDate: sYearMin,
  255. maxDate: sYearMax,
  256. onOpen: onBeforeShow,
  257. parseDate: parseDateUtc,
  258. formatDate: formatDateUtc,
  259. });
  260. bDatePicker = true;
  261. } else if(this.getAttribute("type") == "datetime" || this.getAttribute("type") == "date_time") { // DateTime picker
  262. var oPickerOptions = {
  263. locale: sLang,
  264. disableMobile: true,
  265. enableTime: true,
  266. altInput: true,
  267. altFormat: this.getAttribute("data-frmt-date") ? this.getAttribute("data-frmt-datetime") : "D MMM YYYY h:mma",
  268. dateFormat: "YYYY-MM-DD\\THH:mm:ssZ",
  269. time_24hr: null === this.getAttribute("data-frmt-24h") || 'on' == this.getAttribute("data-frmt-24h") ? true : false,
  270. minDate: sYearMin,
  271. maxDate: sYearMax,
  272. onOpen: onBeforeShow,
  273. parseDate: function (sDate, sFormat) {
  274. let oDate = moment.utc(sDate);
  275. return oDate.toDate();
  276. },
  277. formatDate: function (oDate, sFormat, locale) {
  278. let o;
  279. if (sFormat.endsWith('Z'))
  280. o = moment.utc(oDate);
  281. else
  282. o = moment(oDate);
  283. return o.format(sFormat);
  284. }
  285. };
  286. if (1 == $(this).attr('data-utc')) {
  287. oPickerOptions.parseDate = parseDateUtc;
  288. oPickerOptions.formatDate = formatDateUtc;
  289. }
  290. flatpickr(this, oPickerOptions);
  291. bDatePicker = true;
  292. } else if(this.getAttribute("type") == "dateselect") { // DateTime selector
  293. moment.locale(sLang);
  294. $(this).combodate({
  295. minYear: sYearMin,
  296. maxYear: sYearMax,
  297. format: 'YYYY-MM-DD',
  298. template: 'DD MMMM YYYY',
  299. firstItem: 'none',
  300. 'smartDays': true,
  301. 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',
  302. });
  303. }
  304. if(bDatePicker) {
  305. if (typeof window.glOnColorSchemeChange === 'undefined')
  306. window.glOnColorSchemeChange = [];
  307. window.glOnColorSchemeChange.push(function(iCode) {
  308. var sNameL = 'flatpickr.min.css';
  309. var sNameD = 'themes/dark.css';
  310. var oStylesheet = null;
  311. switch(iCode) {
  312. case 1: //--- light
  313. oStylesheet = document.head.querySelector("link[href*='" + sNameD + "']");
  314. if(oStylesheet)
  315. oStylesheet.href = oStylesheet.href.replace(sNameD, sNameL);
  316. else
  317. bx_get_style(sUrlRoot + 'plugins_public/flatpickr/dist/' + sNameL);
  318. break;
  319. case 2: //--- dark
  320. oStylesheet = document.head.querySelector("link[href*='" + sNameL + "']");
  321. if(oStylesheet)
  322. oStylesheet.href = oStylesheet.href.replace(sNameL, sNameD);
  323. else
  324. bx_get_style(sUrlRoot + 'plugins_public/flatpickr/dist/' + sNameD);
  325. break;
  326. }
  327. });
  328. }
  329. if (window.navigator.appVersion.search(/Chrome\/(.*?) /) == -1 || parseInt(window.navigator.appVersion.match(/Chrome\/(\d+)\./)[1], 10) < 24)
  330. if( this.getAttribute("allow_input") == null)
  331. $(this).attr('readonly', 'readonly');
  332. }
  333. // RGB/RGBA pickers
  334. if(this.getAttribute('type') == 'text' && (oInput.hasClass('bx-form-input-rgb') || oInput.hasClass('bx-form-input-rgba')) && !oInput.hasClass('minicolors-input')) {
  335. oInput.attr('autocomplete', 'off');
  336. oInput.minicolors({
  337. control: oInput.attr('data-control') || 'hue',
  338. format: oInput.attr('data-format') || 'rgb',
  339. letterCase: oInput.attr('data-letterCase') || 'lowercase',
  340. opacity: oInput.hasClass('bx-form-input-rgba'),
  341. position: oInput.attr('data-position') || 'bottom left',
  342. theme: 'bootstrap'
  343. });
  344. }
  345. });
  346. $('form', this).addClass('bx-form-processed');
  347. return this;
  348. };
  349. })(jQuery);
  350. $(document).ready(function() {
  351. $(this).addWebForms();
  352. });
  353. (function($) {
  354. $.fn.removeClassWild = function(mask) {
  355. return this.removeClass(function(index, cls) {
  356. var re = mask.replace(/\*/g, '\\S+');
  357. return (cls.match(new RegExp('\\b' + re + '', 'g')) || []).join(' ');
  358. });
  359. };
  360. })(jQuery);
  361. /** @} */