jquery.webForms.js 9.7 KB

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