patternfly.js
20.8 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
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// Util: PatternFly Sidebar
// Set height of sidebar-pf to height of document minus height of navbar-pf if not mobile
(function ($) {
'use strict';
$.fn.sidebar = function () {
var documentHeight = 0,
navbarpfHeight = 0,
colHeight = 0;
if ($('.navbar-pf .navbar-toggle').is(':hidden')) {
documentHeight = $(document).height();
navbarpfHeight = $('.navbar-pf').outerHeight();
colHeight = documentHeight - navbarpfHeight;
}
$('.sidebar-pf').parent('.row').children('[class*="col-"]').css({"min-height" : colHeight});
};
$(document).ready(function () {
// Call sidebar() on ready if .sidebar-pf exists and .datatable does not exist
if ($('.sidebar-pf').length > 0 && $('.datatable').length === 0) {
$.fn.sidebar();
}
});
$(window).resize(function () {
// Call sidebar() on resize if .sidebar-pf exists
if ($('.sidebar-pf').length > 0) {
$.fn.sidebar();
}
});
}(jQuery));
// Util: PatternFly Popovers
// Add data-close="true" to insert close X icon
(function ($) {
'use strict';
$.fn.popovers = function () {
// Initialize
this.popover();
// Add close icons
this.filter('[data-close=true]').each(function (index, element) {
var $this = $(element),
title = $this.attr('data-original-title') + '<button type="button" class="close" aria-hidden="true"><span class="pficon pficon-close"></span></button>';
$this.attr('data-original-title', title);
});
// Bind Close Icon to Toggle Display
this.on('click', function (e) {
var $this = $(this),
$title = $this.next('.popover').find('.popover-title');
// Only if data-close is true add class "x" to title for right padding
$title.find('.close').parent('.popover-title').addClass('closable');
// Bind x icon to close popover
$title.find('.close').on('click', function () {
$this.popover('hide');
});
// Prevent href="#" page scroll to top
e.preventDefault();
});
return this;
};
}(jQuery));
// Util: DataTables Settings
(function ($) {
'use strict';
if ($.fn.dataTableExt) {
/* Set the defaults for DataTables initialisation */
$.extend(true, $.fn.dataTable.defaults, {
"bDestroy": true,
"bAutoWidth": false,
"iDisplayLength": 20,
"sDom":
"<'dataTables_header' f i r >" +
"<'table-responsive' t >" +
"<'dataTables_footer' p >",
"oLanguage": {
"sInfo": "Showing <b>_START_</b> to <b>_END_</b> of <b>_TOTAL_</b> Items",
"sInfoFiltered" : "(of <b>_MAX_</b>)",
"sInfoEmpty" : "Showing <b>0</b> Results",
"sZeroRecords":
"<p>Suggestions</p>" +
"<ul>" +
"<li>Check the syntax of the search term.</li>" +
"<li>Check that the correct menu option is chosen (token ID vs. user ID).</li>" +
"<li>Use wildcards (* to match zero or more characters or ? to match a single character).</li>" +
"<li>Clear the search field, then click Search to return to the 20 most recent records.</li>" +
"</ul>",
"sSearch": ""
},
"sPaginationType": "bootstrap_input"
});
/* Default class modification */
$.extend($.fn.dataTableExt.oStdClasses, {
"sWrapper": "dataTables_wrapper"
});
/* API method to get paging information */
$.fn.dataTableExt.oApi.fnPagingInfo = function (oSettings) {
return {
"iStart": oSettings._iDisplayStart,
"iEnd": oSettings.fnDisplayEnd(),
"iLength": oSettings._iDisplayLength,
"iTotal": oSettings.fnRecordsTotal(),
"iFilteredTotal": oSettings.fnRecordsDisplay(),
"iPage": oSettings._iDisplayLength === -1 ? 0 : Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength),
"iTotalPages": oSettings._iDisplayLength === -1 ? 0 : Math.ceil(oSettings.fnRecordsDisplay() / oSettings._iDisplayLength)
};
};
/* Combination of Bootstrap + Input Text style pagination control */
$.extend($.fn.dataTableExt.oPagination, {
"bootstrap_input": {
"fnInit": function (oSettings, nPaging, fnDraw) {
var fnClickHandler = function (e) {
e.preventDefault();
if (oSettings.oApi._fnPageChange(oSettings, e.data.action)) {
fnDraw(oSettings);
}
},
els,
nInput;
$(nPaging).append(
'<ul class="pagination">' +
' <li class="first disabled"><span class="i fa fa-angle-double-left"></span></li>' +
' <li class="prev disabled"><span class="i fa fa-angle-left"></span></li>' +
'</ul>' +
'<div class="pagination-input">' +
' <input type="text" class="paginate_input">' +
' <span class="paginate_of">of <b>3</b></span>' +
'</div>' +
'<ul class="pagination">' +
' <li class="next disabled"><span class="i fa fa-angle-right"></span></li>' +
' <li class="last disabled"><span class="i fa fa-angle-double-right"></span></li>' +
'</ul>'
);
els = $('li', nPaging);
$(els[0]).bind('click.DT', { action: "first" }, fnClickHandler);
$(els[1]).bind('click.DT', { action: "previous" }, fnClickHandler);
$(els[2]).bind('click.DT', { action: "next" }, fnClickHandler);
$(els[3]).bind('click.DT', { action: "last" }, fnClickHandler);
nInput = $('input', nPaging);
$(nInput).keyup(function (e) {
if (e.which === 38 || e.which === 39) {
this.value += 1;
} else if ((e.which === 37 || e.which === 40) && this.value > 1) {
this.value -= 1;
}
if (this.value === "" || !this.value.match(/[0-9]/)) {
/* Nothing entered or non-numeric character */
return;
}
var iNewStart = oSettings._iDisplayLength * (this.value - 1);
if (iNewStart > oSettings.fnRecordsDisplay()) {
/* Display overrun */
oSettings._iDisplayStart = (Math.ceil((oSettings.fnRecordsDisplay() - 1) /
oSettings._iDisplayLength) - 1) * oSettings._iDisplayLength;
fnDraw(oSettings);
return;
}
oSettings._iDisplayStart = iNewStart;
fnDraw(oSettings);
});
},
"fnUpdate": function (oSettings, fnDraw) {
var oPaging = oSettings.oInstance.fnPagingInfo(),
an = oSettings.aanFeatures.p,
ien = an.length,
iPages = Math.ceil((oSettings.fnRecordsDisplay()) / oSettings._iDisplayLength),
iCurrentPage = Math.ceil(oSettings._iDisplayStart / oSettings._iDisplayLength) + 1,
i;
for (i = 0; i < ien; i += 1) {
$('.paginate_input').val(iCurrentPage);
$('.paginate_of b').html(iPages);
// Add / remove disabled classes from the static elements
if (oPaging.iPage === 0) {
$('li.first', an[i]).addClass('disabled');
$('li.prev', an[i]).addClass('disabled');
} else {
$('li.first', an[i]).removeClass('disabled');
$('li.prev', an[i]).removeClass('disabled');
}
if (oPaging.iPage === oPaging.iTotalPages - 1 || oPaging.iTotalPages === 0) {
$('li.next', an[i]).addClass('disabled');
$('li.last', an[i]).addClass('disabled');
} else {
$('li.next', an[i]).removeClass('disabled');
$('li.last', an[i]).removeClass('disabled');
}
}
}
}
});
}
}(jQuery));
// Util: PatternFly Collapsible Left Hand Navigation
// Must have navbar-toggle in navbar-pf-alt for expand/collapse
(function ($) {
'use strict';
$.fn.navigation = function () {
var navElement = $('.layout-pf-alt-fixed .nav-pf-vertical-alt'),
bodyContentElement = $('.container-pf-alt-nav-pf-vertical-alt'),
toggleNavBarButton = $('.navbar-toggle'),
explicitCollapse = false,
breakpoints = {
'tablet': 768,
'desktop': 1024
},
checkNavState = function () {
var width = $(window).width();
//Always remove the hidden & peek class
navElement.removeClass('hidden show-mobile-nav collapsed');
//Set the body class back to the default
bodyContentElement.removeClass('collapsed-nav hidden-nav');
// Check to see if the nav needs to collapse
if (width < breakpoints.desktop || explicitCollapse) {
navElement.addClass('collapsed');
bodyContentElement.addClass('collapsed-nav');
}
// Check to see if we need to move down to the mobile state
if (width < breakpoints.tablet) {
//Set the nav to being hidden
navElement.addClass('hidden');
//Make sure this is expanded
navElement.removeClass('collapsed');
//Set the body class to the correct state
bodyContentElement.removeClass('collapsed-nav');
bodyContentElement.addClass('hidden-nav');
}
},
collapseMenu = function () {
//Make sure this is expanded
navElement.addClass('collapsed');
//Set the body class to the correct state
bodyContentElement.addClass('collapsed-nav');
explicitCollapse = true;
},
enableTransitions = function () {
// enable transitions only when toggleNavBarButton is clicked or window is resized
$('html').addClass('transitions');
},
expandMenu = function () {
//Make sure this is expanded
navElement.removeClass('collapsed');
//Set the body class to the correct state
bodyContentElement.removeClass('collapsed-nav');
explicitCollapse = false;
},
bindMenuBehavior = function () {
toggleNavBarButton.on('click', function (e) {
enableTransitions();
var inMobileState = bodyContentElement.hasClass('hidden-nav');
if (inMobileState && navElement.hasClass('show-mobile-nav')) {
//In mobile state just need to hide the nav
navElement.removeClass('show-mobile-nav');
} else if (inMobileState) {
navElement.addClass('show-mobile-nav');
} else if (navElement.hasClass('collapsed')) {
expandMenu();
} else {
collapseMenu();
}
});
},
setTooltips = function () {
$('.nav-pf-vertical-alt [data-toggle="tooltip"]').tooltip({'container': 'body', 'delay': { 'show': '500', 'hide': '200' }});
$(".nav-pf-vertical-alt").on("show.bs.tooltip", function (e) {
if (!$(this).hasClass("collapsed")) {
return false;
}
});
},
init = function () {
//Set correct state on load
checkNavState();
// Bind Top level hamburger menu with menu behavior;
bindMenuBehavior();
//Set tooltips
setTooltips();
};
//Listen for the window resize event and collapse/hide as needed
$(window).on('resize', function () {
checkNavState();
enableTransitions();
});
init();
};
$(document).ready(function () {
if ($('.nav-pf-vertical-alt').length > 0) {
$.fn.navigation();
}
});
}(jQuery));
// Count and Display Remaining Characters
(function ($) {
'use strict';
$.fn.countRemainingChars = function (options) {
var settings = $.extend({
// These are the defaults.
charsMaxLimit: 100,
charsWarnRemaining: 5,
blockInputAtMaxLimit: false
}, options),
$taFld = this,
$countFld = $('#' + settings.countFld).text(settings.charsMaxLimit),
charsRemainingFn = function (charsLength) {
var charsRemaining = settings.charsMaxLimit - charsLength;
$countFld.text(charsRemaining);
$countFld.toggleClass('chars-warn-remaining-pf', charsRemaining <= settings.charsWarnRemaining);
if (charsRemaining < 0) {
$taFld.trigger("overCharsMaxLimitEvent", $taFld.attr('id'));
} else {
$taFld.trigger("underCharsMaxLimitEvent", $taFld.attr('id'));
}
};
this.on('paste', function (event) {
setTimeout(function () {
var charsLength = $taFld.val().length, maxTxt;
if (settings.blockInputAtMaxLimit && charsLength > settings.charsMaxLimit) {
maxTxt = $taFld.val();
maxTxt = maxTxt.substring(0, settings.charsMaxLimit);
$taFld.val(maxTxt);
charsLength = $taFld.val().length;
}
charsRemainingFn(charsLength);
}, 100);
});
this.keyup(function (event) {
charsRemainingFn($taFld.val().length);
});
this.keydown(function (event) {
var charsLength = $taFld.val().length;
if (settings.blockInputAtMaxLimit && charsLength >= settings.charsMaxLimit) {
// Except backspace
if (event.keyCode !== 8) {
event.preventDefault();
}
}
});
return this;
};
}(jQuery));
// Util: PatternFly C3 Chart Defaults
(function ($) {
'use strict';
$.fn.c3ChartDefaults = function () {
var getDefaultColors,
getDefaultDonut,
getDefaultDonutSize,
getDefaultDonutColors,
getDefaultDonutTooltip,
getDefaultDonutLegend,
getDefaultDonutConfig,
getDefaultSparklineArea,
getDefaultSparklineSize,
getDefaultSparklineAxis,
getDefaultSparklineLegend,
getDefaultSparklinePoint,
getDefaultSparklineTooltip,
getDefaultSparklineConfig;
getDefaultColors = function () {
return {
pattern: ['#0088ce', '#00659c', '#3f9c35', '#ec7a08', '#cc0000']
};
};
getDefaultDonut = function (title) {
return {
title: title,
label: {
show: false
},
width: 11
};
};
getDefaultDonutSize = function () {
return {
height: 171 // produces a diameter of 150 and a centered chart
};
};
getDefaultDonutColors = function () {
return {
pattern: ['#0088CE', '#D1D1D1']
};
};
getDefaultDonutTooltip = function () {
return {
show: false
};
};
getDefaultDonutLegend = function () {
return {
show: false
};
};
getDefaultDonutConfig = function (title) {
return {
donut: this.getDefaultDonut(title),
size: this.getDefaultDonutSize(),
legend: this.getDefaultDonutLegend(),
color: this.getDefaultDonutColors(),
tooltip: this.getDefaultDonutTooltip()
};
};
getDefaultSparklineArea = function () {
return {
zerobased: true
};
};
getDefaultSparklineSize = function () {
return {
height: 60
};
};
getDefaultSparklineAxis = function () {
return {
x: {
show: false
},
y: {
show: false
}
};
};
getDefaultSparklineLegend = function () {
return {
show: false
};
};
getDefaultSparklinePoint = function () {
return {
r: 1,
focus: {
expand: {
r: 4
}
}
};
};
getDefaultSparklineTooltip = function () {
return {
// because a sparkline should only contain a single data column,
// the tooltip will only work for a single data column
contents: function (d) {
return '<span class="c3-tooltip-sparkline">' + d[0].value + ' ' + d[0].name + '</span>';
}
};
};
getDefaultSparklineConfig = function () {
return {
area: getDefaultSparklineArea(),
size: getDefaultSparklineSize(),
axis: getDefaultSparklineAxis(),
color: getDefaultColors(),
legend: getDefaultSparklineLegend(),
point: getDefaultSparklinePoint(),
tooltip: getDefaultSparklineTooltip()
};
};
return {
getDefaultColors: getDefaultColors,
getDefaultDonut: getDefaultDonut,
getDefaultDonutSize: getDefaultDonutSize,
getDefaultDonutColors: getDefaultDonutColors,
getDefaultDonutTooltip: getDefaultDonutTooltip,
getDefaultDonutLegend: getDefaultDonutLegend,
getDefaultDonutConfig: getDefaultDonutConfig,
getDefaultSparklineArea: getDefaultSparklineArea,
getDefaultSparklineSize: getDefaultSparklineSize,
getDefaultSparklineAxis: getDefaultSparklineAxis,
getDefaultSparklineLegend: getDefaultSparklineLegend,
getDefaultSparklinePoint: getDefaultSparklinePoint,
getDefaultSparklineTooltip: getDefaultSparklineTooltip,
getDefaultSparklineConfig: getDefaultSparklineConfig
};
};
}(jQuery));
// Util: PatternFly Collapse with fixed heights
// Update the max-height of collapse elements based on the parent container's height.
(function ($) {
'use strict';
$.fn.initCollapseHeights = function () {
var parentElement = this, setCollapseHeights;
setCollapseHeights = function () {
var height, openPanel, contentHeight, bodyHeight, overflowY = 'hidden';
height = parentElement.height();
// Close any open panel
openPanel = parentElement.find('.collapse.in');
if (openPanel && openPanel.length > 0) {
openPanel.removeClass('in');
}
// Determine the necessary height for the closed content
contentHeight = 0;
parentElement.children().each($.proxy(function (i, element) {
var $element = $(element);
contentHeight += $element.outerHeight(true);
}, parentElement)).end();
// Determine the height remaining for opened collapse panels
bodyHeight = height - contentHeight;
// Make sure we have enough height to be able to scroll the contents if necessary
if (bodyHeight < 25) {
bodyHeight = 25;
// Allow the parent to scroll so the child elements are accessible
overflowY = 'auto';
}
// Set the max-height for the collapse panels
parentElement.find('[data-toggle="collapse"]').each($.proxy(function (i, element) {
var $element, selector, $target;
$element = $(element);
// Determine the selector to find the target
selector = $element.attr('data-target');
if (!selector) {
selector = $element.attr('href');
}
// Get the target and set the max-height
$target = $(selector);
$target.css({'max-height': bodyHeight + 'px', 'overflow-y': 'auto'});
}, parentElement)).end();
// Reopen the initially opened panel
if (openPanel && openPanel.length > 0) {
openPanel.addClass("in");
}
parentElement.css({'overflow-y': overflowY});
};
setCollapseHeights();
// Update on window resizing
$(window).resize(setCollapseHeights);
};
}(jQuery));
// Util: PatternFly TreeGrid Tables
(function ($) {
'use strict';
function getParent(rows, node) {
var parent = node.attr('data-parent');
if (typeof parent === "string") {
if (isNaN(parent)) {
parent = rows.closest(parent);
} else {
parent = $(rows[parseInt(parent, 10)]);
}
return parent;
}
}
function renderItem(item, parent) {
if (parent) {
parent.find('.treegrid-node > span.expand-icon')
.toggleClass('fa-angle-right', parent.hasClass('collapsed'))
.toggleClass('fa-angle-down', !parent.hasClass('collapsed'));
item.toggleClass('hidden', parent.hasClass('collapsed'));
if (parent.hasClass('collapsed')) {
item.addClass('collapsed');
}
}
}
function reStripe(tree) {
tree.find('tbody > tr').removeClass('odd');
tree.find('tbody > tr:not(.hidden):odd').addClass('odd');
}
$.fn.treegrid = function (options) {
var i, rows, _this;
rows = this.find('tbody > tr');
_this = this;
$.each(rows, function () {
var node, parent;
node = $(this);
parent = getParent(rows, node);
// Append expand icon dummies
node.children('.treegrid-node').prepend('<span class="icon expand-icon fa"/>');
// Set up an event listener for the node
node.children('.treegrid-node').on('click', function (e) {
var icon = node.find('span.expand-icon');
if (icon.hasClass('fa-angle-right')) {
node.removeClass('collapsed');
}
if (icon.hasClass('fa-angle-down')) {
node.addClass('collapsed');
}
$.each(rows.slice(rows.index(node) + 1), function () {
renderItem($(this), getParent(rows, $(this)));
});
reStripe(_this);
});
if (parent) {
// Calculate indentation depth
i = parent.find('.treegrid-node > span.indent').length + 1;
for (i; i > 0; i -= 1) {
node.children('.treegrid-node').prepend('<span class="indent"/>');
}
// Render expand/collapse icons
renderItem(node, parent);
}
});
reStripe(_this);
};
}(jQuery));