123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- 'use strict';
- var directive = {};
- 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);
- }
- });
- }
- };
- }];
- 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);
- }
- }
- }
- 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);
- }
- }
- };
- }
- }]
- };
- };
- directive.table = function() {
- return {
- restrict: 'E',
- link: function(scope, element, attrs) {
- if (!attrs['class']) {
- element.addClass('table table-bordered table-striped code-table');
- }
- }
- };
- };
- var popoverElement = function() {
- var object = {
- 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();
- });
- },
- show : function(x,y) {
- this.element.addClass('visible');
- this.position(x || 0, y || 0);
- },
- hide : function() {
- this.element.removeClass('visible');
- this.position(-9999,-9999);
- },
- visible : function() {
- return this.position().y >= 0;
- },
- isSituatedAt : function(element) {
- return this.besideElement ? element[0] == this.besideElement[0] : false;
- },
- title : function(value) {
- return this.titleElement.html(value);
- },
- content : function(value) {
- if(value && value.length > 0) {
- value = marked(value);
- }
- return this.contentElement.html(value);
- },
- positionArrow : function(position) {
- this.node.className = 'popover ' + position;
- },
- positionAway : function() {
- this.besideElement = null;
- this.hide();
- },
- positionBeside : function(element) {
- this.besideElement = element;
- var elm = element[0];
- var x = elm.offsetLeft;
- var y = elm.offsetTop;
- x -= 30;
- y -= this.node.offsetHeight + 10;
- this.show(x,y);
- },
- 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;
- };
- 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);
- }
- });
- }
- }
- }];
- directive.tabPane = function() {
- return {
- require: '^tabbable',
- restrict: 'C',
- link: function(scope, element, attrs, tabsCtrl) {
- element.on('$remove', tabsCtrl.addPane(element, attrs));
- }
- };
- };
- 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 }).success(function(html) {
- loading = false;
- html = '<div class="foldout-inner">' +
- '<div calss="foldout-arrow"></div>' +
- html +
- '</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');
- }
- });
- });
- }
- }
- }];
- angular.module('bootstrap', [])
- .directive(directive)
- .factory('popoverElement', popoverElement)
- .run(function() {
- marked.setOptions({
- gfm: true,
- tables: true
- });
- });
|