import torrus 1.0.9
[freeside.git] / httemplate / elements / fckeditor / editor / dialog / fck_select / fck_select.js
1 /*\r
2  * FCKeditor - The text editor for Internet - http://www.fckeditor.net\r
3  * Copyright (C) 2003-2010 Frederico Caldeira Knabben\r
4  *\r
5  * == BEGIN LICENSE ==\r
6  *\r
7  * Licensed under the terms of any of the following licenses at your\r
8  * choice:\r
9  *\r
10  *  - GNU General Public License Version 2 or later (the "GPL")\r
11  *    http://www.gnu.org/licenses/gpl.html\r
12  *\r
13  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")\r
14  *    http://www.gnu.org/licenses/lgpl.html\r
15  *\r
16  *  - Mozilla Public License Version 1.1 or later (the "MPL")\r
17  *    http://www.mozilla.org/MPL/MPL-1.1.html\r
18  *\r
19  * == END LICENSE ==\r
20  *\r
21  * Scripts for the fck_select.html page.\r
22  */\r
23 \r
24 function Select( combo )\r
25 {\r
26         var iIndex = combo.selectedIndex ;\r
27 \r
28         oListText.selectedIndex         = iIndex ;\r
29         oListValue.selectedIndex        = iIndex ;\r
30 \r
31         var oTxtText    = document.getElementById( "txtText" ) ;\r
32         var oTxtValue   = document.getElementById( "txtValue" ) ;\r
33 \r
34         oTxtText.value  = oListText.value ;\r
35         oTxtValue.value = oListValue.value ;\r
36 }\r
37 \r
38 function Add()\r
39 {\r
40         var oTxtText    = document.getElementById( "txtText" ) ;\r
41         var oTxtValue   = document.getElementById( "txtValue" ) ;\r
42 \r
43         AddComboOption( oListText, oTxtText.value, oTxtText.value ) ;\r
44         AddComboOption( oListValue, oTxtValue.value, oTxtValue.value ) ;\r
45 \r
46         oListText.selectedIndex = oListText.options.length - 1 ;\r
47         oListValue.selectedIndex = oListValue.options.length - 1 ;\r
48 \r
49         oTxtText.value  = '' ;\r
50         oTxtValue.value = '' ;\r
51 \r
52         oTxtText.focus() ;\r
53 }\r
54 \r
55 function Modify()\r
56 {\r
57         var iIndex = oListText.selectedIndex ;\r
58 \r
59         if ( iIndex < 0 ) return ;\r
60 \r
61         var oTxtText    = document.getElementById( "txtText" ) ;\r
62         var oTxtValue   = document.getElementById( "txtValue" ) ;\r
63 \r
64         oListText.options[ iIndex ].innerHTML   = HTMLEncode( oTxtText.value ) ;\r
65         oListText.options[ iIndex ].value               = oTxtText.value ;\r
66 \r
67         oListValue.options[ iIndex ].innerHTML  = HTMLEncode( oTxtValue.value ) ;\r
68         oListValue.options[ iIndex ].value              = oTxtValue.value ;\r
69 \r
70         oTxtText.value  = '' ;\r
71         oTxtValue.value = '' ;\r
72 \r
73         oTxtText.focus() ;\r
74 }\r
75 \r
76 function Move( steps )\r
77 {\r
78         ChangeOptionPosition( oListText, steps ) ;\r
79         ChangeOptionPosition( oListValue, steps ) ;\r
80 }\r
81 \r
82 function Delete()\r
83 {\r
84         RemoveSelectedOptions( oListText ) ;\r
85         RemoveSelectedOptions( oListValue ) ;\r
86 }\r
87 \r
88 function SetSelectedValue()\r
89 {\r
90         var iIndex = oListValue.selectedIndex ;\r
91         if ( iIndex < 0 ) return ;\r
92 \r
93         var oTxtValue = document.getElementById( "txtSelValue" ) ;\r
94 \r
95         oTxtValue.value = oListValue.options[ iIndex ].value ;\r
96 }\r
97 \r
98 // Moves the selected option by a number of steps (also negative)\r
99 function ChangeOptionPosition( combo, steps )\r
100 {\r
101         var iActualIndex = combo.selectedIndex ;\r
102 \r
103         if ( iActualIndex < 0 )\r
104                 return ;\r
105 \r
106         var iFinalIndex = iActualIndex + steps ;\r
107 \r
108         if ( iFinalIndex < 0 )\r
109                 iFinalIndex = 0 ;\r
110 \r
111         if ( iFinalIndex > ( combo.options.length - 1 ) )\r
112                 iFinalIndex = combo.options.length - 1 ;\r
113 \r
114         if ( iActualIndex == iFinalIndex )\r
115                 return ;\r
116 \r
117         var oOption = combo.options[ iActualIndex ] ;\r
118         var sText       = HTMLDecode( oOption.innerHTML ) ;\r
119         var sValue      = oOption.value ;\r
120 \r
121         combo.remove( iActualIndex ) ;\r
122 \r
123         oOption = AddComboOption( combo, sText, sValue, null, iFinalIndex ) ;\r
124 \r
125         oOption.selected = true ;\r
126 }\r
127 \r
128 // Remove all selected options from a SELECT object\r
129 function RemoveSelectedOptions(combo)\r
130 {\r
131         // Save the selected index\r
132         var iSelectedIndex = combo.selectedIndex ;\r
133 \r
134         var oOptions = combo.options ;\r
135 \r
136         // Remove all selected options\r
137         for ( var i = oOptions.length - 1 ; i >= 0 ; i-- )\r
138         {\r
139                 if (oOptions[i].selected) combo.remove(i) ;\r
140         }\r
141 \r
142         // Reset the selection based on the original selected index\r
143         if ( combo.options.length > 0 )\r
144         {\r
145                 if ( iSelectedIndex >= combo.options.length ) iSelectedIndex = combo.options.length - 1 ;\r
146                 combo.selectedIndex = iSelectedIndex ;\r
147         }\r
148 }\r
149 \r
150 // Add a new option to a SELECT object (combo or list)\r
151 function AddComboOption( combo, optionText, optionValue, documentObject, index )\r
152 {\r
153         var oOption ;\r
154 \r
155         if ( documentObject )\r
156                 oOption = documentObject.createElement("OPTION") ;\r
157         else\r
158                 oOption = document.createElement("OPTION") ;\r
159 \r
160         if ( index != null )\r
161                 combo.options.add( oOption, index ) ;\r
162         else\r
163                 combo.options.add( oOption ) ;\r
164 \r
165         oOption.innerHTML = optionText.length > 0 ? HTMLEncode( optionText ) : '&nbsp;' ;\r
166         oOption.value     = optionValue ;\r
167 \r
168         return oOption ;\r
169 }\r
170 \r
171 function HTMLEncode( text )\r
172 {\r
173         if ( !text )\r
174                 return '' ;\r
175 \r
176         text = text.replace( /&/g, '&amp;' ) ;\r
177         text = text.replace( /</g, '&lt;' ) ;\r
178         text = text.replace( />/g, '&gt;' ) ;\r
179 \r
180         return text ;\r
181 }\r
182 \r
183 \r
184 function HTMLDecode( text )\r
185 {\r
186         if ( !text )\r
187                 return '' ;\r
188 \r
189         text = text.replace( /&gt;/g, '>' ) ;\r
190         text = text.replace( /&lt;/g, '<' ) ;\r
191         text = text.replace( /&amp;/g, '&' ) ;\r
192 \r
193         return text ;\r
194 }\r