source: ether_iasi/trunk/web/resources/js/OpenLayers-2.12/lib/Rico/Corner.js @ 739

Last change on this file since 739 was 739, checked in by vmipsl, 11 years ago

OpenLayers?

File size: 11.6 KB
Line 
1/**
2 * @requires OpenLayers/Console.js
3 * @requires Rico/Color.js
4 */
5
6
7/*
8 * This file has been edited substantially from the Rico-released
9 * version by the OpenLayers development team.
10 * 
11 *  Copyright 2005 Sabre Airline Solutions 
12 * 
13 *  Licensed under the Apache License, Version 2.0 (the "License");
14 *  you may not use this file except in compliance with the
15 *  License. You may obtain a copy of the License at
16 * 
17 *         http://www.apache.org/licenses/LICENSE-2.0 
18 * 
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the * License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or
22 * implied. See the License for the specific language governing
23 * permissions * and limitations under the License.
24 *
25 */
26
27OpenLayers.Console.warn("OpenLayers.Rico is deprecated");
28
29OpenLayers.Rico = OpenLayers.Rico || {};
30OpenLayers.Rico.Corner = {
31
32    round: function(e, options) {
33        e = OpenLayers.Util.getElement(e);
34        this._setOptions(options);
35
36        var color = this.options.color;
37        if ( this.options.color == "fromElement" ) {
38            color = this._background(e);
39        }
40        var bgColor = this.options.bgColor;
41        if ( this.options.bgColor == "fromParent" ) {
42            bgColor = this._background(e.offsetParent);
43        }
44        this._roundCornersImpl(e, color, bgColor);
45    },
46
47    /**   This is a helper function to change the background
48    *     color of <div> that has had Rico rounded corners added.
49    *
50    *     It seems we cannot just set the background color for the
51    *     outer <div> so each <span> element used to create the
52    *     corners must have its background color set individually.
53    *
54    * @param {DOM} theDiv - A child of the outer <div> that was
55    *                        supplied to the `round` method.
56    *
57    * @param {String} newColor - The new background color to use.
58    */
59    changeColor: function(theDiv, newColor) {
60   
61        theDiv.style.backgroundColor = newColor;
62
63        var spanElements = theDiv.parentNode.getElementsByTagName("span");
64       
65        for (var currIdx = 0; currIdx < spanElements.length; currIdx++) {
66            spanElements[currIdx].style.backgroundColor = newColor;
67        }
68    }, 
69
70
71    /**   This is a helper function to change the background
72    *     opacity of <div> that has had Rico rounded corners added.
73    *
74    *     See changeColor (above) for algorithm explanation
75    *
76    * @param {DOM} theDiv A child of the outer <div> that was
77    *                        supplied to the `round` method.
78    *
79    * @param {int} newOpacity The new opacity to use (0-1).
80    */
81    changeOpacity: function(theDiv, newOpacity) {
82   
83        var mozillaOpacity = newOpacity;
84        var ieOpacity = 'alpha(opacity=' + newOpacity * 100 + ')';
85       
86        theDiv.style.opacity = mozillaOpacity;
87        theDiv.style.filter = ieOpacity;
88
89        var spanElements = theDiv.parentNode.getElementsByTagName("span");
90       
91        for (var currIdx = 0; currIdx < spanElements.length; currIdx++) {
92            spanElements[currIdx].style.opacity = mozillaOpacity;
93            spanElements[currIdx].style.filter = ieOpacity;
94        }
95
96    },
97
98    /** this function takes care of redoing the rico cornering
99    *   
100    *    you can't just call updateRicoCorners() again and pass it a
101    *    new options string. you have to first remove the divs that
102    *    rico puts on top and below the content div.
103    *
104    * @param {DOM} theDiv - A child of the outer <div> that was
105    *                        supplied to the `round` method.
106    *
107    * @param {Object} options - list of options
108    */
109    reRound: function(theDiv, options) {
110
111        var topRico = theDiv.parentNode.childNodes[0];
112        //theDiv would be theDiv.parentNode.childNodes[1]
113        var bottomRico = theDiv.parentNode.childNodes[2];
114       
115        theDiv.parentNode.removeChild(topRico);
116        theDiv.parentNode.removeChild(bottomRico); 
117
118        this.round(theDiv.parentNode, options);
119    }, 
120
121   _roundCornersImpl: function(e, color, bgColor) {
122      if(this.options.border) {
123         this._renderBorder(e,bgColor);
124      }
125      if(this._isTopRounded()) {
126         this._roundTopCorners(e,color,bgColor);
127      }
128      if(this._isBottomRounded()) {
129         this._roundBottomCorners(e,color,bgColor);
130      }
131   },
132
133   _renderBorder: function(el,bgColor) {
134      var borderValue = "1px solid " + this._borderColor(bgColor);
135      var borderL = "border-left: "  + borderValue;
136      var borderR = "border-right: " + borderValue;
137      var style   = "style='" + borderL + ";" + borderR +  "'";
138      el.innerHTML = "<div " + style + ">" + el.innerHTML + "</div>";
139   },
140
141   _roundTopCorners: function(el, color, bgColor) {
142      var corner = this._createCorner(bgColor);
143      for(var i=0 ; i < this.options.numSlices ; i++ ) {
144         corner.appendChild(this._createCornerSlice(color,bgColor,i,"top"));
145      }
146      el.style.paddingTop = 0;
147      el.insertBefore(corner,el.firstChild);
148   },
149
150   _roundBottomCorners: function(el, color, bgColor) {
151      var corner = this._createCorner(bgColor);
152      for(var i=(this.options.numSlices-1) ; i >= 0 ; i-- ) {
153         corner.appendChild(this._createCornerSlice(color,bgColor,i,"bottom"));
154      }
155      el.style.paddingBottom = 0;
156      el.appendChild(corner);
157   },
158
159   _createCorner: function(bgColor) {
160      var corner = document.createElement("div");
161      corner.style.backgroundColor = (this._isTransparent() ? "transparent" : bgColor);
162      return corner;
163   },
164
165   _createCornerSlice: function(color,bgColor, n, position) {
166      var slice = document.createElement("span");
167
168      var inStyle = slice.style;
169      inStyle.backgroundColor = color;
170      inStyle.display  = "block";
171      inStyle.height   = "1px";
172      inStyle.overflow = "hidden";
173      inStyle.fontSize = "1px";
174
175      var borderColor = this._borderColor(color,bgColor);
176      if ( this.options.border && n == 0 ) {
177         inStyle.borderTopStyle    = "solid";
178         inStyle.borderTopWidth    = "1px";
179         inStyle.borderLeftWidth   = "0px";
180         inStyle.borderRightWidth  = "0px";
181         inStyle.borderBottomWidth = "0px";
182         inStyle.height            = "0px"; // assumes css compliant box model
183         inStyle.borderColor       = borderColor;
184      }
185      else if(borderColor) {
186         inStyle.borderColor = borderColor;
187         inStyle.borderStyle = "solid";
188         inStyle.borderWidth = "0px 1px";
189      }
190
191      if ( !this.options.compact && (n == (this.options.numSlices-1)) ) {
192         inStyle.height = "2px";
193      }
194      this._setMargin(slice, n, position);
195      this._setBorder(slice, n, position);
196      return slice;
197   },
198
199   _setOptions: function(options) {
200      this.options = {
201         corners : "all",
202         color   : "fromElement",
203         bgColor : "fromParent",
204         blend   : true,
205         border  : false,
206         compact : false
207      };
208      OpenLayers.Util.extend(this.options, options || {});
209
210      this.options.numSlices = this.options.compact ? 2 : 4;
211      if ( this._isTransparent() ) {
212         this.options.blend = false;
213      }
214   },
215
216   _whichSideTop: function() {
217      if ( this._hasString(this.options.corners, "all", "top") ) {
218         return "";
219      }
220      if ( this.options.corners.indexOf("tl") >= 0 && this.options.corners.indexOf("tr") >= 0 ) {
221         return "";
222      }
223      if (this.options.corners.indexOf("tl") >= 0) {
224         return "left";
225      } else if (this.options.corners.indexOf("tr") >= 0) {
226          return "right";
227      }
228      return "";
229   },
230
231   _whichSideBottom: function() {
232      if ( this._hasString(this.options.corners, "all", "bottom") ) {
233         return "";
234      }
235      if ( this.options.corners.indexOf("bl")>=0 && this.options.corners.indexOf("br")>=0 ) {
236         return "";
237      }
238
239      if(this.options.corners.indexOf("bl") >=0) {
240         return "left";
241      } else if(this.options.corners.indexOf("br")>=0) {
242         return "right";
243      }
244      return "";
245   },
246
247   _borderColor : function(color,bgColor) {
248      if ( color == "transparent" ) {
249         return bgColor;
250      } else if ( this.options.border ) {
251         return this.options.border;
252      } else if ( this.options.blend ) {
253         return this._blend( bgColor, color );
254      } else {
255         return "";
256      }
257   },
258
259
260   _setMargin: function(el, n, corners) {
261      var marginSize = this._marginSize(n);
262      var whichSide = corners == "top" ? this._whichSideTop() : this._whichSideBottom();
263
264      if ( whichSide == "left" ) {
265         el.style.marginLeft = marginSize + "px"; el.style.marginRight = "0px";
266      }
267      else if ( whichSide == "right" ) {
268         el.style.marginRight = marginSize + "px"; el.style.marginLeft  = "0px";
269      }
270      else {
271         el.style.marginLeft = marginSize + "px"; el.style.marginRight = marginSize + "px";
272      }
273   },
274
275   _setBorder: function(el,n,corners) {
276      var borderSize = this._borderSize(n);
277      var whichSide = corners == "top" ? this._whichSideTop() : this._whichSideBottom();
278      if ( whichSide == "left" ) {
279         el.style.borderLeftWidth = borderSize + "px"; el.style.borderRightWidth = "0px";
280      }
281      else if ( whichSide == "right" ) {
282         el.style.borderRightWidth = borderSize + "px"; el.style.borderLeftWidth  = "0px";
283      }
284      else {
285         el.style.borderLeftWidth = borderSize + "px"; el.style.borderRightWidth = borderSize + "px";
286      }
287      if (this.options.border != false) {
288        el.style.borderLeftWidth = borderSize + "px"; el.style.borderRightWidth = borderSize + "px";
289      }
290   },
291
292   _marginSize: function(n) {
293      if ( this._isTransparent() ) {
294         return 0;
295      }
296      var marginSizes          = [ 5, 3, 2, 1 ];
297      var blendedMarginSizes   = [ 3, 2, 1, 0 ];
298      var compactMarginSizes   = [ 2, 1 ];
299      var smBlendedMarginSizes = [ 1, 0 ];
300
301      if ( this.options.compact && this.options.blend ) {
302         return smBlendedMarginSizes[n];
303      } else if ( this.options.compact ) {
304         return compactMarginSizes[n];
305      } else if ( this.options.blend ) {
306         return blendedMarginSizes[n];
307      } else {
308         return marginSizes[n];
309      }
310   },
311
312   _borderSize: function(n) {
313      var transparentBorderSizes = [ 5, 3, 2, 1 ];
314      var blendedBorderSizes     = [ 2, 1, 1, 1 ];
315      var compactBorderSizes     = [ 1, 0 ];
316      var actualBorderSizes      = [ 0, 2, 0, 0 ];
317
318      if ( this.options.compact && (this.options.blend || this._isTransparent()) ) {
319         return 1;
320      } else if ( this.options.compact ) {
321         return compactBorderSizes[n];
322      } else if ( this.options.blend ) {
323         return blendedBorderSizes[n];
324      } else if ( this.options.border ) {
325         return actualBorderSizes[n];
326      } else if ( this._isTransparent() ) {
327         return transparentBorderSizes[n];
328      }
329      return 0;
330   },
331
332   _hasString: function(str) { for(var i=1 ; i<arguments.length ; i++) if (str.indexOf(arguments[i]) >= 0) { return true; } return false; },
333   _blend: function(c1, c2) { var cc1 = OpenLayers.Rico.Color.createFromHex(c1); cc1.blend(OpenLayers.Rico.Color.createFromHex(c2)); return cc1; },
334   _background: function(el) { try { return OpenLayers.Rico.Color.createColorFromBackground(el).asHex(); } catch(err) { return "#ffffff"; } },
335   _isTransparent: function() { return this.options.color == "transparent"; },
336   _isTopRounded: function() { return this._hasString(this.options.corners, "all", "top", "tl", "tr"); },
337   _isBottomRounded: function() { return this._hasString(this.options.corners, "all", "bottom", "bl", "br"); },
338   _hasSingleTextChild: function(el) { return el.childNodes.length == 1 && el.childNodes[0].nodeType == 3; }
339};
Note: See TracBrowser for help on using the repository browser.