7e3de0a9c7c06ddf0a5b818b33a5521c87e1d7d4
[yaffs-website] / js / theme.js
1 /**
2  * @file
3  * Theme hooks for the Drupal Bootstrap base theme.
4  */
5 (function ($, Drupal, Bootstrap) {
6
7   /**
8    * Fallback for theming an icon if the Icon API module is not installed.
9    */
10   if (!Drupal.icon) Drupal.icon = { bundles: {} };
11   if (!Drupal.theme.icon || Drupal.theme.prototype.icon) {
12     $.extend(Drupal.theme, /** @lends Drupal.theme */ {
13       /**
14        * Renders an icon.
15        *
16        * @param {string} bundle
17        *   The bundle which the icon belongs to.
18        * @param {string} icon
19        *   The name of the icon to render.
20        * @param {object|Attributes} [attributes]
21        *   An object of attributes to also apply to the icon.
22        *
23        * @returns {string}
24        */
25       icon: function (bundle, icon, attributes) {
26         if (!Drupal.icon.bundles[bundle]) return '';
27         attributes = Attributes(attributes).addClass('icon').set('aria-hidden', 'true');
28         icon = Drupal.icon.bundles[bundle](icon, attributes);
29         return '<span' + attributes + '></span>';
30       }
31     });
32   }
33
34   /**
35    * Callback for modifying an icon in the "bootstrap" icon bundle.
36    *
37    * @param {string} icon
38    *   The icon being rendered.
39    * @param {Attributes} attributes
40    *   Attributes object for the icon.
41    */
42   Drupal.icon.bundles.bootstrap = function (icon, attributes) {
43     attributes.addClass(['glyphicon', 'glyphicon-' + icon]);
44   };
45
46   /**
47    * Add necessary theming hooks.
48    */
49   $.extend(Drupal.theme, /** @lends Drupal.theme */ {
50
51     /**
52      * Renders a Bootstrap AJAX glyphicon throbber.
53      *
54      * @returns {string}
55      */
56     ajaxThrobber: function () {
57       return Drupal.theme.bootstrapIcon('refresh', {'class': ['ajax-throbber', 'glyphicon-spin'] });
58     },
59
60     /**
61      * Renders a button element.
62      *
63      * @param {object|Attributes} attributes
64      *   An object of attributes to apply to the button. If it contains one of:
65      *   - value: The label of the button.
66      *   - context: The context type of Bootstrap button, can be one of:
67      *     - default
68      *     - primary
69      *     - success
70      *     - info
71      *     - warning
72      *     - danger
73      *     - link
74      *
75      * @returns {string}
76      */
77     button: function (attributes) {
78       attributes = Attributes(attributes).addClass('btn');
79       var context = attributes.get('context', 'default');
80       var label = attributes.get('value', '');
81       attributes.remove('context').remove('value');
82       if (!attributes.hasClass(['btn-default', 'btn-primary', 'btn-success', 'btn-info', 'btn-warning', 'btn-danger', 'btn-link'])) {
83         attributes.addClass('btn-' + Bootstrap.checkPlain(context));
84       }
85       return '<button' + attributes + '>' + label + '</button>';
86     },
87
88     /**
89      * Alias for "button" theme hook.
90      *
91      * @param {object|Attributes} attributes
92      *   An object of attributes to apply to the button.
93      *
94      * @see Drupal.theme.button()
95      *
96      * @returns {string}
97      */
98     btn: function (attributes) {
99       return Drupal.theme('button', attributes);
100     },
101
102     /**
103      * Renders a button block element.
104      *
105      * @param {object|Attributes} attributes
106      *   An object of attributes to apply to the button.
107      *
108      * @see Drupal.theme.button()
109      *
110      * @returns {string}
111      */
112     'btn-block': function (attributes) {
113       return Drupal.theme('button', Attributes(attributes).addClass('btn-block'));
114     },
115
116     /**
117      * Renders a large button element.
118      *
119      * @param {object|Attributes} attributes
120      *   An object of attributes to apply to the button.
121      *
122      * @see Drupal.theme.button()
123      *
124      * @returns {string}
125      */
126     'btn-lg': function (attributes) {
127       return Drupal.theme('button', Attributes(attributes).addClass('btn-lg'));
128     },
129
130     /**
131      * Renders a small button element.
132      *
133      * @param {object|Attributes} attributes
134      *   An object of attributes to apply to the button.
135      *
136      * @see Drupal.theme.button()
137      *
138      * @returns {string}
139      */
140     'btn-sm': function (attributes) {
141       return Drupal.theme('button', Attributes(attributes).addClass('btn-sm'));
142     },
143
144     /**
145      * Renders an extra small button element.
146      *
147      * @param {object|Attributes} attributes
148      *   An object of attributes to apply to the button.
149      *
150      * @see Drupal.theme.button()
151      *
152      * @returns {string}
153      */
154     'btn-xs': function (attributes) {
155       return Drupal.theme('button', Attributes(attributes).addClass('btn-xs'));
156     },
157
158     /**
159      * Renders a glyphicon.
160      *
161      * @param {string} name
162      *   The name of the glyphicon.
163      * @param {object|Attributes} [attributes]
164      *   An object of attributes to apply to the icon.
165      *
166      * @returns {string}
167      */
168     bootstrapIcon: function (name, attributes) {
169       return Drupal.theme('icon', 'bootstrap', name, attributes);
170     }
171
172   });
173
174 })(window.jQuery, window.Drupal, window.Drupal.bootstrap);