jquery.webForms.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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() {
  10. if ('undefined' === typeof(glJsLoadOnaddWebForms) || !glJsLoadOnaddWebForms.length) {
  11. $.fn.processWebForms.apply(this);
  12. }
  13. else {
  14. var $this = this;
  15. bx_get_scripts(glJsLoadOnaddWebForms, function () {
  16. $.fn.processWebForms.apply($this);
  17. });
  18. }
  19. return this;
  20. }
  21. $.fn.processWebForms = function() {
  22. // switchers
  23. $('.bx-switcher-cont', this).each(function() {
  24. var eSwitcher = $(this);
  25. var eInput = $(this).find('input');
  26. if (eSwitcher.hasClass('bx-form-switcher-processed'))
  27. return;
  28. eSwitcher.addClass('bx-form-switcher-processed');
  29. eSwitcher.on('click', function() {
  30. var $this = $(this);
  31. if ($this.hasClass('on')) {
  32. $this.find('input').prop('checked', false).trigger('change');
  33. $this.removeClass('on').addClass('off');
  34. } else {
  35. $this.removeClass('off').addClass('on');
  36. $this.find('input').prop('checked', true).trigger('change');
  37. }
  38. return false;
  39. });
  40. eInput.on('enable', function () {
  41. if (!$(this).prop('checked'))
  42. $(this).prop('checked', true).trigger('change');
  43. if (!eSwitcher.hasClass('on'))
  44. eSwitcher.removeClass('off').addClass('on');
  45. });
  46. eInput.on('disable', function () {
  47. if ($(this).prop('checked'))
  48. $(this).prop('checked', false).trigger('change');
  49. if (!eSwitcher.hasClass('off'))
  50. eSwitcher.removeClass('on').addClass('off');
  51. });
  52. });
  53. // collapsable headers
  54. $('.bx-form-collapsable', this).each(function() {
  55. var eFormSection = $(this);
  56. if (eFormSection.hasClass('bx-form-js-processed'))
  57. return;
  58. eFormSection.addClass('bx-form-js-processed');
  59. var fCallback = function() {
  60. if (eFormSection.hasClass('bx-form-collapsed')) {
  61. // show
  62. eFormSection.removeClass('bx-form-collapsed');
  63. if (eFormSection.hasClass('bx-form-section-hidden')) {
  64. $('.bx-form-section-content:first', eFormSection).hide();
  65. eFormSection.removeClass('bx-form-section-hidden');
  66. }
  67. $('.bx-form-section-content:first', eFormSection).slideDown(function () {
  68. eFormSection.removeClass('bx-form-section-hidden');
  69. $(this).addWebForms();
  70. //--- fire custom event 'bx_show'
  71. eFormSection.trigger('bx_show');
  72. });
  73. } else {
  74. // hide
  75. eFormSection.addClass('bx-form-collapsed');
  76. $('.bx-form-section-content:first', eFormSection).slideUp(function () {
  77. eFormSection.addClass('bx-form-collapsed bx-form-section-hidden');
  78. //--- fire custom event 'bx_hide'
  79. eFormSection.trigger('bx_hide');
  80. });
  81. }
  82. };
  83. $('<u class="bx-form-section-toggler"><i class="sys-icon chevron-right"></i></u>').prependTo($('legend', eFormSection));
  84. $('legend:first', eFormSection).click(fCallback);
  85. });
  86. $("input", this).each(function() {
  87. var oInput = $(this);
  88. var onCreateRange = function (event, ui) {
  89. var eInput = $(this).parent().find('input');
  90. var iSliderWidth = eInput.innerWidth() - 160;
  91. $(this).css('width', iSliderWidth + 'px').css('top', (eInput.innerHeight() - $(this).outerHeight())/2 + 1 + 'px');
  92. eInput.css('padding-right', (iSliderWidth + parseFloat($(this).css('margin-right')) + 10) + 'px');
  93. };
  94. // DoubleRange
  95. if (this.getAttribute("type") == 'doublerange') {
  96. var cur = $(this);
  97. if(!cur.is(':visible') || cur.hasClass('bx-form-doublerange-processed'))
  98. return;
  99. cur.addClass('bx-form-doublerange-processed');
  100. var $slider = $('<div class="bx-def-margin-right"></div>').insertAfter(cur);
  101. var iMin = cur.attr("min") ? parseFloat(cur.attr("min"), 10) : 0;
  102. var iMax = cur.attr("max") ? parseFloat(cur.attr("max"), 10) : 100;
  103. var sRangeDv = cur.attr("range-divider") ? cur.attr("range-divider") : '-';
  104. var funcGetValues = function (e) {
  105. var values = e.val().split(sRangeDv, 2); // get values
  106. if (typeof(values[0]) != 'undefined' && values[0].length)
  107. values[0] = parseFloat(values[0]);
  108. else
  109. values[0] = iMin;
  110. if (typeof(values[1]) != 'undefined' && values[1].length)
  111. values[1] = parseFloat(values[1]);
  112. else
  113. values[1] = iMax;
  114. return values;
  115. };
  116. var onChange = function(e, ui) {
  117. values = ui.values;
  118. cur.val( values[0] + sRangeDv + values[1] );
  119. };
  120. $slider.slider({
  121. range: true,
  122. min: iMin,
  123. max: iMax,
  124. step: parseFloat(cur.attr("step")) ? parseFloat(cur.attr("step")) : 1,
  125. values: funcGetValues(cur),
  126. change: onChange,
  127. slide: onChange,
  128. create: onCreateRange
  129. });
  130. cur.bind('change', function () {
  131. var values = funcGetValues($(this));
  132. $slider.slider("option", "values", values);
  133. });
  134. }
  135. // Single range or slider
  136. else if (this.getAttribute("type") == 'slider') {
  137. var cur = $(this);
  138. if(!cur.is(':visible') || cur.hasClass('bx-form-range-processed'))
  139. return;
  140. cur.addClass('bx-form-range-processed');
  141. var $slider = $('<div class="bx-def-margin-right"></div>').insertAfter(cur)
  142. $slider.css('width', ($slider.parent().innerWidth() - cur.outerWidth() - 50) + 'px');
  143. var iMin = cur.attr("min") ? parseFloat(cur.attr("min"), 10) : 0;
  144. var iMax = cur.attr("max") ? parseFloat(cur.attr("max"), 10) : 100;
  145. var onChange = function(e, ui) {
  146. cur.val( ui.value );
  147. };
  148. $slider.slider({
  149. min: iMin,
  150. max: iMax,
  151. step: parseFloat(cur.attr("step")) ? parseFloat(cur.attr("step")) : 1,
  152. value: cur.val(),
  153. change: onChange,
  154. slide: onChange,
  155. create: onCreateRange
  156. });
  157. cur.bind('change', function () {
  158. $slider.slider("option", "value", $(this).val());
  159. });
  160. }
  161. // Date/Time pickers
  162. if (this.getAttribute("type") == "date" || this.getAttribute("type") == "date_calendar" || this.getAttribute("type") == "datetime" || this.getAttribute("type") == "date_time" || this.getAttribute("type") == "datepicker") {
  163. if ($(this).hasClass('bx-form-datepicker-processed'))
  164. return;
  165. $(this).addClass('bx-form-datepicker-processed');
  166. var iYearMin = '1900';
  167. var iYearMax = '2100';
  168. var m;
  169. if ($(this).attr('min') && (m = $(this).attr('min').match(/^(\d{4})/)))
  170. iYearMin = m[1];
  171. if ($(this).attr('max') && (m = $(this).attr('max').match(/^(\d{4})/)))
  172. iYearMax = m[1];
  173. var onBeforeShow = function(oInput, oInstance) {
  174. $(oInstance.dpDiv).addClass('bx-form-datepicker-modal');
  175. };
  176. if (this.getAttribute("type") == "date" || this.getAttribute("type") == "date_calendar" || this.getAttribute("type") == "datepicker") { // Date picker
  177. $(this).datepicker({
  178. changeYear: true,
  179. changeMonth: true,
  180. dateFormat: 'yy-mm-dd',
  181. //defaultDate: '-22y',
  182. yearRange: iYearMin + ':' + iYearMax,
  183. beforeShow: onBeforeShow
  184. });
  185. } else if(this.getAttribute("type") == "datetime" || this.getAttribute("type") == "date_time") { // DateTime picker
  186. var oPickerOptions = {
  187. changeYear: true,
  188. changeMonth: true,
  189. dateFormat: 'yy-mm-dd',
  190. beforeShow: onBeforeShow
  191. };
  192. if (1 == $(this).attr('data-utc')) {
  193. var e = $(this).clone();
  194. if ($(this).val().length) {
  195. e.val(moment($(this).val() + ' Z').format("YYYY-MM-DD HH:mm:0 Z"));
  196. $(this).val(moment($(this).val() + ' Z').format("YYYY-MM-DD HH:mm"));
  197. }
  198. e.attr('type', 'hidden').appendTo($(this).parent());
  199. $(this).attr('name', $(this).attr('name') + '_fake');
  200. oPickerOptions['altField'] = e;
  201. oPickerOptions['altFieldTimeOnly'] = false;
  202. oPickerOptions['altTimeFormat'] = 'HH:mm:00 Z';
  203. oPickerOptions['showTimezone'] = 'false';
  204. }
  205. $(this).datetimepicker(oPickerOptions);
  206. }
  207. if (window.navigator.appVersion.search(/Chrome\/(.*?) /) == -1 || parseInt(window.navigator.appVersion.match(/Chrome\/(\d+)\./)[1], 10) < 24)
  208. if( this.getAttribute("allow_input") == null)
  209. $(this).attr('readonly', 'readonly');
  210. }
  211. // RGB/RGBA pickers
  212. if(this.getAttribute('type') == 'text' && (oInput.hasClass('bx-form-input-rgb') || oInput.hasClass('bx-form-input-rgba')) && !oInput.hasClass('minicolors-input')) {
  213. oInput.minicolors({
  214. control: oInput.attr('data-control') || 'hue',
  215. format: oInput.attr('data-format') || 'rgb',
  216. letterCase: oInput.attr('data-letterCase') || 'lowercase',
  217. opacity: oInput.hasClass('bx-form-input-rgba'),
  218. position: oInput.attr('data-position') || 'bottom left',
  219. theme: 'bootstrap'
  220. });
  221. }
  222. });
  223. $('form', this).addClass('bx-form-processed');
  224. return this;
  225. };
  226. })(jQuery);
  227. $(document).ready(function() {
  228. $(this).addWebForms();
  229. });
  230. /** @} */