more
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / editors / SliderEditor.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <edit:SingleTagEditor
3         xmlns:fx="http://ns.adobe.com/mxml/2009"
4         xmlns:mx="library://ns.adobe.com/flex/mx"
5     xmlns:s="library://ns.adobe.com/flex/spark"
6         xmlns:edit="net.systemeD.potlatch2.mapfeatures.editors.*"
7         xmlns:flexlib="flexlib.controls.*"
8         verticalGap="0"
9         width="100%"
10         toolTip="{fieldDescription}"
11     direction="horizontal" styleName="titledEditor">
12
13     <s:FormItem label="{fieldName}">
14       <mx:HSlider id="inputSlider"
15                   minimum="{sliderMinimum}" maximum="{sliderMaximum}"
16                   snapInterval="{sliderSnapInterval}" tickInterval="{sliderSnapInterval}"
17                   labels="{sliderLabels}" dataTipFormatFunction="formatDataTip"
18                   value="{translatedValue}"
19                   enabled="{isValueValid}"
20                   liveDragging="true"
21                   change="setTagValueFromSlider()" />
22     </s:FormItem>
23
24   <fx:Script><![CDATA[
25
26     private function setTagValueFromSlider():void {
27         // unset the tag if being set to the default
28         var newValue:Number = inputSlider.value;
29         value = newValue == SliderEditorFactory(_factory).defaultValue ? null : newValue.toString()
30     }
31
32     private function formatDataTip(value:Number):String {
33         if ( value == inputSlider.minimum )
34             return inputSlider.labels[0];
35         if ( value == inputSlider.maximum )
36             return inputSlider.labels[inputSlider.labels.length-1];
37         if ( value == SliderEditorFactory(_factory).defaultValue )
38             return SliderEditorFactory(_factory).defaultValueName
39         return value.toString();
40     }
41
42     [Bindable(event="factory_set")]
43     public function get sliderMinimum():Number {
44         return SliderEditorFactory(_factory) == null ? 0 : SliderEditorFactory(_factory).minimum;
45     }
46
47     [Bindable(event="factory_set")]
48     public function get sliderMaximum():Number {
49         return SliderEditorFactory(_factory) == null ? 100 : SliderEditorFactory(_factory).maximum;
50     }
51
52     [Bindable(event="factory_set")]
53     public function get sliderSnapInterval():Number {
54         return SliderEditorFactory(_factory) == null ? 1 : SliderEditorFactory(_factory).snapInterval;
55     }
56
57     [Bindable(event="factory_set")]
58     public function get sliderLabels():Array {
59         return SliderEditorFactory(_factory) == null ? ["0","100"] : SliderEditorFactory(_factory).labels;
60     }
61
62     [Bindable(event="tag_changed")]
63     private function get translatedValue():Number {
64         var validatedValue:Number = getValidatedValue();
65         return isNaN(validatedValue) ? SliderEditorFactory(_factory).defaultValue : validatedValue;
66     }
67
68     [Bindable(event="tag_changed")]
69     private function get isValueValid():Boolean {
70         var validatedValue:Number = getValidatedValue();
71         return !isNaN(validatedValue);
72     }
73
74     private function getValidatedValue():Number {
75         var valueStr:String = value;
76         if ( valueStr == null || valueStr == "" )
77             return SliderEditorFactory(_factory).defaultValue;
78
79         var parsed:Number = parseFloat(value);
80         if ( isNaN(parsed) || parsed < sliderMinimum || parsed > sliderMaximum )
81             return Number.NaN;
82         return parsed;
83     }
84         
85   ]]></fx:Script>
86 </edit:SingleTagEditor>
87