소스 검색

Extract input selector into variable

Manuel Meurer 11 년 전
부모
커밋
b7616d2b74
1개의 변경된 파일13개의 추가작업 그리고 12개의 파일을 삭제
  1. 13 12
      static/js/bootstrap-switch.js

+ 13 - 12
static/js/bootstrap-switch.js

@@ -16,6 +16,7 @@
   "use strict";
 
   $.fn['bootstrapSwitch'] = function (method) {
+    var inputSelector = 'input[type!="hidden"]';
     var methods = {
       init: function () {
         return this.each(function () {
@@ -71,13 +72,13 @@
             $label = $('<label>')
               .html("&nbsp;")
               .addClass(myClasses)
-              .attr('for', $element.find('input[type!="hidden"]').attr('id'));
+              .attr('for', $element.find(inputSelector).attr('id'));
 
             if (icon) {
               $label.html('<i class="icon icon-' + icon + '"></i>');
             }
 
-            $div = $element.find('input[type!="hidden"]').wrap($('<div>')).parent().data('animated', false);
+            $div = $element.find(inputSelector).wrap($('<div>')).parent().data('animated', false);
 
             if ($element.data('animated') !== false)
               $div.addClass('switch-animate').data('animated', true);
@@ -88,10 +89,10 @@
               .append($switchRight);
 
             $element.find('>div').addClass(
-              $element.find('input[type!="hidden"]').is(':checked') ? 'switch-on' : 'switch-off'
+              $element.find(inputSelector).is(':checked') ? 'switch-on' : 'switch-off'
             );
 
-            if ($element.find('input[type!="hidden"]').is(':disabled'))
+            if ($element.find(inputSelector).is(':disabled'))
               $(this).addClass('deactivate');
 
             var changeStatus = function ($this) {
@@ -114,7 +115,7 @@
               changeStatus($(this));
             });
 
-            $element.find('input[type!="hidden"]').on('change', function (e, skipOnChange) {
+            $element.find(inputSelector).on('change', function (e, skipOnChange) {
               var $this = $(this)
                 , $element = $this.parent()
                 , thisState = $this.is(':checked')
@@ -216,8 +217,8 @@
               $form.bind('reset', function () {
                 setTimeout(function () {
                   $form.find('.switch').each(function () {
-                    var $input = $(this).find('input[type!="hidden"]');
-                    
+                    var $input = $(this).find(inputSelector);
+
                     $input.prop('checked', $input.is(':checked')).trigger('change');
                   });
                 }, 1);
@@ -231,7 +232,7 @@
         var $this = $(this);
 
         $this.toggleClass('deactivate');
-        $this.find('input[type!="hidden"]').prop('disabled', $this.is('.deactivate'));
+        $this.find(inputSelector).prop('disabled', $this.is('.deactivate'));
       },
       isActive: function () {
         return !$(this).hasClass('deactivate');
@@ -241,11 +242,11 @@
 
         if (active) {
           $this.removeClass('deactivate');
-          $this.find('input[type!="hidden"]').removeAttr('disabled');
+          $this.find(inputSelector).removeAttr('disabled');
         }
         else {
           $this.addClass('deactivate');
-          $this.find('input[type!="hidden"]').attr('disabled', 'disabled');
+          $this.find(inputSelector).attr('disabled', 'disabled');
         }
       },
       toggleState: function (skipOnChange) {
@@ -266,10 +267,10 @@
         }
       },
       setState: function (value, skipOnChange) {
-        $(this).find('input[type!="hidden"]').prop('checked', value).trigger('change', skipOnChange);
+        $(this).find(inputSelector).prop('checked', value).trigger('change', skipOnChange);
       },
       status: function () {
-        return $(this).find('input[type!="hidden"]').is(':checked');
+        return $(this).find(inputSelector).is(':checked');
       },
       destroy: function () {
         var $element = $(this)