jquery.webForms.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. // switchers
  27. $('.bx-switcher-cont', this).each(function() {
  28. var eSwitcher = $(this);
  29. var eInput = $(this).find('input');
  30. if (eSwitcher.hasClass('bx-form-switcher-processed'))
  31. return;
  32. eSwitcher.addClass('bx-form-switcher-processed');
  33. eSwitcher.on('click', function() {
  34. var $this = $(this);
  35. if ($this.hasClass('on')) {
  36. $this.find('input').prop('checked', false).trigger('change');
  37. $this.removeClass('on').addClass('off');
  38. } else {
  39. $this.removeClass('off').addClass('on');
  40. $this.find('input').prop('checked', true).trigger('change');
  41. }
  42. return false;
  43. });
  44. eInput.on('enable', function () {
  45. if (!$(this).prop('checked'))
  46. $(this).prop('checked', true).trigger('change');
  47. if (!eSwitcher.hasClass('on'))
  48. eSwitcher.removeClass('off').addClass('on');
  49. });
  50. eInput.on('disable', function () {
  51. if ($(this).prop('checked'))
  52. $(this).prop('checked', false).trigger('change');
  53. if (!eSwitcher.hasClass('off'))
  54. eSwitcher.removeClass('on').addClass('off');
  55. });
  56. });
  57. // collapsable headers
  58. $('.bx-form-collapsable', this).each(function() {
  59. var eFormSection = $(this);
  60. if (eFormSection.hasClass('bx-form-js-processed'))
  61. return;
  62. eFormSection.addClass('bx-form-js-processed');
  63. var fCallback = function() {
  64. if (eFormSection.hasClass('bx-form-collapsed')) {
  65. // show
  66. eFormSection.removeClass('bx-form-collapsed');
  67. if (eFormSection.hasClass('bx-form-section-hidden')) {
  68. $('.bx-form-section-content:first', eFormSection).hide();
  69. eFormSection.removeClass('bx-form-section-hidden');
  70. }
  71. $('.bx-form-section-content:first', eFormSection).slideDown(function () {
  72. eFormSection.removeClass('bx-form-section-hidden');
  73. $(this).addWebForms();
  74. //--- fire custom event 'bx_show'
  75. eFormSection.trigger('bx_show');
  76. });
  77. } else {
  78. // hide
  79. eFormSection.addClass('bx-form-collapsed');
  80. $('.bx-form-section-content:first', eFormSection).slideUp(function () {
  81. eFormSection.addClass('bx-form-collapsed bx-form-section-hidden');
  82. //--- fire custom event 'bx_hide'
  83. eFormSection.trigger('bx_hide');
  84. });
  85. }
  86. };
  87. $('<u class="bx-form-section-toggler"><i class="sys-icon chevron-right"></i></u>').prependTo($('.bx-form-section-title', eFormSection));
  88. $('.bx-form-section-title:first', eFormSection).click(fCallback);
  89. });
  90. $("select", this).each(function () {
  91. // Labels selector
  92. var oInput = $(this);
  93. if (oInput.hasClass('bx-form-select-labels')) {
  94. $(".bx-form-select-labels").select2ToTree();
  95. }
  96. if(oInput.hasClass('bx-form-input-rgb-list')) {
  97. oInput.bind('change', function() {
  98. oInput.removeClassWild('bg-*').addClass('bg-' + $(this).val());
  99. });
  100. oInput.removeClassWild('bg-*').addClass('bg-' + $(this).val());
  101. }
  102. });
  103. if (!$(".bx-form-input-wrapper-password").hasClass("bx-inited")) {
  104. var sClassFocus = "bx-form-input-focus";
  105. $(".bx-form-input-wrapper-password input").bind('focus', function() {
  106. $(this).parents(".bx-form-input-wrapper-password:first").addClass(sClassFocus);
  107. }).bind('blur', function() {
  108. $(this).parents(".bx-form-input-wrapper-password:first").removeClass(sClassFocus);
  109. });
  110. $(".bx-form-input-wrapper-password a").on("click", function () {
  111. var oIcon = $(this).find("i");
  112. var oFld = $(this).parents(".bx-form-input-wrapper-password").find("input");
  113. if (oIcon.hasClass("eye")) {
  114. oIcon.removeClass("eye").addClass("eye-slash");
  115. oFld.attr("type", "text");
  116. }
  117. else {
  118. oIcon.addClass("eye").removeClass("eye-slash");
  119. oFld.attr("type", "password");
  120. }
  121. });
  122. $(".bx-form-input-wrapper-password").addClass("bx-inited");
  123. }
  124. $("input", this).each(function() {
  125. var oInput = $(this);
  126. var onCreateRange = function (event, ui) {
  127. var eInput = $(this).parent().find('input');
  128. var iSliderWidth = eInput.innerWidth() - 160;
  129. $(this).css('width', iSliderWidth + 'px').css('top', (eInput.innerHeight() - $(this).outerHeight())/2 + 1 + 'px');
  130. eInput.css('padding-right', (iSliderWidth + parseFloat($(this).css('margin-right')) + 10) + 'px');
  131. };
  132. // DoubleRange
  133. if (this.getAttribute("type") == 'doublerange') {
  134. var cur = $(this);
  135. if(!cur.is(':visible') || cur.hasClass('bx-form-doublerange-processed'))
  136. return;
  137. cur.addClass('bx-form-doublerange-processed');
  138. var $slider = $('<div class="bx-def-margin-right"></div>').insertAfter(cur);
  139. var iMin = cur.attr("min") ? parseFloat(cur.attr("min"), 10) : 0;
  140. var iMax = cur.attr("max") ? parseFloat(cur.attr("max"), 10) : 100;
  141. var sRangeDv = cur.attr("range-divider") ? cur.attr("range-divider") : '-';
  142. var funcGetValues = function (e) {
  143. var values = e.val().split(sRangeDv, 2); // get values
  144. if (typeof(values[0]) != 'undefined' && values[0].length)
  145. values[0] = parseFloat(values[0]);
  146. else
  147. values[0] = iMin;
  148. if (typeof(values[1]) != 'undefined' && values[1].length)
  149. values[1] = parseFloat(values[1]);
  150. else
  151. values[1] = iMax;
  152. return values;
  153. };
  154. var onChange = function(e, ui) {
  155. values = ui.values;
  156. cur.val( values[0] + sRangeDv + values[1] );
  157. };
  158. $slider.slider({
  159. range: true,
  160. min: iMin,
  161. max: iMax,
  162. step: parseFloat(cur.attr("step")) ? parseFloat(cur.attr("step")) : 1,
  163. values: funcGetValues(cur),
  164. change: onChange,
  165. slide: onChange,
  166. create: onCreateRange
  167. });
  168. cur.bind('change', function () {
  169. var values = funcGetValues($(this));
  170. $slider.slider("option", "values", values);
  171. });
  172. }
  173. // Single range or slider
  174. else if (this.getAttribute("type") == 'slider') {
  175. var cur = $(this);
  176. if(!cur.is(':visible') || cur.hasClass('bx-form-range-processed'))
  177. return;
  178. cur.addClass('bx-form-range-processed');
  179. var $slider = $('<div class="bx-def-margin-right"></div>').insertAfter(cur)
  180. $slider.css('width', ($slider.parent().innerWidth() - cur.outerWidth() - 50) + 'px');
  181. var iMin = cur.attr("min") ? parseFloat(cur.attr("min"), 10) : 0;
  182. var iMax = cur.attr("max") ? parseFloat(cur.attr("max"), 10) : 100;
  183. var onChange = function(e, ui) {
  184. cur.val( ui.value );
  185. };
  186. $slider.slider({
  187. min: iMin,
  188. max: iMax,
  189. step: parseFloat(cur.attr("step")) ? parseFloat(cur.attr("step")) : 1,
  190. value: cur.val(),
  191. change: onChange,
  192. slide: onChange,
  193. create: onCreateRange
  194. });
  195. cur.bind('change', function () {
  196. $slider.slider("option", "value", $(this).val());
  197. });
  198. }
  199. // Date/Time pickers
  200. 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") {
  201. if ($(this).hasClass('bx-form-datepicker-processed'))
  202. return;
  203. $(this).addClass('bx-form-datepicker-processed');
  204. var sYearMin = '1900';
  205. var sYearMax = '2100';
  206. var m;
  207. if ($(this).attr('min') && (m = $(this).attr('min').match(/^(\d{4})/)))
  208. sYearMin = m[1];
  209. if ($(this).attr('max') && (m = $(this).attr('max').match(/^(\d{4})/)))
  210. sYearMax = m[1];
  211. var onBeforeShow = function(oInput, oInstance) {
  212. $(oInstance.dpDiv).addClass('bx-form-datepicker-modal');
  213. };
  214. if (this.getAttribute("type") == "date" || this.getAttribute("type") == "date_calendar" || this.getAttribute("type") == "datepicker") { // Date picker
  215. flatpickr(this, {
  216. altInput: true,
  217. altFormat: this.getAttribute("data-frmt-date") ? this.getAttribute("data-frmt-date") : "F j, Y",
  218. dateFormat: "Y-m-d",
  219. minDate: sYearMin,
  220. maxDate: sYearMax,
  221. onOpen: onBeforeShow
  222. });
  223. } else if(this.getAttribute("type") == "datetime" || this.getAttribute("type") == "date_time") { // DateTime picker
  224. var oPickerOptions = {
  225. enableTime: true,
  226. altInput: true,
  227. altFormat: this.getAttribute("data-frmt-date") ? this.getAttribute("data-frmt-datetime") : "F j, Y H:i",
  228. dateFormat: "Y-m-d H:i",
  229. time_24hr: null === this.getAttribute("data-frmt-24h") || 'on' == this.getAttribute("data-frmt-24h") ? true : false,
  230. minDate: sYearMin,
  231. maxDate: sYearMax,
  232. onOpen: onBeforeShow
  233. };
  234. if (1 == $(this).attr('data-utc'))
  235. oPickerOptions.dateFormat = "Y-m-d H:i:00 \\Z";
  236. flatpickr(this, oPickerOptions);
  237. } else if(this.getAttribute("type") == "dateselect") { // DateTime selector
  238. moment.locale(sLang);
  239. $(this).combodate({
  240. minYear: sYearMin,
  241. maxYear: sYearMax,
  242. format: 'YYYY-MM-DD',
  243. template: 'DD MMMM YYYY',
  244. firstItem: 'none',
  245. 'smartDays': true,
  246. 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',
  247. });
  248. }
  249. if (window.navigator.appVersion.search(/Chrome\/(.*?) /) == -1 || parseInt(window.navigator.appVersion.match(/Chrome\/(\d+)\./)[1], 10) < 24)
  250. if( this.getAttribute("allow_input") == null)
  251. $(this).attr('readonly', 'readonly');
  252. }
  253. // RGB/RGBA pickers
  254. if(this.getAttribute('type') == 'text' && (oInput.hasClass('bx-form-input-rgb') || oInput.hasClass('bx-form-input-rgba')) && !oInput.hasClass('minicolors-input')) {
  255. oInput.attr('autocomplete', 'off');
  256. oInput.minicolors({
  257. control: oInput.attr('data-control') || 'hue',
  258. format: oInput.attr('data-format') || 'rgb',
  259. letterCase: oInput.attr('data-letterCase') || 'lowercase',
  260. opacity: oInput.hasClass('bx-form-input-rgba'),
  261. position: oInput.attr('data-position') || 'bottom left',
  262. theme: 'bootstrap'
  263. });
  264. }
  265. });
  266. $('form', this).addClass('bx-form-processed');
  267. return this;
  268. };
  269. })(jQuery);
  270. $(document).ready(function() {
  271. $(this).addWebForms();
  272. });
  273. (function($) {
  274. $.fn.removeClassWild = function(mask) {
  275. return this.removeClass(function(index, cls) {
  276. var re = mask.replace(/\*/g, '\\S+');
  277. return (cls.match(new RegExp('\\b' + re + '', 'g')) || []).join(' ');
  278. });
  279. };
  280. })(jQuery);
  281. /** @} */