jquery.webForms.js 18 KB

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