jquery.webForms.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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($('legend', eFormSection));
  88. $('legend: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. });
  97. if (!$(".bx-form-input-wrapper-password").hasClass("bx-inited")) {
  98. var sClassFocus = "bx-form-input-focus";
  99. $(".bx-form-input-wrapper-password input").bind('focus', function() {
  100. $(this).parents(".bx-form-input-wrapper-password:first").addClass(sClassFocus);
  101. }).bind('blur', function() {
  102. $(this).parents(".bx-form-input-wrapper-password:first").removeClass(sClassFocus);
  103. });
  104. $(".bx-form-input-wrapper-password a").on("click", function () {
  105. var oIcon = $(this).find("i");
  106. var oFld = $(this).parents(".bx-form-input-wrapper-password").find("input");
  107. if (oIcon.hasClass("eye")) {
  108. oIcon.removeClass("eye").addClass("eye-slash");
  109. oFld.attr("type", "text");
  110. }
  111. else {
  112. oIcon.addClass("eye").removeClass("eye-slash");
  113. oFld.attr("type", "password");
  114. }
  115. });
  116. $(".bx-form-input-wrapper-password").addClass("bx-inited");
  117. }
  118. $("input", this).each(function() {
  119. var oInput = $(this);
  120. var onCreateRange = function (event, ui) {
  121. var eInput = $(this).parent().find('input');
  122. var iSliderWidth = eInput.innerWidth() - 160;
  123. $(this).css('width', iSliderWidth + 'px').css('top', (eInput.innerHeight() - $(this).outerHeight())/2 + 1 + 'px');
  124. eInput.css('padding-right', (iSliderWidth + parseFloat($(this).css('margin-right')) + 10) + 'px');
  125. };
  126. // DoubleRange
  127. if (this.getAttribute("type") == 'doublerange') {
  128. var cur = $(this);
  129. if(!cur.is(':visible') || cur.hasClass('bx-form-doublerange-processed'))
  130. return;
  131. cur.addClass('bx-form-doublerange-processed');
  132. var $slider = $('<div class="bx-def-margin-right"></div>').insertAfter(cur);
  133. var iMin = cur.attr("min") ? parseFloat(cur.attr("min"), 10) : 0;
  134. var iMax = cur.attr("max") ? parseFloat(cur.attr("max"), 10) : 100;
  135. var sRangeDv = cur.attr("range-divider") ? cur.attr("range-divider") : '-';
  136. var funcGetValues = function (e) {
  137. var values = e.val().split(sRangeDv, 2); // get values
  138. if (typeof(values[0]) != 'undefined' && values[0].length)
  139. values[0] = parseFloat(values[0]);
  140. else
  141. values[0] = iMin;
  142. if (typeof(values[1]) != 'undefined' && values[1].length)
  143. values[1] = parseFloat(values[1]);
  144. else
  145. values[1] = iMax;
  146. return values;
  147. };
  148. var onChange = function(e, ui) {
  149. values = ui.values;
  150. cur.val( values[0] + sRangeDv + values[1] );
  151. };
  152. $slider.slider({
  153. range: true,
  154. min: iMin,
  155. max: iMax,
  156. step: parseFloat(cur.attr("step")) ? parseFloat(cur.attr("step")) : 1,
  157. values: funcGetValues(cur),
  158. change: onChange,
  159. slide: onChange,
  160. create: onCreateRange
  161. });
  162. cur.bind('change', function () {
  163. var values = funcGetValues($(this));
  164. $slider.slider("option", "values", values);
  165. });
  166. }
  167. // Single range or slider
  168. else if (this.getAttribute("type") == 'slider') {
  169. var cur = $(this);
  170. if(!cur.is(':visible') || cur.hasClass('bx-form-range-processed'))
  171. return;
  172. cur.addClass('bx-form-range-processed');
  173. var $slider = $('<div class="bx-def-margin-right"></div>').insertAfter(cur)
  174. $slider.css('width', ($slider.parent().innerWidth() - cur.outerWidth() - 50) + 'px');
  175. var iMin = cur.attr("min") ? parseFloat(cur.attr("min"), 10) : 0;
  176. var iMax = cur.attr("max") ? parseFloat(cur.attr("max"), 10) : 100;
  177. var onChange = function(e, ui) {
  178. cur.val( ui.value );
  179. };
  180. $slider.slider({
  181. min: iMin,
  182. max: iMax,
  183. step: parseFloat(cur.attr("step")) ? parseFloat(cur.attr("step")) : 1,
  184. value: cur.val(),
  185. change: onChange,
  186. slide: onChange,
  187. create: onCreateRange
  188. });
  189. cur.bind('change', function () {
  190. $slider.slider("option", "value", $(this).val());
  191. });
  192. }
  193. // Date/Time pickers
  194. if (this.getAttribute("type") == "date" || this.getAttribute("type") == "date_calendar" || this.getAttribute("type") == "datetime" || this.getAttribute("type") == "date_time" || this.getAttribute("type") == "datepicker") {
  195. if ($(this).hasClass('bx-form-datepicker-processed'))
  196. return;
  197. $(this).addClass('bx-form-datepicker-processed');
  198. var iYearMin = '1900';
  199. var iYearMax = '2100';
  200. var m;
  201. if ($(this).attr('min') && (m = $(this).attr('min').match(/^(\d{4})/)))
  202. iYearMin = m[1];
  203. if ($(this).attr('max') && (m = $(this).attr('max').match(/^(\d{4})/)))
  204. iYearMax = m[1];
  205. var onBeforeShow = function(oInput, oInstance) {
  206. $(oInstance.dpDiv).addClass('bx-form-datepicker-modal');
  207. };
  208. if (this.getAttribute("type") == "date" || this.getAttribute("type") == "date_calendar" || this.getAttribute("type") == "datepicker") { // Date picker
  209. $(this).datepicker({
  210. changeYear: true,
  211. changeMonth: true,
  212. dateFormat: 'yy-mm-dd',
  213. //defaultDate: '-22y',
  214. yearRange: iYearMin + ':' + iYearMax,
  215. beforeShow: onBeforeShow
  216. });
  217. } else if(this.getAttribute("type") == "datetime" || this.getAttribute("type") == "date_time") { // DateTime picker
  218. var oPickerOptions = {
  219. changeYear: true,
  220. changeMonth: true,
  221. dateFormat: 'yy-mm-dd',
  222. beforeShow: onBeforeShow
  223. };
  224. if (1 == $(this).attr('data-utc')) {
  225. var e = $(this).clone();
  226. if ($(this).val().length) {
  227. e.val(moment($(this).val() + ' Z').format("YYYY-MM-DD HH:mm:0 Z"));
  228. $(this).val(moment($(this).val() + ' Z').format("YYYY-MM-DD HH:mm"));
  229. }
  230. e.attr('type', 'hidden').appendTo($(this).parent());
  231. $(this).attr('name', $(this).attr('name') + '_fake');
  232. oPickerOptions['altField'] = e;
  233. oPickerOptions['altFieldTimeOnly'] = false;
  234. oPickerOptions['altTimeFormat'] = 'HH:mm:00 Z';
  235. oPickerOptions['showTimezone'] = 'false';
  236. }
  237. $(this).datetimepicker(oPickerOptions);
  238. }
  239. if (window.navigator.appVersion.search(/Chrome\/(.*?) /) == -1 || parseInt(window.navigator.appVersion.match(/Chrome\/(\d+)\./)[1], 10) < 24)
  240. if( this.getAttribute("allow_input") == null)
  241. $(this).attr('readonly', 'readonly');
  242. }
  243. // RGB/RGBA pickers
  244. if(this.getAttribute('type') == 'text' && (oInput.hasClass('bx-form-input-rgb') || oInput.hasClass('bx-form-input-rgba')) && !oInput.hasClass('minicolors-input')) {
  245. oInput.attr('autocomplete', 'off');
  246. oInput.minicolors({
  247. control: oInput.attr('data-control') || 'hue',
  248. format: oInput.attr('data-format') || 'rgb',
  249. letterCase: oInput.attr('data-letterCase') || 'lowercase',
  250. opacity: oInput.hasClass('bx-form-input-rgba'),
  251. position: oInput.attr('data-position') || 'bottom left',
  252. theme: 'bootstrap'
  253. });
  254. }
  255. });
  256. $('form', this).addClass('bx-form-processed');
  257. return this;
  258. };
  259. })(jQuery);
  260. $(document).ready(function() {
  261. $(this).addWebForms();
  262. });
  263. /** @} */