Debian Codegraph @

dojox/mobile/deviceTheme.js

raw symbols SCIP
1 (typeof define === "undefined" ? function(deps, def) { def(); } : define)([
2 "dojo/_base/config",
3 "dojo/_base/lang",
4 "dojo/_base/window",
5 "require"
6 ], function(config, lang, win, require){
7
8 // module:
9 // dojox/mobile/deviceTheme
10
11 var dm = lang && lang.getObject("dojox.mobile", true) || {};
12
13 var DeviceTheme = function(){
14 // summary:
15 // Automatic theme loader.
16 // description:
17 // This module detects the user agent of the browser and loads the
18 // appropriate theme files. It can be enabled by simply including
19 // the dojox/mobile/deviceTheme script in your application as follows:
20 //
21 // | <script src="dojox/mobile/deviceTheme.js"></script>
22 // | <script src="dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script>
23 //
24 // Using the script tag as above is the recommended way to load the
25 // deviceTheme. Trying to load it using the AMD loader can lead to styles
26 // being applied too late, because the loading of the theme files would
27 // be performed asynchronously by the browser, so you could not assume
28 // that the loading has been completed when your widgets are initialized.
29 // However, loading deviceTheme using the script tag has the drawback that
30 // deviceTheme.js cannot be included in a build.
31 //
32 // You can also pass an additional query parameter string:
33 // theme={theme id} to force a specific theme through the browser
34 // URL input. The available theme ids are Android, Holodark (theme introduced in Android 3.0),
35 // BlackBerry, Custom, iPhone, and iPad. The theme names are case-sensitive. If the given
36 // id does not match, the iOS7 theme is used.
37 //
38 // | http://your.server.com/yourapp.html // automatic detection
39 // | http://your.server.com/yourapp.html?theme=Android // forces Android theme
40 // | http://your.server.com/yourapp.html?theme=Holodark // forces Holodark theme
41 // | http://your.server.com/yourapp.html?theme=BlackBerry // forces Blackberry theme
42 // | http://your.server.com/yourapp.html?theme=Custom // forces Custom theme
43 // | http://your.server.com/yourapp.html?theme=iPhone // forces iPhone theme
44 // | http://your.server.com/yourapp.html?theme=iPad // forces iPad theme
45 // | http://your.server.com/yourapp.html?theme=ios7 // forces iOS 7 theme
46 //
47 // To simulate a particular device from the application code, the user agent
48 // can be forced by setting dojoConfig.mblUserAgent as follows:
49 //
50 // | <script src="dojox/mobile/deviceTheme.js" data-dojo-config="mblUserAgent: 'Holodark'"></script>
51 // | <script src="dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script>
52 //
53 // By default, an all-in-one theme file (e.g. themes/ios7/ios7.css) is
54 // loaded. The all-in-one theme files contain style sheets for all the
55 // dojox/mobile widgets regardless of whether they are used in your
56 // application or not.
57 //
58 // If you want to choose what theme files to load, you can specify them
59 // via dojoConfig or data-dojo-config as shown in the following example:
60 //
61 // | <script src="dojox/mobile/deviceTheme.js"
62 // | data-dojo-config="mblThemeFiles:['base','Button']"></script>
63 // | <script src="dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script>
64 //
65 // In the case of this example, if iphone is detected, for example, the
66 // following files will be loaded:
67 //
68 // | dojox/mobile/themes/ios7/base.css
69 // | dojox/mobile/themes/ios7/Button.css
70 //
71 // If you want to load style sheets for your own custom widgets, you can
72 // specify a package name along with a theme file name in an array.
73 //
74 // | ['base',['com.acme','MyWidget']]
75 //
76 // In this case, the following files will be loaded.
77 //
78 // | dojox/mobile/themes/ios7/base.css
79 // | com/acme/themes/ios7/MyWidget.css
80 //
81 // If you specify '@theme' as a theme file name, it will be replaced with
82 // the theme folder name (e.g. 'ios7'). For example,
83 //
84 // | ['@theme',['com.acme','MyWidget']]
85 //
86 // will load the following files:
87 //
88 // | dojox/mobile/themes/ios7/ios7.css
89 // | com/acme/themes/ios7/MyWidget.css
90
91 if(!win){
92 win = window;
93 win.doc = document;
94 win._no_dojo_dm = dm;
95 }
96 config = config || win.mblConfig || {};
97 var scripts = win.doc.getElementsByTagName("script");
98 for(var i = 0; i < scripts.length; i++){
99 var n = scripts[i];
100 var src = n.getAttribute("src") || "";
101 if(src.match(/\/deviceTheme\.js/i)){
102 config.baseUrl = src.replace("deviceTheme\.js", "../../dojo/");
103 var conf = (n.getAttribute("data-dojo-config") || n.getAttribute("djConfig"));
104 if(conf){
105 var obj = eval("({ " + conf + " })");
106 for(var key in obj){
107 config[key] = obj[key];
108 }
109 }
110 break;
111 }else if(src.match(/\/dojo\.js/i)){
112 config.baseUrl = src.replace("dojo\.js", "");
113 break;
114 }
115 }
116
117 this.loadCssFile = function(/*String*/file){
118 // summary:
119 // Loads the given CSS file programmatically.
120 var link = win.doc.createElement("link");
121 link.href = file;
122 link.type = "text/css";
123 link.rel = "stylesheet";
124 var head = win.doc.getElementsByTagName('head')[0];
125 head.insertBefore(link, head.firstChild);
126 dm.loadedCssFiles.push(link);
127 };
128
129 this.toUrl = function(/*String*/path){
130 // summary:
131 // A wrapper for require.toUrl to support non-dojo usage.
132 return require ? require.toUrl(path) : config.baseUrl + "../" + path;
133 };
134
135 this.setDm = function(/*Object*/_dm){
136 // summary:
137 // Replaces the dojox/mobile object.
138 // description:
139 // When this module is loaded from a script tag, dm is a plain
140 // local object defined at the beginning of this module.
141 // common.js will replace the local dm object with the
142 // real dojox/mobile object through this method.
143 dm = _dm;
144 };
145
146 this.themeMap = config.themeMap || [
147 // summary:
148 // A map of user-agents to theme files.
149 // description:
150 // The first array element is a regexp pattern that matches the
151 // userAgent string.
152 //
153 // The second array element is a theme folder name.
154 //
155 // The third array element is an array of css file paths to load.
156 //
157 // The matching is performed in the array order, and stops after the
158 // first match.
159 [
160 "Holodark",
161 "holodark",
162 []
163 ],
164 [
165 "Android [3-9]",
166 "holodark",
167 []
168 ],
169 [
170 "Android",
171 "android",
172 []
173 ],
174 [
175 "BlackBerry",
176 "blackberry",
177 []
178 ],
179 [
180 "BB10",
181 "blackberry",
182 []
183 ],
184 [
185 "ios7",
186 "ios7",
187 []
188 ],
189 [
190 "iPhone;.*OS ([7-9]|1[0-9])_",
191 "ios7",
192 []
193 ],
194 [
195 "iPad;.*OS ([7-9]|1[0-9])_",
196 "ios7",
197 []
198 ],
199 [
200 "iPhone",
201 "iphone",
202 []
203 ],
204 [
205 "iPad",
206 "iphone",
207 [this.toUrl("dojox/mobile/themes/iphone/ipad.css")]
208 ],
209 [
210 "WindowsPhone",
211 "windows",
212 []
213 ],
214 [
215 "Windows Phone",
216 "windows",
217 []
218 ],
219 [
220 "Trident",
221 "ios7",
222 []
223 ],
224 [
225 "Custom",
226 "custom",
227 []
228 ],
229 [
230 ".*",
231 "ios7",
232 []
233 ]
234 ];
235
236 dm.loadedCssFiles = [];
237 this.loadDeviceTheme = function(/*String?*/userAgent){
238 // summary:
239 // Loads a device-specific theme according to the user-agent
240 // string.
241 // description:
242 // This function is automatically called when this module is
243 // evaluated.
244 var t = config.mblThemeFiles || dm.themeFiles || ["@theme"];
245 var i, j;
246 var m = this.themeMap;
247 var ua = userAgent || config.mblUserAgent || (location.search.match(/theme=(\w+)/) ? RegExp.$1 : navigator.userAgent);
248 for(i = 0; i < m.length; i++){
249 if(ua.match(new RegExp(m[i][0]))){
250 var theme = m[i][1];
251 if(theme == "windows" && config.mblDisableWindowsTheme){
252 continue;
253 }
254 var cls = win.doc.documentElement.className;
255 cls = cls.replace(new RegExp(" *" + dm.currentTheme + "_theme"), "") + " " + theme + "_theme";
256 win.doc.documentElement.className = cls;
257 dm.currentTheme = theme;
258 var files = [].concat(m[i][2]);
259 for(j = 0; j < t.length; j++){
260 var isArray = (t[j] instanceof Array || typeof t[j] == "array");
261 var path;
262 if(!isArray && t[j].indexOf('/') !== -1){
263 path = t[j];
264 }else{
265 var pkg = isArray ? (t[j][0]||"").replace(/\./g, '/') : "dojox/mobile";
266 var name = (isArray ? t[j][1] : t[j]).replace(/\./g, '/');
267 var f = "themes/" + theme + "/" +
268 (name === "@theme" ? theme : name) + ".css";
269 path = pkg + "/" + f;
270 }
271 files.unshift(this.toUrl(path));
272 }
273 //remove old css files
274 for(var k = 0; k < dm.loadedCssFiles.length; k++){
275 var n = dm.loadedCssFiles[k];
276 n.parentNode.removeChild(n);
277 }
278 dm.loadedCssFiles = [];
279 for(j = 0; j < files.length; j++){
280 // dojox.mobile mirroring support
281 var cssFilePath = files[j].toString();
282 if(config["dojo-bidi"] == true && cssFilePath.indexOf("_rtl") == -1){
283 var rtlCssList = "android.css blackberry.css custom.css iphone.css holodark.css base.css Carousel.css ComboBox.css IconContainer.css IconMenu.css ListItem.css RoundRectCategory.css SpinWheel.css Switch.css TabBar.css ToggleButton.css ToolBarButton.css ProgressIndicator.css Accordion.css GridLayout.css FormLayout.css";
284 var cssName = cssFilePath.substr(cssFilePath.lastIndexOf('/') + 1);
285 if(rtlCssList.indexOf(cssName) != -1){
286 this.loadCssFile(cssFilePath.replace(".css","_rtl.css"));
287 }
288 }
289 this.loadCssFile(files[j].toString());
290 }
291
292 if(userAgent && dm.loadCompatCssFiles){
293 dm.loadCompatCssFiles();
294 }
295 break;
296 }
297 }
298 };
299 };
300
301 // Singleton. (TODO: can we replace DeviceTheme class and singleton w/a simple hash of functions?)
302 var deviceTheme = new DeviceTheme();
303
304 deviceTheme.loadDeviceTheme();
305 window.deviceTheme = dm.deviceTheme = deviceTheme;
306
307 return deviceTheme;
308 });