| 1 | // module: |
| 2 | // dijit/tests/boilerplate |
| 3 | // description: |
| 4 | // A <script src="boilerplate.js"> on your test page will: |
| 5 | // |
| 6 | // - load claro or a specified theme |
| 7 | // - load the loader (i.e. define the require() method) |
| 8 | // |
| 9 | // By URL flags you can specify the theme, |
| 10 | // and optionally enable RTL (right to left) mode, and/or dj_a11y (high- |
| 11 | // contrast/image off emulation) ... probably not a genuine test for a11y. |
| 12 | // |
| 13 | // You should NOT be using this in a production environment. Include |
| 14 | // your css and set your classes manually: |
| 15 | // |
| 16 | // <style type="text/css"> |
| 17 | // @import "dijit/themes/claro/document.css"; |
| 18 | // </style> |
| 19 | // <link id="themeStyles" rel="stylesheet" href="dijit/themes/claro/claro.css"/> |
| 20 | // <script type="text/javascript" src="dojo/dojo.js"></script> |
| 21 | // ... |
| 22 | // <body class="claro"> |
| 23 | |
| 24 | var dir = "", |
| 25 | theme = "claro", |
| 26 | testMode = null; |
| 27 | |
| 28 | dojoConfig = { |
| 29 | async: true, |
| 30 | isDebug: true, |
| 31 | locale: "en-us" |
| 32 | }; |
| 33 | |
| 34 | // Parse the URL, get parameters |
| 35 | if(window.location.href.indexOf("?") > -1){ |
| 36 | var str = window.location.href.substr(window.location.href.indexOf("?")+1).split(/#/); |
| 37 | var ary = str[0].split(/&/); |
| 38 | for(var i = 0; i < ary.length; i++){ |
| 39 | var split = ary[i].split("="), |
| 40 | key = split[0], |
| 41 | value = (split[1]||'').replace(/[^\w]/g, ""); // replace() to prevent XSS attack |
| 42 | switch(key){ |
| 43 | case "locale": |
| 44 | // locale string | null |
| 45 | dojoConfig.locale = value; |
| 46 | break; |
| 47 | case "dir": |
| 48 | // rtl | null |
| 49 | dir = value; |
| 50 | break; |
| 51 | case "theme": |
| 52 | // tundra | soria | nihilo | claro | null |
| 53 | theme = /null|none/.test(value) ? null : value; |
| 54 | break; |
| 55 | case "a11y": |
| 56 | if(value){ testMode = "dj_a11y"; } |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Find the <script src="boilerplate.js"> tag, to get test directory and data-dojo-config argument |
| 63 | var scripts = document.getElementsByTagName("script"), script, testDir; |
| 64 | for(i = 0; script = scripts[i]; i++){ |
| 65 | var src = script.getAttribute("src"), |
| 66 | match = src && src.match(/(.*|^)boilerplate\.js/i); |
| 67 | if(match){ |
| 68 | // Sniff location of dijit/tests directory relative to this test file. testDir will be an empty string if it's |
| 69 | // the same directory, or a string including a slash, ex: "../", if the test is in a subdirectory. |
| 70 | testDir = match[1]; |
| 71 | |
| 72 | // Sniff configuration on attribute in script element. |
| 73 | // Allows syntax like <script src="boilerplate.js data-dojo-config="parseOnLoad: true">, where the settings |
| 74 | // specified override the default settings. |
| 75 | var attr = script.getAttribute("data-dojo-config"); |
| 76 | if(attr){ |
| 77 | var overrides = eval("({ " + attr + " })"); |
| 78 | for(var key in overrides){ |
| 79 | dojoConfig[key] = overrides[key]; |
| 80 | } |
| 81 | } |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Output the boilerplate text to load the theme CSS |
| 87 | if(theme){ |
| 88 | var themeDir = testDir + "../themes/" + theme + "/"; |
| 89 | document.write([ |
| 90 | '<style type="text/css">', |
| 91 | theme == "claro" ? '@import "' + themeDir + 'document.css";' : "", |
| 92 | '@import "' + testDir + 'css/dijitTests.css";', |
| 93 | '</style>', |
| 94 | '<link id="themeStyles" rel="stylesheet" href="' + themeDir + theme + '.css"/>' |
| 95 | ].join("\n")); |
| 96 | } |
| 97 | |
| 98 | // Output the boilerplate text to load the loader, and to do some initial manipulation when the page finishes loading |
| 99 | // For 2.0 this should be changed to require the loader (ex: requirejs) directly, rather than dojo.js. |
| 100 | document.write('<script type="text/javascript" src="' + testDir + '../../dojo/dojo.js"></script>'); |
| 101 | |
| 102 | // On IE9 the following inlined script will run before dojo has finished loading, leading to an error because require() |
| 103 | // isn't defined yet. Workaround it by putting the code in a separate file. |
| 104 | //document.write('<script type="text/javascript">require(["dojo/domReady!"], boilerplateOnLoad);</script>'); |
| 105 | document.write('<script type="text/javascript" src="' + testDir + 'boilerplateOnload.js"></script>'); |
| 106 | |
| 107 | function boilerplateOnLoad(){ |
| 108 | // This function is the first registered domReady() callback, allowing us to setup |
| 109 | // theme stuff etc. before the widgets start instantiating. |
| 110 | |
| 111 | // theme (claro, tundra, etc.) |
| 112 | if(theme){ |
| 113 | // Set <body> to point to the specified theme |
| 114 | document.body.className = theme; |
| 115 | } |
| 116 | |
| 117 | // a11y (flag for faux high-contrast testing) |
| 118 | if(testMode){ |
| 119 | document.body.className += " " + testMode; |
| 120 | } |
| 121 | |
| 122 | // BIDI |
| 123 | if(dir == "rtl"){ |
| 124 | // set dir=rtl on <html> node |
| 125 | document.body.parentNode.setAttribute("dir", "rtl"); |
| 126 | |
| 127 | require(["dojo/query!css2", "dojo/NodeList-dom"], function(query){ |
| 128 | // pretend all the labels are in an RTL language, because |
| 129 | // that affects how they lay out relative to inline form widgets |
| 130 | query("label").attr("dir", "rtl"); |
| 131 | }); |
| 132 | } |
| 133 | |
| 134 | // parseOnLoad: true requires that the parser itself be loaded. |
| 135 | if(dojoConfig.parseOnLoad){ |
| 136 | require(["dojo/parser"]); |
| 137 | } |
| 138 | } |