'use strict';

angular.module('ng-nouislider', []).directive('nouislider', function(){
  return {
    restrict: 'A',
    scope: {
      min: '=',
      max: '=',
      minPadding: '=',
      maxPadding: '=',
      step: '=',
      margin: '=',
      enable: '=',
      range: '=',
      ngModel: '=',
      ngFrom: '=',
      ngTo: '='
    },
    link: function(scope, element, attrs) {
      var slider = $(element);
      var getMin = function() { return +scope.min; };
      var getMax = function() { return +scope.max; };
      var getRealMin = function() { return getMin() + (+(scope.minPadding || 0)); };
      var getRealMax = function() { return getMax() - (+(scope.maxPadding || 0)); };
      var getFrom = function() { return +(scope.ngFrom || scope.min); };
      var getTo = function() { return +(scope.ngTo || scope.max); };
      var getStep = function() { return +(scope.step || 1); };
      var getModel = function() { return +(scope.ngModel || scope.min); };
      var getMargin = function() { return +(scope.margin) || 0; };
      var regularVal = function(val) {
        if (val <= getRealMin()) { val = getRealMin(); }
        if (val >= getRealMax()) { val = getRealMax(); }
        return val;
      };

      if (scope.range === true || scope.range === 'true') {
        // range slider
        slider.noUiSlider({
          start:[
            getFrom(),
            getTo()
          ],
          connect: true,
          margin: getMargin(),
          step: getStep(),
          range: {
            min: [getMin()],
            max: [getMax()]
          }
        });

        slider.on('slide', function() {
          var from = regularVal(+slider.val()[0]), to = regularVal(+slider.val()[1]);
          slider.val([from, to]);
          scope.$apply(function() {
            scope.ngFrom = from;
            scope.ngTo = to;
          });
        });

        scope.$watch('ngFrom', function(newFrom, oldFrom) {
          if (newFrom !== oldFrom) { slider.val([newFrom, null]); }
        });

        scope.$watch('ngTo', function(newTo, oldTo) {
          if (newTo !== oldTo) { slider.val([null, newTo]); }
        });
      } else {
        // simple slider
        slider.noUiSlider({
          start: getModel(),
          step: getStep(),
          connect: 'lower',
          range: {
            min: getMin(),
            max: getMax()
          }
        });

        slider.on('slide', function() {
          var val = regularVal(+slider.val());
          slider.val(val);
          scope.$apply(function() { scope.ngModel = val; });
        });
        scope.$watch('ngModel', function(newVal, oldVal) {
          if (newVal !== oldVal) { slider.val(newVal); }
        });
      }

      scope.$watch('enable', function(newEnable, oldEnable) {
        if (newEnable === false || newEnable === 'false') { // remove disabled attr
          element.attr('disabled', true);
        } else { // add disabled attr
          element.removeAttr('disabled');
        }
      });

    }
  };

});

@@ -306,29 +306,13 @@
   ng
-From: '=',%0A      ngTo
+Range
 : '=
@@ -686,20 +686,21 @@
  var get
-From
+Range
  = funct
@@ -702,32 +702,40 @@
 function() %7B
+%0A       
  return 
 +(scope.ngFr
@@ -722,26 +722,24 @@
  return 
-+(
 scope.ng
 From %7C%7C 
@@ -734,30 +734,93 @@
 e.ng
-From %7C%7C scope.min); %7D;
+Range ? scope.ngRange.map(function(d) %7B return +d; %7D) : %5BgetMin(), getMax()%5D;%0A      %7D
 %0A   
@@ -829,18 +829,20 @@
  var get
-To
+From
  = funct
@@ -860,34 +860,77 @@
 urn 
-+(scope.ngTo %7C%7C scope.max)
+getRange()%5B0%5D; %7D;%0A      var getTo = function() %7B return getRange()%5B1%5D
 ; %7D;
@@ -1908,48 +1908,26 @@
 e.ng
-From
+Range
  = 
+%5B
 from
-;%0A            scope.ngTo =
+,
  to
+%5D
 ;%0A  
@@ -1975,16 +1975,27 @@
 atch
-('ngFrom
+Collection('ngRange
 ', f
@@ -2009,16 +2009,18 @@
 (newFrom
+To
 , oldFro
@@ -2020,16 +2020,18 @@
  oldFrom
+To
 ) %7B%0A    
@@ -2047,16 +2047,18 @@
 (newFrom
+To
  !== old
@@ -2061,16 +2061,18 @@
  oldFrom
+To
 ) %7B slid
@@ -2090,143 +2090,27 @@
 From
-, null%5D); %7D%0A        %7D);%0A%0A        scope.$watch('ngTo', function(newTo, oldTo) %7B%0A          if (newTo !== oldTo) %7B slider.val(%5Bnull, newTo
+To%5B0%5D, newFromTo%5B1%5D
 %5D); 

Change attributes ng-from, ng-to -> ng-range