View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.vaadin.gwt.client.layout;
35  
36  import com.google.gwt.core.client.JavaScriptObject;
37  
38  /**
39   * Utility class for fetching CSS properties from DOM StyleSheets JS object and
40   * for creating new CSS rules dynamically.
41   *
42   * @deprecated since 5.3.10, not needed anymore.
43   */
44  @Deprecated
45  public class CssRule {
46  
47      private final String selector;
48      private final JavaScriptObject rules = null;
49  
50      private CssRule(final String selector) {
51          this.selector = selector;
52      }
53  
54      /**
55       * @param selector the CSS selector to search for in the stylesheets
56       * @param deep should the search follow any @import statements?
57       */
58      public CssRule(final String selector, final boolean deep) {
59          this.selector = selector;
60          fetchRule(selector, deep);
61      }
62  
63      private native void fetchRule(final String selector, final boolean deep)
64      /*-{
65          var sheets = $doc.styleSheets;
66          for(var i = 0; i < sheets.length; i++) {
67              var sheet = sheets[i];
68              if(sheet.href && sheet.href.indexOf("VAADIN/themes")>-1) {
69                  this.@info.magnolia.ui.vaadin.gwt.client.layout.CssRule::rules = @info.magnolia.ui.vaadin.gwt.client.layout.CssRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Z)(sheet, selector, deep);
70                  return;
71              }
72          }
73          this.@info.magnolia.ui.vaadin.gwt.client.layout.CssRule::rules = [];
74      }-*/;
75  
76      /*
77       * Loops through all current style rules and collects all matching to
78       * 'rules' array. The array is reverse ordered (last one found is first).
79       */
80      private static native JavaScriptObject searchForRule(
81              final JavaScriptObject sheet, final String selector,
82              final boolean deep)
83      /*-{
84          if(!$doc.styleSheets)
85              return null;
86  
87          selector = selector.toLowerCase();
88  
89          var allMatches = [];
90  
91          // IE handles imported sheet differently
92          if(deep && sheet.imports.length > 0) {
93              for(var i=0; i < sheet.imports.length; i++) {
94                  var imports = @info.magnolia.ui.vaadin.gwt.client.layout.CssRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Z)(sheet.imports[i], selector, deep);
95                  allMatches.concat(imports);
96              }
97          }
98  
99          var theRules = new Array();
100         if (sheet.cssRules)
101             theRules = sheet.cssRules
102         else if (sheet.rules)
103             theRules = sheet.rules
104 
105         var j = theRules.length;
106         for(var i=0; i<j; i++) {
107             var r = theRules[i];
108             if(r.type == 1 || sheet.imports) {
109                 var selectors = r.selectorText.toLowerCase().split(",");
110                 var n = selectors.length;
111                 for(var m=0; m<n; m++) {
112                     if(selectors[m].replace(/^\s+|\s+$/g, "") == selector) {
113                         allMatches.unshift(r);
114                         break; // No need to loop other selectors for this rule
115                     }
116                 }
117             } else if(deep && r.type == 3) {
118                 // Search @import stylesheet
119                 var imports = @info.magnolia.ui.vaadin.gwt.client.layout.CssRule::searchForRule(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Z)(r.styleSheet, selector, deep);
120                 allMatches.concat(imports);
121             }
122         }
123 
124         return allMatches;
125     }-*/;
126 
127     /**
128      * Returns a specific property value from this CSS rule.
129      *
130      * @param propertyName camelCase CSS property name
131      * @return the value of the property as a String
132      */
133     public native String getProperty(final String propertyName)
134     /*-{
135         var j = this.@info.magnolia.ui.vaadin.gwt.client.layout.CssRule::rules.length;
136         for(var i=0; i<j; i++){
137             var value = this.@info.magnolia.ui.vaadin.gwt.client.layout.CssRule::rules[i].style[propertyName];
138         if(value)
139             return value;
140         }
141         return null;
142     }-*/;
143 
144     /**
145      * Sets a specific property value for this CSS rule.
146      *
147      * @param propertyName camelCase CSS property name
148      * @param propertyValue the value of the property as a String
149      */
150     public native void setProperty(final String propertyName,
151                                    final String propertyValue)
152     /*-{
153         this.@info.magnolia.ui.vaadin.gwt.client.layout.CssRule::rules[0].style[propertyName] = propertyValue;
154     }-*/;
155 
156     public String getSelector() {
157         return selector;
158     }
159 
160     public static CssRule create(String selector) {
161         CssRule newRule = new CssRule(selector);
162         createRule(selector, newRule);
163         return newRule;
164     }
165 
166     private static native void createRule(final String selector, CssRule rule)
167     /*-{
168     var sheets = $doc.styleSheets;
169     for(var i = 0; i < sheets.length; i++) {
170         var sheet = sheets[i];
171         if(sheet.href && sheet.href.indexOf("VAADIN/themes")>-1) {
172             if(sheet.insertRule) {
173                 sheet.insertRule(selector + "{}", sheet.cssRules.length);
174                 var r = sheet.cssRules[sheet.cssRules.length-1];
175             } else { // IE
176                 sheet.addRule(selector, "foo:bar");
177                 var r = sheet.rules[sheet.rules.length-1];
178             }
179             rule.@info.magnolia.ui.vaadin.gwt.client.layout.CssRule::rules = [r];
180         }
181     }
182     }-*/;
183 
184 }