Merge branch 'master' into history
[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         width="100%"
9         toolTip="{fieldDescription}"
10         label="{fieldName}:"
11         styleName="titledEditor">
12
13     <mx:HSlider id="inputSlider"
14                 minimum="{sliderMinimum}" maximum="{sliderMaximum}"
15                 snapInterval="{sliderSnapInterval}" tickInterval="{sliderSnapInterval}"
16                 labels="{sliderLabels}" dataTipFormatFunction="formatDataTip"
17                 value="{translatedValue}"
18                 enabled="{isValueValid}"
19                 liveDragging="true"
20                 change="setTagValueFromSlider()" />
21
22   <fx:Script><![CDATA[
23
24     private function setTagValueFromSlider():void {
25         // unset the tag if being set to the default
26         var newValue:Number = inputSlider.value;
27         value = newValue == SliderEditorFactory(_factory).defaultValue ? null : newValue.toString()
28     }
29
30     private function formatDataTip(value:Number):String {
31         if ( value == inputSlider.minimum )
32             return inputSlider.labels[0];
33         if ( value == inputSlider.maximum )
34             return inputSlider.labels[inputSlider.labels.length-1];
35         if ( value == SliderEditorFactory(_factory).defaultValue )
36             return SliderEditorFactory(_factory).defaultValueName
37         return value.toString();
38     }
39
40     [Bindable(event="factory_set")]
41     public function get sliderMinimum():Number {
42         return SliderEditorFactory(_factory) == null ? 0 : SliderEditorFactory(_factory).minimum;
43     }
44
45     [Bindable(event="factory_set")]
46     public function get sliderMaximum():Number {
47         return SliderEditorFactory(_factory) == null ? 100 : SliderEditorFactory(_factory).maximum;
48     }
49
50     [Bindable(event="factory_set")]
51     public function get sliderSnapInterval():Number {
52         return SliderEditorFactory(_factory) == null ? 1 : SliderEditorFactory(_factory).snapInterval;
53     }
54
55     [Bindable(event="factory_set")]
56     public function get sliderLabels():Array {
57         return SliderEditorFactory(_factory) == null ? ["0","100"] : SliderEditorFactory(_factory).labels;
58     }
59
60     [Bindable(event="tag_changed")]
61     private function get translatedValue():Number {
62         var validatedValue:Number = getValidatedValue();
63         return isNaN(validatedValue) ? SliderEditorFactory(_factory).defaultValue : validatedValue;
64     }
65
66     [Bindable(event="tag_changed")]
67     private function get isValueValid():Boolean {
68         var validatedValue:Number = getValidatedValue();
69         return !isNaN(validatedValue);
70     }
71
72     private function getValidatedValue():Number {
73         var valueStr:String = value;
74         if ( valueStr == null || valueStr == "" )
75             return SliderEditorFactory(_factory).defaultValue;
76
77         var parsed:Number = parseFloat(value);
78         if ( isNaN(parsed) || parsed < sliderMinimum || parsed > sliderMaximum )
79             return Number.NaN;
80         return parsed;
81     }
82         
83   ]]></fx:Script>
84 </edit:SingleTagEditor>
85