Index: trunk/extensions/MobileFrontend/tests/js/test_application.js |
— | — | @@ -1,6 +1,26 @@ |
2 | 2 | var MFE = MobileFrontend; |
3 | 3 | var MFET = window.MobileFrontendTests; |
4 | 4 | |
| 5 | +module("MobileFrontend application.js: utils"); |
| 6 | + |
| 7 | +test("addClass", function() { |
| 8 | + var el = $("<div />")[0]; |
| 9 | + MFE.utils(el).addClass("foo"); |
| 10 | + MFE.utils(el).addClass("bar"); |
| 11 | + strictEqual($(el).hasClass("foo"), true); |
| 12 | + strictEqual($(el).hasClass("bar"), true); |
| 13 | +}); |
| 14 | + |
| 15 | +test("removeClass", function() { |
| 16 | + var el = $("<div />")[0]; |
| 17 | + MFE.utils(el).addClass("foo"); |
| 18 | + MFE.utils(el).addClass("bar"); |
| 19 | + MFE.utils(el).removeClass("foo"); |
| 20 | + MFE.utils(el).removeClass("bar"); |
| 21 | + strictEqual($(el).hasClass("foo"), false); |
| 22 | + strictEqual($(el).hasClass("bar"), false); |
| 23 | +}); |
| 24 | + |
5 | 25 | module("MobileFrontend application.js: cookies"); |
6 | 26 | |
7 | 27 | test("read and write cookies", function() { |
Index: trunk/extensions/MobileFrontend/javascripts/beta_application.js |
— | — | @@ -177,13 +177,40 @@ |
178 | 178 | return null; |
179 | 179 | } |
180 | 180 | |
| 181 | + function utilities( el ) { |
| 182 | + function addClass( name ) { |
| 183 | + var className = el.className; |
| 184 | + var classNames = className.split( ' ' ); |
| 185 | + classNames.push(name); // TODO: only push if unique |
| 186 | + el.className = classNames.join( ' ' ); |
| 187 | + } |
| 188 | + |
| 189 | + function removeClass( name ) { |
| 190 | + var className = el.className, |
| 191 | + classNames = className.split( ' ' ), |
| 192 | + newClasses = [], i; |
| 193 | + for( i = 0; i < classNames.length; i++ ) { |
| 194 | + if( classNames[i] !== name ) { |
| 195 | + newClasses.push( classNames[i] ); |
| 196 | + } |
| 197 | + } |
| 198 | + el.className = newClasses.join( ' ' ); |
| 199 | + } |
| 200 | + |
| 201 | + return { |
| 202 | + addClass: addClass, |
| 203 | + removeClass: removeClass |
| 204 | + }; |
| 205 | + } |
| 206 | + |
181 | 207 | return { |
182 | 208 | readCookie: readCookie, |
183 | 209 | writeCookie: writeCookie, |
184 | 210 | removeCookie: removeCookie, |
185 | 211 | wm_reveal_for_hash: wm_reveal_for_hash, |
186 | 212 | wm_toggle_section: wm_toggle_section, |
187 | | - init: init |
| 213 | + init: init, |
| 214 | + utils: utilities |
188 | 215 | }; |
189 | 216 | |
190 | 217 | }(); |
Index: trunk/extensions/MobileFrontend/javascripts/application.js |
— | — | @@ -166,13 +166,40 @@ |
167 | 167 | return null; |
168 | 168 | } |
169 | 169 | |
| 170 | + function utilities( el ) { |
| 171 | + function addClass( name ) { |
| 172 | + var className = el.className; |
| 173 | + var classNames = className.split( ' ' ); |
| 174 | + classNames.push(name); // TODO: only push if unique |
| 175 | + el.className = classNames.join( ' ' ); |
| 176 | + } |
| 177 | + |
| 178 | + function removeClass( name ) { |
| 179 | + var className = el.className, |
| 180 | + classNames = className.split( ' ' ), |
| 181 | + newClasses = [], i; |
| 182 | + for( i = 0; i < classNames.length; i++ ) { |
| 183 | + if( classNames[i] !== name ) { |
| 184 | + newClasses.push( classNames[i] ); |
| 185 | + } |
| 186 | + } |
| 187 | + el.className = newClasses.join( ' ' ); |
| 188 | + } |
| 189 | + |
| 190 | + return { |
| 191 | + addClass: addClass, |
| 192 | + removeClass: removeClass |
| 193 | + }; |
| 194 | + } |
| 195 | + |
170 | 196 | return { |
171 | 197 | readCookie: readCookie, |
172 | 198 | writeCookie: writeCookie, |
173 | 199 | removeCookie: removeCookie, |
174 | 200 | wm_reveal_for_hash: wm_reveal_for_hash, |
175 | 201 | wm_toggle_section: wm_toggle_section, |
176 | | - init: init |
| 202 | + init: init, |
| 203 | + utils: utilities |
177 | 204 | }; |
178 | 205 | |
179 | 206 | }(); |