This repository was archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathstormpath_docs.js
More file actions
355 lines (333 loc) · 11.5 KB
/
stormpath_docs.js
File metadata and controls
355 lines (333 loc) · 11.5 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
(function ($) {
var current_page = '';
var languages = [];
$(document).ready(function(e) {
// Initialize global variables.
languages = typeof sp_docs_menu !== 'undefined' ? sp_docs_menu : {};
current_page = window.location.pathname.split('/').slice(2);
current_page = current_page.length ? current_page[0] : undefined;
var $form = $('form.stormpath-docs-select-language');
if ($form.length) {
$form.find('select[name="language"]').change(function(e) {
var lang = $('> option:selected', this).val();
if (lang && lang in languages) {
do_redirect(lang);
}
});
$form.find('input[type="submit"]').click(function(e) {
var lang = $(this).parents('form').find('select[name="language"] option:selected').val();
if (lang && lang in languages) {
do_redirect(lang);
e.preventDefault();
e.stopPropagation();
return false;
}
});
}
// Make language select "fancy".
$('select.stormpath-fancy-select:not(.processed)').each(function() {
var $fancy_select = new fancySelect(this);
$(this).addClass('processed');
var $form = $(this).parents('form');
$form.find('input[type="submit"]').click(function(e) {
var lang = $(this).parents('form').find('select[name="language"] option:selected').val();
window.location.href = '/' + lang + '/quickstart';
e.preventDefault();
e.stopPropagation();
return false;
});
// When lang is selected redirect user immediately.
if ($form.hasClass('stormpath-docs-select-lang')) {
$fancy_select.find('span:not(.first)').click(function(e) {
window.location.href = '/' + $(this).attr('value') + '/quickstart';
e.stopPropagation();
});
}
});
// Hide all 3+ level menues. Active menu branch will be shown later on window.load
$('.block-stormpath-docs .menu .menu .menu').hide();
// For the front page.
var max_height = 0;
$('body.page-docs .center-wrapper .pane-block').each(function() {
max_height = Math.max(max_height, $(this).height());
});
$('body.page-docs .center-wrapper .pane-block').css('height', max_height);
});
/**
* Redirect browser to another language.
*/
function do_redirect(lang) {
if (current_page !== undefined && languages[lang].indexOf(current_page) >= 0) {
window.location.pathname = lang +'/'+ current_page;
}
else if (current_page !== undefined && languages[lang].indexOf('quickstart') >= 0) {
window.location.pathname = lang +'/quickstart';
}
else if (current_page !== undefined) {
window.location.pathname = lang +'/'+ languages[lang][0];
} else {
window.location.pathname = lang +'/quickstart';
}
}
/**
*
*/
strormpath_docs_menu_map = function($_anchors) {
$anchors = $_anchors.filter(function() {
// Fill menu map.
var id = this.hash.substring(1);
return (document.getElementById(id) != null);
});
var _this = this;
this.current = null;
this.current_branch = null;
this.branches = [];
$anchors.each(function() {
// Fill menu map.
var id = this.hash.substring(1);
var element = document.getElementById(id);
var item = {
id: id,
anchor: this,
element: element,
level: parseInt($(this).attr('data-level')) || 0,
start: element.offsetTop,
};
if (item.level > 0) {
item.parent = $(this).parents('ul.menu:first').parent('li').find('a.fragment:first');
}
else {
var branch = $(this).next('ul.menu');
// Set unique id to easily identify branch.
branch.data('uuid', Math.random());
_this.branches.push(branch);
}
_this.push(item);
// Set 'end' for previous item.
if (_this.length > 1) {
_this[_this.length - 2].end = _this[_this.length - 1].start;
}
});
if (_this.length > 0) {
_this[_this.length - 1].end = Infinity;
}
};
strormpath_docs_menu_map.prototype = new Array();
/**
* Find element by id.
*/
strormpath_docs_menu_map.prototype.findById = function(id) {
for (var key in this) {
if (this.hasOwnProperty(key) && (parseInt(key) == key) && this[key].id === id) {
return this[key];
}
}
return undefined;
}
/**
* Push old status to the history.
*/
strormpath_docs_menu_map.prototype.pushHistoryState = function(path) {
if (typeof window.history.pushState === 'function') {
history.pushState({}, '', path);
}
}
/**
* Set new current item in the docs menu.
*/
strormpath_docs_menu_map.prototype.setCurrent = function(current) {
var prev_item = this.current;
if (this.current !== null) {
$(this.current).removeClass('current');
}
this.current = current;
$(this.current).addClass('current');
var current_item = this.findById(this.current.hash.substr(1));
if (typeof current_item === 'undefined') {
// Reset current path.
return;
}
if (current_item.level === 0) {
var new_branch = $(this.current).next('ul.menu');
}
else {
var new_branch = $(current_item.parent).next('ul.menu');
}
if (this.current_branch === null) {
this.current_branch = new_branch;
}
else if (new_branch.data('uuid') !== this.current_branch.data('uuid')) {
$(this.current_branch).hide();
this.current_branch = new_branch;
}
$(this.current_branch).show();
$(this.current_branch).height(); // Do not remove.
}
/**
* Get currently selected branch.
*/
strormpath_docs_menu_map.prototype.getCurrentBranch = function() {
return this.current_branch === null ? undefined : this.current_branch;
}
$(window).load(function() {
// Offset from the top of the content area to the header of current section.
// This will help us to know when to switch active menu item when user scrolls the page.
var MAGIC_OFFSET = $('#header-wrapper').height() + 20;
var $anchors = $('.block-stormpath-docs .menu a.fragment');
if (!$anchors.length) {
return;
}
var content_area = window;
var menu_map = new strormpath_docs_menu_map($anchors);
$anchors.each(function() {
// Mark active menu item.
if (window.location.hash === this.hash || window.location.hash == '#!'+ this.hash.substr(1)) {
menu_map.setCurrent(this);
/*
var id = this.hash.substr(1);
var el = document.getElementById(id);
$(content_area).scrollTop(Math.max(0, el.offsetTop));
*/
return false; // break;
}
});
/*
$anchors.click(function(e) {
// Scroll content area manually.
var id = this.hash.substr(1);
var el = document.getElementById(id);
$(content_area).scrollTop(Math.max(0, el.offsetTop - MAGIC_OFFSET));
menu_map.pushHistoryState('#'+ this.hash.substr(1));
e.preventDefault();
return false;
});
*/
var current_root = $('.block-stormpath-docs .menu .leaf.active-trail > a').get(0);
$(current_root).removeClass('active');
if (menu_map.current === null) {
menu_map.setCurrent(current_root);
}
$(content_area).scroll(function(e) {
var top = $(content_area).scrollTop() + MAGIC_OFFSET + 2;
var new_item_found = false;
// Find menu map item user currently view.
for (var key in menu_map) {
if (!menu_map.hasOwnProperty(key) || (parseInt(key) != key)) {
continue;
}
if (top >= menu_map[key].element.offsetTop && top < menu_map[key].end) {
menu_map.setCurrent(menu_map[key].anchor);
new_item_found = true;
break;
}
}
if (!menu_map.current && !new_item_found) {
menu_map.setCurrent(current_root);
}
});
// Trigger scroll event to set actual current item.
// User could already had scrolled content area until window.load triggered.
//$(content_area).trigger('scroll');
});
// Fancy select functions.
function fancySelect(select) {
var _this = this;
var $select = $(select);
var $fancy = $('<span class="stormpath-fancy-select"></span>');
this._$select = $select;
this.$select = $fancy;
this.options = [];
this.value = null;
$fancy.attr('tabindex', $select.attr('tabindex') || 0);
$('> option', $select).each(function(key) {
var value = $(this).val();
var text = $(this).html();
var is_selected = $(this).attr('selected') == 'selected';
_this.options[key] = new _this.option(value, text, is_selected);
$fancy.append(_this.options[key].$option);
if (is_selected) {
_this.value = key; // use key as value since option can not have value attribute at all.
}
_this.options[key].$option.click({key: key}, function(e) {
_this.selectOption(_this.options[e.data.key], e.data.key);
_this.$select.trigger('blur');
});
});
if (_this.value === null) {
_this.value = 0;
}
// Like default selects do, add selected option to the top of the options list.
this.selectOption(this.options[_this.value], _this.value);
$fancy.focus(this.onFocus);
$fancy.blur(this.onBlur);
$fancy.keydown({select: this}, this.onKeydown);
$select.hide().after($fancy);
return $fancy;
}
fancySelect.prototype.selectOption = function(option, index) {
for (var k in this.options) {
if (k == index) {
this.options[k].$option.addClass('selected');
continue;
}
this.options[k].$option.removeClass('selected');
}
var $new_option = option.$option.clone().removeClass('selected').addClass('first');
this.$select.find('.first').remove();
this.$select.prepend($new_option);
this.value = index;
$('option', this._$select).each(function(k) {
if (k == index) {
$(this).prop('selected', true);
}
else {
$(this).removeProp('selected');
}
});
}
fancySelect.prototype.option = function(value, text, is_selected) {
this.value = value;
this.text = text;
this.is_selected = is_selected;
var $option = $('<span></span>').attr('value', this.value);
$option.text(this.text);
if (this.is_selected) {
$option.addClass('selected');
}
$option.click({option: this}, this.onClick);
this.$option = $option;
}
fancySelect.prototype.option.prototype.onClick = function(e) {
e.data.option.is_selected = true;
e.data.option.$option.addClass('selected');
}
fancySelect.prototype.onFocus = function(e) {
// In event context this points on the target element.
$(this).addClass('focused');
}
fancySelect.prototype.onBlur = function(e) {
$(this).removeClass('focused');
}
fancySelect.prototype.onKeydown = function(e) {
var _this = e.data.select;
switch (e.which) {
case 38: // Up arrow.
case 63232: // Safari up arrow.
if (_this.value > 0) {
_this.selectOption(_this.options[_this.value - 1], _this.value - 1);
}
e.preventDefault();
break;
case 40: // Down arrow.
case 63233: // Safari down arrow.
if (_this.value + 1 < _this.options.length) {
_this.selectOption(_this.options[_this.value + 1], _this.value + 1);
}
e.preventDefault();
break;
case 13: // enter
_this.$select.trigger('blur');
break
}
}
})(jQuery);