Created
March 16, 2018 14:31
-
-
Save mistergibson/0393c96978fd2b1a09d6b0612af191f0 to your computer and use it in GitHub Desktop.
CSS Def. Alteration with Opal 0.11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Css | |
@@CssCache = [] | |
@@CssRuleProperties = {} | |
# | |
class CSSRule | |
# Provide EASY interface to native CSS Rule. | |
def initialize(the_native_rule) | |
@native_rule = the_native_rule | |
@name = (`#{@native_rule}.selectorText` || "").to_s.to_sym | |
end | |
# | |
def native_object() | |
@native_rule | |
end | |
# | |
def inspect() | |
"<CSSRule: #{@name}>" | |
end | |
# | |
def name() | |
@name | |
end | |
# | |
def keys() | |
GxG::Css::valid_properties() | |
end | |
# | |
def [](the_key) | |
result = nil | |
csskey = @@CssRuleProperties[(the_key)] | |
if csskey | |
result = `#{@native_rule}.style[#{csskey}]` | |
end | |
result | |
end | |
# | |
def []=(the_key, the_value) | |
# | |
csskey = @@CssRuleProperties[(the_key)] | |
if csskey | |
`#{@native_rule}.style[#{csskey}] = #{(the_value || "")}` | |
else | |
# ignore invalid keys | |
end | |
the_value | |
end | |
# | |
def merge(properties={}) | |
properties.keys.each do |the_key| | |
self[(the_key)] = properties[(the_key)] | |
end | |
self | |
end | |
# | |
end | |
# | |
def self.refresh_css_cache() | |
sheets = `document.styleSheets.length`.to_i | |
if sheets > 0 | |
new_cache = [] | |
(0..(sheets - 1)).each do |sheet_index| | |
begin | |
ruleset = `document.styleSheets[#{sheet_index}].cssRules` | |
rescue Exception => the_error | |
ruleset = `document.styleSheets[#{sheet_index}].rules` | |
end | |
if ruleset | |
rules = (`#{ruleset}.length`.to_i) | |
if rules > 0 | |
(0..(rules - 1)).each do |rule_index| | |
begin | |
native_rule = `document.styleSheets[#{sheet_index}].cssRules[#{rule_index}]` | |
rescue Exception => the_error | |
native_rule = `document.styleSheets[#{sheet_index}].rules[#{rule_index}]` | |
end | |
if native_rule | |
new_cache << GxG::Css::CSSRule.new(native_rule) | |
if @@CssRuleProperties.size == 0 | |
`Object.keys(#{native_rule}.style)`.each do |the_property_label| | |
if the_property_label | |
if ["0", "1", "3", "4", "5", "6", "7", "8", "9"].include?(the_property_label[0]) | |
the_key = the_property_label.to_sym | |
else | |
key_text = (the_property_label[0].capitalize + the_property_label[1..-1]).split_camelcase | |
if key_text.is_a?(String) | |
key_text = [(key_text)] | |
end | |
the_key = key_text.join("-").downcase.to_sym | |
end | |
unless @@CssRuleProperties.keys.include?(the_key) | |
@@CssRuleProperties[(the_key)] = the_property_label.to_s | |
end | |
end | |
end | |
end | |
# | |
end | |
end | |
end | |
end | |
end | |
@@CssCache = new_cache | |
end | |
true | |
end | |
# | |
def self.valid_properties() | |
@@CssRuleProperties.keys | |
end | |
# | |
def self.rules() | |
result = [] | |
@@CssCache.each do |entry| | |
result << entry.name | |
end | |
result | |
end | |
# | |
def self.find_rule(the_rule_selector=nil) | |
result = [] | |
@@CssCache.each do |entry| | |
if entry.name == (the_rule_selector.to_s.to_sym) | |
result << entry | |
end | |
end | |
result | |
end | |
# | |
def self.set_rule(the_rule_selector=nil, the_properties={}, options={:merge => true}) | |
# find existing | |
# overwrite or merge (options) | |
# create if non-existent | |
found = GxG::Css::find_rule(the_rule_selector) | |
if found.size > 0 | |
found.each do |the_rule_entry| | |
the_rule = the_rule_entry.values[0] | |
if options[:overwrite] == true | |
@@CssRuleProperties.keys.each do |clear_key| | |
unless the_properties.keys.include?(clear_key) | |
`#{the_rule.native_object()}.style[#{@@CssRuleProperties[(clear_key)]}] = ""` | |
end | |
end | |
end | |
the_properties.keys.each do |the_key| | |
csskey = @@CssRuleProperties[(the_key)] | |
if csskey | |
`#{the_rule.native_object()}.style[#{csskey}] = #{the_properties[(the_key)]}` | |
else | |
# ignore invalid keys | |
end | |
end | |
end | |
else | |
# create it | |
rule_body = {} | |
the_properties.keys.each do |property_key| | |
csskey = @@CssRuleProperties[(property_key)] | |
if csskey | |
rule_body[(csskey)] = the_properties[(property_key)] | |
end | |
end | |
sheet_index = (`document.styleSheets.length`.to_i - 1) | |
rules = `document.styleSheets[#{sheet_index}].cssRules` | |
until rules do | |
sheet_index -= 1 | |
if sheet_index < 0 | |
rules = nil | |
break | |
end | |
rules = `document.styleSheets[#{sheet_index}].cssRules` | |
end | |
if rules | |
# | |
rule_index = (`#{rules}.length`.to_i - 1) | |
response_code = `document.styleSheets[#{sheet_index}].addRule(#{the_rule_selector},"",#{rule_index})`.to_i | |
if response_code == -1 | |
begin | |
native_rule = `document.styleSheets[#{sheet_index}].cssRules[#{rule_index}]` | |
rescue Exception => the_error | |
native_rule = `document.styleSheets[#{sheet_index}].rules[#{rule_index}]` | |
end | |
the_properties.keys.each do |property_key| | |
csskey = @@CssRuleProperties[(property_key)] | |
if csskey | |
`#{native_rule}.style[#{csskey}] = #{the_properties[(property_key)]}` | |
end | |
end | |
@@CssCache << GxG::Css::CSSRule.new(native_rule) | |
end | |
end | |
end | |
true | |
end | |
# | |
def self.remove_rule(the_rule_selector=nil) | |
found_indexes = [] | |
@@CssCache.each_with_index do |entry, index| | |
if entry.name == the_rule_selector.to_s.to_sym | |
found_indexes << index | |
end | |
end | |
found = [] | |
found_indexes.reverse.each do |the_index| | |
found << @@CssCache.delete_at(the_index) | |
end | |
if found.size > 0 | |
found.each do |the_rule| | |
native_rule = the_rule.native_object() | |
the_sheet = `#{native_rule}.parentStyleSheet` | |
if the_sheet | |
rule_count = `#{the_sheet}.cssRules.length`.to_i | |
if rule_count > 0 | |
(0..(rule_count - 1)).to_a.reverse.each do |rule_index| | |
other_native_rule = `#{the_sheet}.cssRules[#{rule_index}]` | |
if other_native_rule | |
if `#{native_rule}.selectorText == #{other_native_rule}.selectorText` | |
`#{the_sheet}.deleteRule(#{rule_index})` | |
end | |
end | |
end | |
end | |
end | |
end | |
true | |
else | |
false | |
end | |
end | |
# | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment