-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.js
525 lines (476 loc) · 18.9 KB
/
demo.js
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
$(function() {
var widgetNames = ["slider", "progressbar", "menubar", "button", "dialog", "checkbox",
"accordion", "tree", "carousel", "tabs", "tooltip", "autocomplete", "panel", "datepicker"];
var loadedWidgets = {};
var coreWidgets = ["slider", "button", "checkbox", "tooltip", "tabs", "panel", "datepicker", "autocomplete", "accordion", "menubar"];
// update last modifed information
var months= ["January","February","March","April","May","June","July","August","September","October","November","December"];
var lastMod = new Date(document.lastModified);
$("#timeStamp").html(lastMod.getDate() + " " + months[lastMod.getMonth()] + " " + lastMod.getFullYear());
$(document.body).append($("<span id='statusUpdater' aria-live='polite' class='ui-helper-hidden-accessible'></span>"));
// Allow tab to be selected through deep linking
var selectedTabId = 0
var query = document.location.search;
if (query) {
var match = query.match(/(\\?|&)tabId=([^&]+)(\\&|$)/i);
if (match && match[2]) {
if (!isNaN(match[2]))
selectedTabId = match[2];
else
selectedTabId = jQuery.inArray(match[2], widgetNames);
if (selectedTabId == -1)
selectedTabId = 0;
}
}
//Load main tabs
$("#demoTabs").tabs({labelledBy: "tabsLbl", selected : selectedTabId});
//show event doesn't fire for selected tab on creation, so manually trigger the handler
createTabPanelContents($("#demoTabs > .ui-tabs-panel").eq(selectedTabId));
setTimeout(function() {
//createSliders($("#slider"));
}, 500);
$('#demoTabs').bind('tabsshow', function(event, ui) {
createTabPanelContents($(ui.panel));
if ($(ui.panel).attr("id") != "tooltip" && loadedWidgets["tooltip"])
$(".toggleTooltips :ui-tooltip").tooltip("close");
});
$('#demoTabs > .ui-tabs-panel').each(function() {
if (jQuery.inArray(this.id, coreWidgets) == -1)
return;
$(this)
.prepend($("<button class='enabled'>Disable "+ this.id+"</button>").button().click(
function(){
var btn = $(this);
if (btn.hasClass("enabled")) {
toggleEnabledInPanel(btn.closest(".ui-tabs-panel"), false);
$("#statusUpdater").text(btn.text().replace(/disable/i, "") + " disabled");
btn.toggleClass("enabled").button("option", "label", btn.text().replace(/Disable/i, "Enable"));
}
else {
toggleEnabledInPanel(btn.closest(".ui-tabs-panel"), true);
$("#statusUpdater").text(btn.text().replace(/enable/i, "") + " enabled");
btn.toggleClass("enabled").button("option", "label", btn.text().replace(/Enable/i, "Disable"));
}
}))
.prepend($("<button class='created'>Destroy "+ this.id+"</button>").button().click(
function(){
var btn = $(this);
if (btn.hasClass("created")) {
destroyInPanel(btn.closest(".ui-tabs-panel"));
$("#statusUpdater").text(btn.text().replace(/destroy/i, "") + " destroyed");
btn.toggleClass("created").button("option", "label", btn.text().replace(/Destroy/i, "Create"));
}
else {
createTabPanelContents(btn.closest(".ui-tabs-panel"), true);
$("#statusUpdater").text(btn.text().replace(/create/i, "") + " created");
btn.toggleClass("created").button("option", "label", btn.text().replace(/Create/i, "Destroy"));
}
}));
});
function createTabPanelContents(panel, skipLoadedCheck) {
if (!panel || panel.length == 0)
return;
var widgetId = $(panel).attr("id");
if (!widgetId || (loadedWidgets[widgetId] & !skipLoadedCheck) || $.inArray(widgetId, widgetNames) == -1)
return;
switch(widgetId) {
case "slider":
createSliders(panel);
break;
case "progressbar":
createProgressBars(panel);
break;
case "button":
$("#repeat").addClass("hiddenFieldset")
$("#toolbar").addClass("ui-widget-header").addClass("ui-corner-all");
createButtons(panel);
break;
case "menubar":
$("#sampleMenubar").menubar({isMenuBar : true});
//VERY experimental: Moving focus back when accessing menu by shortcut
$(document).bind("keyup", function(event){
if (event.keyCode == 77 & event.shiftKey && event.altKey) {
if (event.target.nodeType != 1 || !$(event.target).is(":focusable"))
$("#sampleMenubar").data("returnFocusTo", null)
else
$("#sampleMenubar").data("returnFocusTo", event.target)
var tabId = $.inArray("menubar", widgetNames);
if (tabId != -1) {
$("#demoTabs").tabs("select", tabId);
$("#sampleMenubar").find("a[tabindex=0]").get(0).focus();
}
}
})
break;
case "dialog":
createDialog(panel);
break;
case "tree":
$('#sampleTree').jstree({plugins : ["themes", "html_data", "ui", "hotkeys"]});
break;
case "carousel":
$('#mycarousel1').jcarousel({
animation: 500,
itemSelectedCallback : itemSelectedCallback
});
break;
case "tooltip":
autoCreateInPanel(panel);
$("<button id='tooltipToggler'/>").text("Toggle tooltips for static elements").button().toggle(function() {
$(".toggleTooltips :ui-tooltip").tooltip("open");
}, function() {
$(".toggleTooltips :ui-tooltip").tooltip("close");
}).appendTo("#tooltipButtonAnchor");
break;
case "tabs":
$("#sampleTabs").tabs({labelledBy: "tabsDemoLbl"});
break;
case "autocomplete":
createAutoComplete(panel);
break;
default: //No special logic required, simply call component's method on demo objects in tab poanel
autoCreateInPanel(panel);
}
loadedWidgets[widgetId] = true;
}
function autoCreateInPanel(panel) {
var widgetId = panel.attr("id");
var elements = panel.find(".demoWidget");
if (typeof elements[widgetId] == "function")
elements[widgetId]();
}
function destroyInPanel(panel) {
if (!panel || panel.length === 0)
return;
var widgetId = panel.attr("id");
if (!widgetId || $.inArray(widgetId, coreWidgets) == -1)
return;
switch (widgetId) {
case "slider":
autoDestroyInPanel(panel);
destroySliders(panel);
break;
case "button":
autoDestroyInPanel(panel);
$("#repeat").buttonset("destroy");
$("#repeat").removeClass("hiddenFieldset");
$("#toolbar").removeClass("ui-widget-header").removeClass("ui-corner-all");
break;
case "autocomplete":
autoDestroyInPanel(panel);
$( "#tags-2" ).unbind();
break;
case "tooltip":
$("#tooltipToggler").remove();
autoDestroyInPanel(panel);
break;
default:
autoDestroyInPanel(panel);
break;
}
}
function autoDestroyInPanel(panel) {
var widgetId = panel.attr("id");
var elements = panel.find(".demoWidget");
if (typeof elements[widgetId] == "function")
elements[widgetId]("destroy");
}
function toggleEnabledInPanel(panel, enable) {
var widgetId = panel.attr("id");
switch (widgetId) {
case "button":
autoToggleEnabled(panel, enable);
$("#repeat :radio").button(enable ? "enable" : "disable");
break;
case "tabs":
$("#sampleTabs").tabs(enable ? "enable" : "disable", 1);
break;
case "autocomplete":
if (enable)
$(":ui-autocomplete").removeAttr("disabled");
else
$(":ui-autocomplete").attr("disabled", "disabled");
break;
default:
autoToggleEnabled(panel, enable);
break;
}
}
function autoToggleEnabled(panel, enable) {
var elements = panel.find(".demoWidget");
var widgetId = panel.attr("id");
if (typeof elements[widgetId] == "function")
elements[widgetId](enable ? "enable" : "disable");
}
//SLIDER
function createSliders(panel) {
//single slider
$(panel).find(".fallback").hide();
$("#singleSlider1").slider({unittext : "MB",
label : "price",
unittext: "$",
slide: function(event, ui) {
updateSliderLabels(ui, ["#slider1Val"]);
},
change : function(event, ui) {
updateSliderLabels(ui, ["#slider1Val"]);
}
});
setTimeout( function() {
$(panel).find(".sliderValue").show();
updateSliderLabels({value : $("#singleSlider1").slider("value"), handle : $("#singleSlider1").find(".ui-slider-handle").eq(0)}, ["#slider1Val"]);
updateSliderLabels({value : $("#singleSlider1").slider("value"), handle : $("#singleSlider1").find(".ui-slider-handle").eq(0)}, ["#slider1Val"]);
}, 100);
// range slider
var rangeSlider = $("#rangeSlider1")
.slider({
range: true,
min: 0,
max: 500,
values: [75, 300],
unittext: "$",
label: "price range",
slide: function(event, ui) {
updateSliderLabels(ui, ["#slider2ValMin", "#slider2ValMax"]);
},
change : function(event, ui) {
updateSliderLabels(ui, ["#slider2ValMin", "#slider2ValMax"]);
}
});
setTimeout(function() {
var sliderValues = rangeSlider.slider("values");
updateSliderLabels({value : sliderValues[0], values : sliderValues, handle : rangeSlider.find(".ui-slider-handle").eq(0)}, ["#slider2ValMin", "#slider2ValMax"]);
updateSliderLabels({value : sliderValues[1], values : sliderValues, handle : rangeSlider.find(".ui-slider-handle").eq(1)}, ["#slider2ValMin", "#slider2ValMax"]);
// need to do this twice for some reason, va;ue is not properly positioned otherwise
updateSliderLabels({value : sliderValues[0], values : sliderValues, handle : rangeSlider.find(".ui-slider-handle").eq(0)}, ["#slider2ValMin", "#slider2ValMax"]);
updateSliderLabels({value : sliderValues[1], values : sliderValues, handle : rangeSlider.find(".ui-slider-handle").eq(1)}, ["#slider2ValMin", "#slider2ValMax"]);
}, 100);
}
function updateSliderLabels(ui, valueLabels) {
if (!ui.values)
ui.values = [ui.value];
// need to be able to determine which of the handles actually changes
var index = $.inArray(ui.value, ui.values);
var myAlign = index == 0 ? "right" : "left";
var atAlign = index == 0 ? "left" : "right";
$(valueLabels[index])
.position({
my: myAlign + " bottom",
at : atAlign + " top",
of: ui.handle
})
.text("$" + ui.value);
return;
}
function destroySliders(panel) {
panel.find(".fallback").show();
panel.find("demoWidget").slider("destroy");
panel.find(".sliderValue").hide();
}
//POGRESSBAR
function createProgressBars(panel) {
$(panel).find('#progressTrigger').button()
.click(function() {
$("#progressMsg").remove();
var progressBar = $("#sampleProgressBar")
.progressbar({
value: 0,
labelledBy: "progressMsg"
});
var progressDialog = $("#progressDialog")
.append("<p id='progressMsg' aria-live='true'>Loading Files...</p>")
.dialog({autoOpen : true,
modal : true,
title : "progress",
resizable : false,
draggable : false,
dialogClass : "noCloseBtn",
width : 500,
beforeClose : function() {
if ($("#sampleProgressBar").progressbar('value') != 100)
return false;}
})
var progressUpdater;
setTimeout(function() {
$("#sampleProgressBar").progressbar('value', 0);
progressUpdater = setInterval(function() {
if ($("#sampleProgressBar").progressbar('value') == 100) {
clearInterval(progressUpdater);
$("#progressDialog").dialog("close");
$('#progressTrigger').focus();
}
$("#sampleProgressBar").progressbar('value', $("#sampleProgressBar").progressbar('value') + 2);
}, 250);
}, 100);
});
}
// BUTTON
function createButtons(panel) {
$('#beginning').button({
text: false,
icons: {
primary: 'ui-icon-seek-start'
}
});
$('#rewind').button({
text: false,
icons: {
primary: 'ui-icon-seek-prev'
}
});
$('#play').button({
text: false,
icons: {
primary: 'ui-icon-play'
}
})
.click(function() {
var options;
if ($(this).text() == 'play') {
options = {
label: 'pause',
icons: {
primary: 'ui-icon-pause'
}
};
} else {
options = {
label: 'play',
icons: {
primary: 'ui-icon-play'
}
};
}
$(this).button('option', options);
});
$('#stop').button({
text: false,
icons: {
primary: 'ui-icon-stop'
}
})
.click(function() {
$('#play').button('option', {
label: 'play',
icons: {
primary: 'ui-icon-play'
}
});
});
$('#forward').button({
text: false,
icons: {
primary: 'ui-icon-seek-next'
}
});
$('#end').button({
text: false,
icons: {
primary: 'ui-icon-seek-end'
}
});
$("#shuffle").button();
$("#repeat").buttonset();
}
// DIALOG;
function createDialog(panel) {
$("#sampleDialog").dialog({
draggable : true,
resizable : true,
minHeight: 350,
minWidth: 270,
close : function(e){
$('#dialogTrigger').focus()
}
,autoOpen : false,
describedBy : "dialogDescription",
modal : true,
buttons: { "Ok": function() { $(this).dialog("close"); }}});
$('#dialogTrigger').button()
.click(function() {
$("#sampleDialog").dialog("open")
.find(":input").eq(0).focus();
return false;
});
}
// HELPER FUNCTIONS
// carousel
function itemSelectedCallback(carousel, item, index) {
item = $(item);
var src = item.find("img").attr("src");
var alt = item.find("img").attr("alt");
if (src) {
$("#viewerImg").attr("src", src);
}
if (alt) {
$("#viewerImg").attr("alt", alt);
}
}
// AUTOCOMPLETE
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
function createAutoComplete(panel) {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#tags-1" ).autocomplete({
source: availableTags
});
$( "#tags-2" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
}
});