|
| 1 | +/** |
| 2 | + * @ngdoc service |
| 3 | + * @name $ionicBody |
| 4 | + * @module ionic |
| 5 | + * @description An angular utility service to easily and efficiently |
| 6 | + * add and remove CSS classes from the document's body element. |
| 7 | + */ |
| 8 | +IonicModule |
| 9 | +.factory('$ionicBody', ['$document', function($document) { |
| 10 | + return { |
| 11 | + /** |
| 12 | + * @ngdoc method |
| 13 | + * @name $ionicBody#add |
| 14 | + * @description Add a class to the document's body element. |
| 15 | + * @param {string} class Each argument will be added to the body element. |
| 16 | + * @returns {$ionicBody} The $ionicBody service so methods can be chained. |
| 17 | + */ |
| 18 | + addClass: function() { |
| 19 | + for(var x=0; x<arguments.length; x++) { |
| 20 | + $document[0].body.classList.add(arguments[x]); |
| 21 | + } |
| 22 | + return this; |
| 23 | + }, |
| 24 | + /** |
| 25 | + * @ngdoc method |
| 26 | + * @name $ionicBody#removeClass |
| 27 | + * @description Remove a class from the document's body element. |
| 28 | + * @param {string} class Each argument will be removed from the body element. |
| 29 | + * @returns {$ionicBody} The $ionicBody service so methods can be chained. |
| 30 | + */ |
| 31 | + removeClass: function() { |
| 32 | + for(var x=0; x<arguments.length; x++) { |
| 33 | + $document[0].body.classList.remove(arguments[x]); |
| 34 | + } |
| 35 | + return this; |
| 36 | + }, |
| 37 | + /** |
| 38 | + * @ngdoc method |
| 39 | + * @name $ionicBody#enableClass |
| 40 | + * @description Similar to the `add` method, except the first parameter accepts a boolean |
| 41 | + * value determining if the class should be added or removed. Rather than writing user code, |
| 42 | + * such as "if true then add the class, else then remove the class", this method can be |
| 43 | + * given a true or false value which reduces redundant code. |
| 44 | + * @param {boolean} shouldEnableClass A true/false value if the class should be added or removed. |
| 45 | + * @param {string} class Each remaining argument would be added or removed depending on |
| 46 | + * the first argument. |
| 47 | + * @returns {$ionicBody} The $ionicBody service so methods can be chained. |
| 48 | + */ |
| 49 | + enableClass: function(shouldEnableClass) { |
| 50 | + var args = Array.prototype.slice.call(arguments).slice(1); |
| 51 | + if(shouldEnableClass) { |
| 52 | + this.addClass.apply(this, args); |
| 53 | + } else { |
| 54 | + this.removeClass.apply(this, args); |
| 55 | + } |
| 56 | + return this; |
| 57 | + }, |
| 58 | + /** |
| 59 | + * @ngdoc method |
| 60 | + * @name $ionicBody#append |
| 61 | + * @description Append a child to the document's body. |
| 62 | + * @param {element} element The element to be appended to the body. The passed in element |
| 63 | + * can be either a jqLite element, or a DOM element. |
| 64 | + * @returns {$ionicBody} The $ionicBody service so methods can be chained. |
| 65 | + */ |
| 66 | + append: function(ele) { |
| 67 | + $document[0].body.appendChild( ele.length ? ele[0] : ele ); |
| 68 | + return this; |
| 69 | + }, |
| 70 | + /** |
| 71 | + * @ngdoc method |
| 72 | + * @name $ionicBody#get |
| 73 | + * @description Get the document's body element. |
| 74 | + * @returns {element} Returns the document's body element. |
| 75 | + */ |
| 76 | + get: function() { |
| 77 | + return $document[0].body; |
| 78 | + } |
| 79 | + }; |
| 80 | +}]); |
0 commit comments