Source: widgets/SpwTimer.js

Retour à la documentation
/**
 * @class spw.widgets.SpwTimer
 */
define(["dojo/_base/declare", "spw/api/SpwBaseTemplatedWidget", "dojo/text!./templates/SpwTimer.html",
        "dojo/i18n!./nls/SpwTimer","dojo/dom-style", "dojo/on", "dojo/dom-construct", "dojo/_base/lang",
        "dojo/_base/array","dijit/form/HorizontalSlider",
        "dijit/form/HorizontalRule","dijit/form/HorizontalRuleLabels"],
		function(declare, SpwBaseTemplatedWidget, tmpl,
				labels, domStyle, on, domConstruct, lang, array,HorizontalSlider,
				HorizontalRule,HorizontalRuleLabels) {
	return declare("spw.widgets.SpwTimer", [SpwBaseTemplatedWidget], /** @lends spw.widgets.SpwTimer.prototype */{
		
		templateString: tmpl,
		labels: labels,
		
		visible:true,
		time:100,
		
		btDropDown:null,
		sliderNode:null,
		slider:null,
		
		currentTimerService:null,
		horizontalRule:null,
		horizontalRuleLabels:null,
		
		defaultSelected:true,
		
        /**
         * @constructs
         * @param config
         */
		constructor: function() {
		},
		
		postCreate: function(){
			this.inherited(arguments);
			
			if(!this.defaultSelected){
				domConstruct.create("option", {innerHTML:this.labels.noTimerLabel}, this.timerSelect, "last");
			}
			
			if(this.mapServices){
				array.forEach(this.mapServices, lang.hitch(this, function(mapService){
					if(typeof(mapService.hasLegend) === "undefined")
						mapService.hasLegend = false;
					domConstruct.create("option", {innerHTML:mapService.label}, this.timerSelect, "last");
				}));
			}
		},
		
		onActivate: function(){
			this.inherited(arguments);
			this.activateTimer();
		},
		
		onDeactivate: function(){
			this.inherited(arguments);
			this.deactivateTimer();
		},
		
		activateTimer: function(){
			
			this.deactivateTimer();
			
			var index = this.getIndexSelected();
			
			if(index !== null && this.mapServices[index]){
				
				this.sliderNode = domConstruct.create("div", {}, this.timerDIV, "last");
				
				this.currentTimerService = this.spwViewer.get('spwMap').addMapService(this.mapServices[index]);
				
				var timerCount = this.currentTimerService.get('timeLayers').length;
				this.horizontalRule = new HorizontalRule({count:timerCount, container:"bottomDecoration"}, domConstruct.create("div", {}, this.sliderNode, "last"));
				
				var labels = [];
				array.forEach(this.currentTimerService.get('timeLayers'), function(timeLayer){
					labels.push(timeLayer.ruleLabel);
				});
				this.horizontalRuleLabels = new HorizontalRuleLabels({count:timerCount, container:"bottomDecoration", labels:labels}, domConstruct.create("div", {}, this.sliderNode, "last"));
				
				this.slider = new HorizontalSlider({
			        name: "slider",
			        value: 0,
			        minimum: 0,
			        maximum: 100,
			        discreteValues: 101,
			        intermediateChanges: true,
			        style: "width:100%;",
			        onChange: lang.hitch(this,function(value){
			        	this.currentTimerService.setTime(value,(this.slider.get('maximum') - (this.slider.get('minimum'))));
			        })
			    }, this.sliderNode);
			}
		},
		
		deactivateTimer: function(){
			if(this.slider)
				this.slider.destroy();
			
			if(this.currentTimerService){
				this.spwViewer.get('spwMap').removeMapService(this.currentTimerService.get('serviceId'));
				this.currentTimerService = null;
			}
		},
		
		getIndexSelected:function(){
			var selected = null;
			if(!this.defaultSelected){
				if(this.timerSelect.selectedIndex === 0)
					return selected;
				else
					return this.timerSelect.selectedIndex -1;
			}
			else{
				return this.timerSelect.selectedIndex;
			}
		}
	});
});