| 1 |
define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/window", "dojo/_base/sniff"], |
| 2 |
function(lang, ArrayUtil, Window, has) { |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
var dh = lang.getObject("dojox.html", true); |
| 13 |
var dynamicStyleMap = {}; |
| 14 |
var pageStyleSheets = {}; |
| 15 |
var titledSheets = []; |
| 16 |
|
| 17 |
dh.insertCssRule = function(selector, declaration, styleSheetName){ |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
var ss = dh.getDynamicStyleSheet(styleSheetName); |
| 39 |
var styleText = selector + " {" + declaration + "}"; |
| 40 |
console.log("insertRule:", styleText); |
| 41 |
if(has("ie")){ |
| 42 |
|
| 43 |
ss.cssText+=styleText; |
| 44 |
console.log("ss.cssText:", ss.cssText); |
| 45 |
}else if(ss.sheet){ |
| 46 |
ss.sheet.insertRule(styleText, ss._indicies.length); |
| 47 |
}else{ |
| 48 |
ss.appendChild(Window.doc.createTextNode(styleText)); |
| 49 |
} |
| 50 |
ss._indicies.push(selector+" "+declaration); |
| 51 |
return selector; |
| 52 |
}; |
| 53 |
|
| 54 |
dh.removeCssRule = function(selector, declaration, styleSheetName){ |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
var ss; |
| 62 |
var index=-1; |
| 63 |
var nm; |
| 64 |
var i; |
| 65 |
for(nm in dynamicStyleMap){ |
| 66 |
if(styleSheetName && styleSheetName !== nm) {continue;} |
| 67 |
ss = dynamicStyleMap[nm]; |
| 68 |
for(i=0;i<ss._indicies.length;i++){ |
| 69 |
if(selector+" "+declaration === ss._indicies[i]){ |
| 70 |
index = i; |
| 71 |
break; |
| 72 |
} |
| 73 |
} |
| 74 |
if(index>-1) { break; } |
| 75 |
} |
| 76 |
if(!ss){ |
| 77 |
console.warn("No dynamic style sheet has been created from which to remove a rule."); |
| 78 |
return false; |
| 79 |
} |
| 80 |
if(index===-1){ |
| 81 |
console.warn("The css rule was not found and could not be removed."); |
| 82 |
return false; |
| 83 |
} |
| 84 |
ss._indicies.splice(index, 1); |
| 85 |
if(has("ie")){ |
| 86 |
|
| 87 |
ss.removeRule(index); |
| 88 |
}else if(ss.sheet){ |
| 89 |
ss.sheet.deleteRule(index); |
| 90 |
} |
| 91 |
return true; |
| 92 |
}; |
| 93 |
|
| 94 |
dh.modifyCssRule = function(selector, declaration, styleSheetName){ |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
}; |
| 102 |
|
| 103 |
dh.getStyleSheet = function(styleSheetName){ |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
if(dynamicStyleMap[styleSheetName || "default"]){ |
| 117 |
return dynamicStyleMap[styleSheetName || "default"]; |
| 118 |
} |
| 119 |
if(!styleSheetName){ |
| 120 |
|
| 121 |
|
| 122 |
return false; |
| 123 |
} |
| 124 |
var allSheets = dh.getStyleSheets(); |
| 125 |
|
| 126 |
if(allSheets[styleSheetName]){ |
| 127 |
return dh.getStyleSheets()[styleSheetName]; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
var nm; |
| 132 |
for ( nm in allSheets){ |
| 133 |
if( allSheets[nm].href && allSheets[nm].href.indexOf(styleSheetName)>-1){ |
| 134 |
return allSheets[nm]; |
| 135 |
} |
| 136 |
} |
| 137 |
return false; |
| 138 |
}; |
| 139 |
|
| 140 |
dh.getDynamicStyleSheet = function(styleSheetName){ |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if(!styleSheetName){ styleSheetName="default"; } |
| 150 |
if(!dynamicStyleMap[styleSheetName]){ |
| 151 |
if(Window.doc.createStyleSheet){ |
| 152 |
dynamicStyleMap[styleSheetName] = Window.doc.createStyleSheet(); |
| 153 |
if(has("ie") < 9) { |
| 154 |
|
| 155 |
dynamicStyleMap[styleSheetName].title = styleSheetName; |
| 156 |
} |
| 157 |
}else{ |
| 158 |
dynamicStyleMap[styleSheetName] = Window.doc.createElement("style"); |
| 159 |
dynamicStyleMap[styleSheetName].setAttribute("type", "text/css"); |
| 160 |
Window.doc.getElementsByTagName("head")[0].appendChild(dynamicStyleMap[styleSheetName]); |
| 161 |
console.log(styleSheetName, " ss created: ", dynamicStyleMap[styleSheetName].sheet); |
| 162 |
} |
| 163 |
dynamicStyleMap[styleSheetName]._indicies = []; |
| 164 |
} |
| 165 |
return dynamicStyleMap[styleSheetName]; |
| 166 |
}; |
| 167 |
|
| 168 |
dh.enableStyleSheet = function(styleSheetName){ |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
var ss = dh.getStyleSheet(styleSheetName); |
| 174 |
if(ss){ |
| 175 |
if(ss.sheet){ |
| 176 |
ss.sheet.disabled = false; |
| 177 |
}else{ |
| 178 |
ss.disabled = false; |
| 179 |
} |
| 180 |
} |
| 181 |
}; |
| 182 |
|
| 183 |
dh.disableStyleSheet = function(styleSheetName){ |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
var ss = dh.getStyleSheet(styleSheetName); |
| 189 |
if(ss){ |
| 190 |
if(ss.sheet){ |
| 191 |
ss.sheet.disabled = true; |
| 192 |
}else{ |
| 193 |
ss.disabled = true; |
| 194 |
} |
| 195 |
} |
| 196 |
}; |
| 197 |
|
| 198 |
dh.activeStyleSheet = function(title){ |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
var sheets = dh.getToggledStyleSheets(); |
| 208 |
var i; |
| 209 |
if(arguments.length === 1){ |
| 210 |
|
| 211 |
ArrayUtil.forEach(sheets, function(s){ |
| 212 |
s.disabled = (s.title === title) ? false : true; |
| 213 |
}); |
| 214 |
}else{ |
| 215 |
for(i=0;i<sheets.length;i++){ |
| 216 |
if(sheets[i].disabled === false){ |
| 217 |
return sheets[i]; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
return true; |
| 222 |
}; |
| 223 |
|
| 224 |
dh.getPreferredStyleSheet = function(){ |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
dh.getToggledStyleSheets = function(){ |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
var nm; |
| 244 |
if(!titledSheets.length){ |
| 245 |
var sObjects = dh.getStyleSheets(); |
| 246 |
for(nm in sObjects){ |
| 247 |
if(sObjects[nm].title){ |
| 248 |
titledSheets.push(sObjects[nm]); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
return titledSheets; |
| 253 |
}; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
dh.getStyleSheets = function(){ |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
if(pageStyleSheets.collected) {return pageStyleSheets;} |
| 264 |
var sheets = Window.doc.styleSheets; |
| 265 |
ArrayUtil.forEach(sheets, function(n){ |
| 266 |
var s = (n.sheet) ? n.sheet : n; |
| 267 |
var name = s.title || s.href; |
| 268 |
if(has("ie")){ |
| 269 |
|
| 270 |
if(s.cssText.indexOf("#default#VML") === -1){ |
| 271 |
if(s.href){ |
| 272 |
|
| 273 |
pageStyleSheets[name] = s; |
| 274 |
}else if(s.imports.length){ |
| 275 |
|
| 276 |
ArrayUtil.forEach(s.imports, function(si){ |
| 277 |
pageStyleSheets[si.title || si.href] = si; |
| 278 |
}); |
| 279 |
}else{ |
| 280 |
|
| 281 |
pageStyleSheets[name] = s; |
| 282 |
} |
| 283 |
} |
| 284 |
}else{ |
| 285 |
|
| 286 |
pageStyleSheets[name] = s; |
| 287 |
pageStyleSheets[name].id = s.ownerNode.id; |
| 288 |
var rules = []; |
| 289 |
try { |
| 290 |
rules = s[s.cssRules?"cssRules":"rules"]; |
| 291 |
} catch(err) { |
| 292 |
|
| 293 |
console.warn("Reading css rules from stylesheet "+s.href+" is forbidden due to same-origin policy. See http://www.w3.org/TR/CSP/#cascading-style-sheet-css-parsing",s); |
| 294 |
} |
| 295 |
ArrayUtil.forEach(rules, function(r){ |
| 296 |
if(r.href){ |
| 297 |
|
| 298 |
pageStyleSheets[r.href] = r.styleSheet; |
| 299 |
pageStyleSheets[r.href].id = s.ownerNode.id; |
| 300 |
} |
| 301 |
}); |
| 302 |
} |
| 303 |
}); |
| 304 |
|
| 305 |
pageStyleSheets.collected = true; |
| 306 |
return pageStyleSheets; |
| 307 |
}; |
| 308 |
|
| 309 |
return dh; |
| 310 |
}); |