var ShConfig = ShConfig||{}; ShConfig.Slider = {}; ShConfig.Slider.uiInit = function($, min, max, step, watcher, value){ $(".slider").slider({ range: "min", min: min, max: max, step: step, value: value, create: function(){ $('#slider-value').appendTo($('.slider a').get(0)); $('#slider-price').appendTo($('.slider a').get(0)); }, slide: function(event, ui){ watcher.trigger('SLIDE', ui.value); }, stop: function(){ watcher.trigger('STOP'); }, }); }; ShConfig.Slider.watcher = function(){ var watches = {}; return { watch: function(callbacks) { var id = Math.random().toString(); watches[id] = callbacks; // Return a function that removes the listener return function() { watches[id] = null; delete watches[id]; }; }, trigger: function(type, value) { for (var k in watches) { if(type == 'SLIDE'){ watches[k].slide(value); } if(type == 'STOP'){ watches[k].stop(); } } } }; }; ShConfig.Slider.ModelController = function(itemList, max, predefined){ return function($scope){ $scope.label = predefined.label; $scope.price = predefined.price; $scope.max = max; var unbind = consumptionWatcher.watch({ slide: function(newVal){ for(var i in itemList){ if(itemList[i].value == newVal){ $scope.label = itemList[i].label; $scope.price = itemList[i].price; $scope.$apply(); } } }, stop: function(){ //nothing to do } }); $scope.$on('$destroy', unbind); }; }; ShConfig.Slider.buildPredefinedSelection = function( predefinedSelectionLabel, itemList ){ var sliderSelectionFromConsumptionList = function(i){ return { label: itemList[i].label, price: itemList[i].price, value: itemList[i].value, }; }; for(var i in itemList){ if(itemList[i].label == predefinedSelectionLabel){ return sliderSelectionFromConsumptionList(i); } } return sliderSelectionFromConsumptionList(0); };