Skip to content

Commit

Permalink
updated to be object oriented
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityblast committed Mar 22, 2012
1 parent 59d0586 commit 7104252
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#StyleRule

StyleRule adds the ability to manage css style rules with javascript.
Cross-browser css style rules management from javascript.

##Example

var index_red = StyleRule.add("body", "background: red;");
var index_yellow = StyleRule.add("body", "background: yellow;");
var index_blue = StyleRule.add("body", "background: blue;");
var index_brown = StyleRule.add("body", "background: brown;");

var styleRule = new StyleRule();
var index_red = styleRule.add("body", "background: red;");
var index_yellow = styleRule.add("body", "background: yellow;");
var index_blue = styleRule.add("body", "background: blue;");
var index_brown = styleRule.add("body", "background: brown;");

StyleRule.replace(index_blue, "body", "background: violet;");
StyleRule.remove(index_yellow);
styleRule.replace(index_blue, "body", "background: violet;");
styleRule.remove(index_yellow);
20 changes: 8 additions & 12 deletions src/style_rule.coffee
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
class StyleRule
@init: ->
constructor: ->
@styleNode = @_buildStyleNode()
@style = @styleNode[if @styleNode.sheet then 'sheet' else 'styleSheet']
@cssRulesProperty = if @style.rules then "rules" else "cssRules"
@removeMethodName = if @style.removeRule then "removeRule" else "deleteRule"
@insertRuleMethod = if @style.insertRule then @_insertRule else @_addRule

@_buildStyleNode: ->
_buildStyleNode: ->
styleNode = document.createElement "style"
styleNode.type = "text/css"
styleNode.rel = "alternate stylesheet"
head = document.head or document.getElementsByTagName("head")[0] or document.documentElement
head.appendChild styleNode

@_addRule: (selector, declaration, index) ->
_addRule: (selector, declaration, index) ->
index = index or @getCssRules().length
@style.addRule selector, declaration, index
index

@_insertRule: (selector, declaration, index) ->
_insertRule: (selector, declaration, index) ->
index = index or @getCssRules().length
@style.insertRule "#{selector} { #{declaration} }", index
index

@getCssRules: ->
@init() if not @style?
getCssRules: ->
@style[@cssRulesProperty]

@add: (selector, declaration, index) ->
@init() if not @style?
add: (selector, declaration, index) ->
@insertRuleMethod.call @, selector, declaration, index

@remove: (index) ->
@init() if not @style?
remove: (index) ->
index = index or @getCssRules().length - 1
@style[@removeMethodName].call @style, index

@replace: (index, selector, declaration) ->
@init() if not @style?
replace: (index, selector, declaration) ->
@style[@removeMethodName].call @style, index
@add selector, declaration, index

Expand Down

0 comments on commit 7104252

Please sign in to comment.