BSTabs.js.html 9.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: BSTabs.js</title>

    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
    <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: BSTabs.js</h1>


    <section>
        <article>
            <pre class="prettyprint source linenums"><code>/**
                * @author Rafael Ostertag &lt;rafael.ostertag@math.uzh.ch>
                */

                /* global $ */
                /* global console */
                /* global EventEmitter */

                /* @depend QfqEvents.js */

                /**
                * Qfq Namespace
                *
                * @namespace QfqNS
                */
                var QfqNS = QfqNS || {};


                (function (n) {
                'use strict';
                /**
                * Tab Constructor.
                *
                * Programatically access Bootstrap nav-tabs.
                *
                * @param {string} tabId HTML id of the element having `nav` and `nav-tabs` classes
                * @constructor
                *
                * @name QfqNS.BSTabs
                */
                n.BSTabs = function (tabId) {
                this.tabId = tabId;
                this._tabContainerLinkSelector = '#' + this.tabId + ' a[data-toggle="tab"]';
                this._tabActiveSelector = '#' + this.tabId + ' .active a[data-toggle="tab"]';
                this.tabs = {};
                this.currentTab = this.getActiveTabFromDOM();
                this.eventEmitter = new EventEmitter();

                // Fill this.tabs
                this.fillTabInformation();

                // Enable update of current tab field
                this.installTabHandlers();
                };

                n.BSTabs.prototype.on = n.EventEmitter.onMixin;

                /**
                * Get active tab from DOM.
                *
                * Used upon object creation to fill the currentTab. It gets the ID of the currently shown tab. It does
                it, by
                * targeting the element in the navigator having the `active` class set.
                *
                * @private
                */
                n.BSTabs.prototype.getActiveTabFromDOM = function () {
                var activeTabAnchors = $(this._tabActiveSelector);
                if (activeTabAnchors.length &lt; 1) {
                // This could be due to the DOM not fully loaded. If that's really the case, then the active tab
                // attribute should be set by the show.bs.tab handler
                return null;
                }

                return activeTabAnchors[0].hash.slice(1);
                };

                /**
                * Fill tabs object.
                *
                * Fill the tabs object using the tab HTML id as attribute name
                *
                * @private
                */
                n.BSTabs.prototype.fillTabInformation = function () {
                var tabLinks = $(this._tabContainerLinkSelector);
                if ($(tabLinks).length === 0) {
                throw new Error("Unable to find a BootStrap container matching: " + this._tabContainerLinkSelector);
                }

                var that = this;
                tabLinks.each(function (index, element) {
                if (element.hash !== "") {
                var tabId = element.hash.slice(1);

                that.tabs[tabId] = {
                index: index,
                element: element
                };
                }
                }
                );
                };

                /**
                * @private
                */
                n.BSTabs.prototype.installTabHandlers = function () {
                $(this._tabContainerLinkSelector)
                .on('show.bs.tab', this.tabShowHandler.bind(this));

                };

                /**
                * Tab Show handler.
                *
                * Sets this.currentTab to the clicked tab and calls all registered tab click handlers.
                *
                * @private
                * @param event
                */
                n.BSTabs.prototype.tabShowHandler = function (event) {
                n.Log.debug('Enter: BSTabs.tabShowHandler()');
                this.currentTab = event.target.hash.slice(1);

                n.Log.debug("BSTabs.tabShowHandler(): invoke user handler(s)");
                this.eventEmitter.emitEvent('bootstrap.tab.shown', n.EventEmitter.makePayload(this, null));
                n.Log.debug('Exit: BSTabs.tabShowHandler()');
                };

                /**
                * Get all tab IDs.
                *
                * @returns {Array}
                *
                * @public
                */
                n.BSTabs.prototype.getTabIds = function () {
                var tabIds = [];
                for (var tabId in this.tabs) {
                if (this.tabs.hasOwnProperty(tabId)) {
                tabIds.push(tabId);
                }
                }
                return tabIds;
                };

                /**
                *
                * @returns {Array}
                *
                * @public
                */
                n.BSTabs.prototype.getTabAnchors = function () {
                var tabLinks = [];
                for (var tabId in this.tabs) {
                if (this.tabs.hasOwnProperty(tabId)) {
                tabLinks.push(this.tabs[tabId].element);
                }
                }

                return tabLinks;
                };

                /**
                * Activate a given tab.
                *
                * @param {string} tabId Id of the tab to activate
                *
                */
                n.BSTabs.prototype.activateTab = function (tabId) {
                if (!this.tabs[tabId]) {
                console.error("Unable to find tab with id: " + tabId);
                return false;
                }

                $(this.tabs[tabId].element).tab('show');
                return true;
                };

                n.BSTabs.prototype.getCurrentTab = function () {
                return this.currentTab;
                };

                n.BSTabs.prototype.getTabName = function (tabId) {
                if (!this.tabs[tabId]) {
                console.error("Unable to find tab with id: " + tabId);
                return null;
                }

                return $(this.tabs[tabId].element).text().trim();
                };

                n.BSTabs.prototype.getActiveTab = function () {
                return this.currentTab;
                };

                n.BSTabs.prototype.getContainingTabIdForFormControl = function (formControlName) {
                var $formControl = $("[name='" + formControlName + "']");
                if ($formControl.length === 0) {
                n.Log.debug("BSTabs.getContainingTabForFormControl(): unable to find form control with name '" +
                formControlName + "'");
                return null;
                }

                var iterator = $formControl[0];
                while (iterator !== null) {
                if (iterator.hasAttribute('role') &amp;&amp;
                iterator.getAttribute('role') === 'tabpanel') {
                return iterator.id || null;
                }

                iterator = iterator.parentElement;
                }

                return null;
                };

                })(QfqNS);</code></pre>
        </article>
    </section>


</div>

<nav>
    <h2><a href="index.html">Home</a></h2>

    <h3>Classes</h3>
    <ul>
        <li><a href="n.Alert.html">Alert</a></li>
        <li><a href="QfqNS.BSTabs.html">BSTabs</a></li>
        <li><a href="QfqNS.Element.Checkbox.html">Checkbox</a></li>
        <li><a href="QfqNS.Element.FormGroup.html">FormGroup</a></li>
        <li><a href="QfqNS.Element.Radio.html">Radio</a></li>
        <li><a href="QfqNS.Element.Select.html">Select</a></li>
        <li><a href="QfqNS.Element.Textual.html">Textual</a></li>
        <li><a href="QfqNS.FileDelete.html">FileDelete</a></li>
        <li><a href="QfqNS.FileUpload.html">FileUpload</a></li>
        <li><a href="QfqNS.Form.html">Form</a></li>
        <li><a href="QfqNS.PageState.html">PageState</a></li>
        <li><a href="QfqNS.QfqForm.html">QfqForm</a></li>
        <li><a href="QfqNS.QfqPage.html">QfqPage</a></li>
        <li><a href="QfqNS.QfqRecordList.html">QfqRecordList</a></li>
    </ul>
    <h3>Namespaces</h3>
    <ul>
        <li><a href="global.html#QfqNS">QfqNS</a></li>
        <li><a href="QfqNS.Element.html">Element</a></li>
        <li><a href="QfqNS.Helper.html">Helper</a></li>
    </ul>
    <h3><a href="global.html">Global</a></h3>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Mon May 23 2016 12:28:24
    GMT+0200 (CEST)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"></script>
</body>
</html>