-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathangular-bootstrap.js
654 lines (604 loc) · 17.8 KB
/
angular-bootstrap.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
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
'use strict';
var directive = {};
/**
* @ngdoc directive
* @name bootstrap.directive:dropdownToggle
* @element button
* @restrict C
*
* @description
* Toggles a dropdown menu that follows the expect bootstrap layout.
*
* @example
<example module="bootstrap">
<file name="index.html">
<div class="dropdown">
<button id="toggleExample" class="btn btn-default dropdown-toggle" type="button">
Dropdown Toggle
<span class="caret" aria-hidden="true"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="toggleExample">
<li><a href="">Menu Item 1</a></li>
<li><a href="">Menu Item 2</a></li>
<li><a href="">Menu Item 3</a></li>
<li><a href="">Menu Item 4</a></li>
</ul>
</div>
</file>
</example>
*/
directive.dropdownToggle =
['$document', '$location', '$window',
function ($document, $location, $window) {
var openElement = null, close;
return {
restrict: 'C',
link: function(scope, element, attrs) {
scope.$watch(function dropdownTogglePathWatch(){return $location.path();}, function dropdownTogglePathWatchAction() {
close && close();
});
element.parent().on('click', function(event) {
close && close();
});
element.on('click', function(event) {
event.preventDefault();
event.stopPropagation();
var iWasOpen = false;
if (openElement) {
iWasOpen = openElement === element;
close();
}
if (!iWasOpen){
element.parent().addClass('open');
openElement = element;
close = function (event) {
event && event.preventDefault();
event && event.stopPropagation();
$document.off('click', close);
element.parent().removeClass('open');
close = null;
openElement = null;
}
$document.on('click', close);
}
});
}
};
}];
/**
* @ngdoc directive
* @name bootstrap.directive:syntax
* @restrict A
*
* @description
* Generates a Github, Plunkr or JSFiddle link that opens in a new tab,
* depending on the attributes provided.
*
* @example
<example module="bootstrap">
<file name="index.html">
<div syntax
syntax-github="https://github.com/angular-ui/grunt-uidocs-generator/"
syntax-plunkr="http://plnkr.co/edit/"
syntax-fiddle="https://jsfiddle.net/">
</div>
</file>
</example>
*/
directive.syntax = function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
function makeLink(type, text, link, icon) {
return '<a href="' + link + '" class="btn syntax-' + type + '" target="_blank" rel="nofollow">' +
'<span class="' + icon + '"></span> ' + text +
'</a>';
};
var html = '';
var types = {
'github' : {
text : 'View on Github',
key : 'syntaxGithub',
icon : 'icon-github'
},
'plunkr' : {
text : 'View on Plunkr',
key : 'syntaxPlunkr',
icon : 'icon-arrow-down'
},
'jsfiddle' : {
text : 'View on JSFiddle',
key : 'syntaxFiddle',
icon : 'icon-cloud'
}
};
for(var type in types) {
var data = types[type];
var link = attrs[data.key];
if(link) {
html += makeLink(type, data.text, link, data.icon);
}
};
var nav = document.createElement('nav');
nav.className = 'syntax-links';
nav.innerHTML = html;
var node = element[0];
var par = node.parentNode;
par.insertBefore(nav, node);
}
}
}
/**
* @ngdoc directive
* @name bootstrap.directive:tabbable
* @element ul
* @restrict C
*
* @description
* Generates bootstrap tabs and adds them inside the element with
* the tabbable class.
*/
directive.tabbable = function() {
return {
restrict: 'C',
compile: function(element) {
var navTabs = angular.element('<ul class="nav nav-tabs"></ul>'),
tabContent = angular.element('<div class="tab-content"></div>');
tabContent.append(element.contents());
element.append(navTabs).append(tabContent);
},
controller: ['$scope', '$element', function($scope, $element) {
var navTabs = $element.contents().eq(0),
ngModel = $element.controller('ngModel') || {},
tabs = [],
selectedTab;
ngModel.$render = function() {
var $viewValue = this.$viewValue;
if (selectedTab ? (selectedTab.value != $viewValue) : $viewValue) {
if(selectedTab) {
selectedTab.paneElement.removeClass('active');
selectedTab.tabElement.removeClass('active');
selectedTab = null;
}
if($viewValue) {
for(var i = 0, ii = tabs.length; i < ii; i++) {
if ($viewValue == tabs[i].value) {
selectedTab = tabs[i];
break;
}
}
if (selectedTab) {
selectedTab.paneElement.addClass('active');
selectedTab.tabElement.addClass('active');
}
}
}
};
this.addPane = function(element, attr) {
var li = angular.element('<li><a href></a></li>'),
a = li.find('a'),
tab = {
paneElement: element,
paneAttrs: attr,
tabElement: li
};
tabs.push(tab);
attr.$observe('value', update)();
attr.$observe('title', function(){ update(); a.text(tab.title); })();
function update() {
tab.title = attr.title;
tab.value = attr.value || attr.title;
if (!ngModel.$setViewValue && (!ngModel.$viewValue || tab == selectedTab)) {
// we are not part of angular
ngModel.$viewValue = tab.value;
}
ngModel.$render();
}
navTabs.append(li);
li.on('click', function(event) {
event.preventDefault();
event.stopPropagation();
if (ngModel.$setViewValue) {
$scope.$apply(function() {
ngModel.$setViewValue(tab.value);
ngModel.$render();
});
} else {
// we are not part of angular
ngModel.$viewValue = tab.value;
ngModel.$render();
}
});
return function() {
tab.tabElement.remove();
for(var i = 0, ii = tabs.length; i < ii; i++ ) {
if (tab == tabs[i]) {
tabs.splice(i, 1);
}
}
};
}
}]
};
};
/**
* @ngdoc directive
* @name bootstrap.directive:table
* @element table
* @restrict E
*
* @description
* Adds bootstrap classes to a table that has none.
*
* @example
<example module="bootstrap">
<file name="index.html">
<table>
<thead>
<tr>
<th>Hero Name</th>
<th>Real Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>Iron Man</td>
<td>Tony Stark</td>
</tr>
<tr>
<td>Hulk</td>
<td>Bruce Banner</td>
</tr>
<tr>
<td>Spider-Man</td>
<td>Peter Parker</td>
</tr>
<tr>
<td>Captain America</td>
<td>Steve Rogers</td>
</tr>
</tbody>
</table>
</file>
</example>
*/
directive.table = function() {
return {
restrict: 'E',
link: function(scope, element, attrs) {
if (!attrs['class']) {
element.addClass('table table-bordered table-striped code-table');
}
}
};
};
/**
* @ngdoc service
* @name bootstrap.service:popoverElement
*
* @description
* Provides helper functions for {@link bootstrap.directive:popover the popover directive}
*
*/
var popoverElement = function() {
var object = {
/**
* @ngdoc method
* @name init
* @kind function
* @methodOf bootstrap.service:popoverElement
* @description Attaches a popover element to the body of the page.
*/
init : function() {
this.element = angular.element(
'<div class="popover popover-incode top">' +
'<div class="arrow"></div>' +
'<div class="popover-inner">' +
'<div class="popover-title"><code></code></div>' +
'<div class="popover-content"></div>' +
'</div>' +
'</div>'
);
this.node = this.element[0];
this.element.css({
'display':'block',
'position':'absolute'
});
angular.element(document.body).append(this.element);
var inner = this.element.children()[1];
this.titleElement = angular.element(inner.childNodes[0].firstChild);
this.contentElement = angular.element(inner.childNodes[1]);
//stop the click on the tooltip
this.element.bind('click', function(event) {
event.preventDefault();
event.stopPropagation();
});
var self = this;
angular.element(document.body).bind('click',function(event) {
if(self.visible()) self.hide();
});
},
/**
* @ngdoc method
* @name show
* @kind function
* @methodOf bootstrap.service:popoverElement
* @description Adds visible class to the popover element on the page and
* set its position.
*/
show : function(x,y) {
this.element.addClass('visible');
this.position(x || 0, y || 0);
},
/**
* @ngdoc method
* @name hide
* @kind function
* @methodOf bootstrap.service:popoverElement
* @description Removes visible class to the popover element on the page and
* set its position to be out of bounds.
*/
hide : function() {
this.element.removeClass('visible');
this.position(-9999,-9999);
},
/**
* @ngdoc method
* @name visible
* @kind function
* @methodOf bootstrap.service:popoverElement
* @description Returns whether or not the popover element is visible.
* @returns {boolean} true if the elements y position is greater than 0.
*/
visible : function() {
return this.position().y >= 0;
},
/**
* @ngdoc method
* @name isSituatedAt
* @kind function
* @methodOf bootstrap.service:popoverElement
* @description Returns whether or not the popover element is situated
* next to the element passed in.
* @param {DOMElement} element Element to check against.
* @returns {boolean} Returns true is the current element is the besideElement
*/
isSituatedAt : function(element) {
return this.besideElement ? element[0] == this.besideElement[0] : false;
},
/**
* @ngdoc method
* @name title
* @kind function
* @methodOf bootstrap.service:popoverElement
* @description Updates the title value of the popover element.
* @param {string} value New title for the popover element.
* @returns {string} The new value of the title of the popover element.
*/
title : function(value) {
return this.titleElement.html(value);
},
/**
* @ngdoc method
* @name content
* @kind function
* @methodOf bootstrap.service:popoverElement
* @description Runs the value passed in through the marked library and
* updates the value of the content of the popover element.
* @param {string} value New content for the popover element.
* @returns {string} The new value of the content of the popover element.
*/
content : function(value) {
if(value && value.length > 0) {
value = marked(value);
}
return this.contentElement.html(value);
},
/**
* @ngdoc method
* @name positionArrow
* @kind function
* @methodOf bootstrap.service:popoverElement
* @description Adds the position class for the arrow on the popover element.
* @param {string} position Position class for popover arrow.
*/
positionArrow : function(position) {
this.node.className = 'popover ' + position;
},
/**
* @ngdoc method
* @name positionAway
* @kind function
* @methodOf bootstrap.service:popoverElement
* @description Sets besideElement to null and hides the popover element.
*/
positionAway : function() {
this.besideElement = null;
this.hide();
},
/**
* @ngdoc method
* @name positionBeside
* @kind function
* @methodOf bootstrap.service:popoverElement
* @description Sets besideElement to element passed in and
* shows the popover element.
* @param {DOMElement} element Element to position the popover besides.
*/
positionBeside : function(element) {
this.besideElement = element;
var elm = element[0],
x = element[0].getBoundingClientRect().left,
y = elm.offsetTop - 20;
this.show(x, y);
},
/**
* @ngdoc method
* @name position
* @kind function
* @methodOf bootstrap.service:popoverElement
* @description Sets the top and left position of the popover element if
* x and y are not null. Otherwise returns the x and y coordinates of
* the popover element.
* @param {number} x Left position for the popover element.
* @param {number} y Top position for the popover element.
* @returns {object | null} The current x and y coordinates of the popover
* element or nothing if valid x and y coordinates are provided.
*/
position : function(x, y) {
if(x != null && y != null) {
this.element.css('left',x + 'px');
this.element.css('top', y + 'px');
} else {
return {
x: this.node.offsetLeft,
y: this.node.offsetTop
};
}
}
};
object.init();
object.hide();
return object;
};
/**
* @ngdoc directive
* @name bootstrap.directive:popover
* @element button
* @restrict A
*
* @description
* Opens a bootstrap popover element with the data from the tile and content tags.
*
* @example
<example module="bootstrap">
<file name="index.html">
<button popover
type="button"
class="btn btn-default"
title="Popover Title"
content="Popover Content">
Open Popover
</button>
</file>
</example>
*/
directive.popover = ['popoverElement', function(popover) {
return {
restrict: 'A',
priority : 500,
link: function(scope, element, attrs) {
element.bind('click',function(event) {
event.preventDefault();
event.stopPropagation();
if(popover.isSituatedAt(element) && popover.visible()) {
popover.title('');
popover.content('');
popover.positionAway();
} else {
popover.title(attrs.title);
popover.content(attrs.content);
popover.positionBeside(element);
}
});
}
}
}];
/**
* @ngdoc directive
* @name bootstrap.directive:tabPane
* @requires bootstrap.directive:tabbable
* @element li
* @restrict C
*
* @description
* Add tabs to the tabbale element generated by tabbable directive.
*/
directive.tabPane = function() {
return {
require: '^tabbable',
restrict: 'C',
link: function(scope, element, attrs, tabsCtrl) {
element.on('$remove', tabsCtrl.addPane(element, attrs));
}
};
};
/**
* @ngdoc directive
* @name bootstrap.directive:foldout
* @element button
* @restrict A
*
* @description
* Makes a button open a foldout to the target of the button.
*
* @example
<example module="bootstrap">
<file name="index.html">
<div class="foldout-container">
<button foldout
class="btn btn-default" type="button"
url="/partials/api/bootstrap.html">
Show Foldout
</button>
</div>
</file>
</example>
*/
directive.foldout = ['$http', '$animate','$window', function($http, $animate, $window) {
return {
restrict: 'A',
priority : 500,
link: function(scope, element, attrs) {
var container, loading, url = attrs.url;
if(/\/build\//.test($window.location.href)) {
url = '/build/docs' + url;
}
element.bind('click',function() {
scope.$apply(function() {
if(!container) {
if(loading) return;
loading = true;
var par = element.parent();
container = angular.element('<div class="foldout">loading...</div>');
$animate.enter(container, null, par);
$http.get(url, {cache: true}).then(function(response) {
loading = false;
var html = '<div class="foldout-inner">' +
'<div class="foldout-arrow"></div>' +
response.data +
'</div>';
container.html(html);
//avoid showing the element if the user has already closed it
if(container.css('display') == 'block') {
container.css('display','none');
$animate.addClass(container, 'ng-hide');
}
});
} else {
container.hasClass('ng-hide') ? $animate.removeClass(container, 'ng-hide') : $animate.addClass(container, 'ng-hide');
}
});
});
}
}
}];
/**
* @ngdoc overview
* @name bootstrap
*
* @description
* # bootstrap
*
* This module provides an angular wrapper to a few of the bootstrap
* components that are used by grunt-uidocs-generator.
*/
angular.module('bootstrap', [])
.directive(directive)
.factory('popoverElement', popoverElement)
.run(function() {
marked.setOptions({
gfm: true,
tables: true
});
});