Created
November 12, 2012 01:02
-
-
Save ericzou/4056997 to your computer and use it in GitHub Desktop.
angular-js-bootstrap-0.1.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module("ui.bootstrap", ["ui.bootstrap.accordion","ui.bootstrap.dropdownToggle","ui.bootstrap.modal","ui.bootstrap.tabs"]); | |
angular.module('ui.bootstrap.accordion', []); | |
angular.module('ui.bootstrap.accordion').controller('AccordionController', ['$scope', function ($scope) { | |
var groups = $scope.groups = []; | |
this.select = function(group) { | |
angular.forEach(groups, function (group) { | |
group.selected = false; | |
}); | |
group.selected = true; | |
}; | |
this.toggle = function(group) { | |
if (group.selected) { | |
group.selected = false; | |
} else { | |
this.select(group); | |
} | |
}; | |
this.addGroup = function(group) { | |
groups.push(group); | |
if(group.selected) { | |
this.select(group); | |
} | |
}; | |
this.removeGroup = function(group) { | |
groups.splice(groups.indexOf(group), 1); | |
}; | |
}]); | |
/* accordion: Bootstrap accordion implementation | |
* @example | |
<accordion> | |
<accordion-group title="sth">Static content</accordion-group> | |
<accordion-group title="sth">Static content - is it? {{sth}}</accordion-group> | |
<accordion-group title="group.title" ng-repeat="group in groups">{{group.content}}</accordion-group> | |
</accordion> | |
*/ | |
angular.module('ui.bootstrap.accordion').directive('accordion', function () { | |
return { | |
restrict:'E', | |
transclude:true, | |
scope:{}, | |
controller:'AccordionController', | |
templateUrl:'template/accordion/accordion.html' | |
}; | |
}); | |
angular.module('ui.bootstrap.accordion').directive('accordionGroup', function () { | |
return { | |
require:'^accordion', | |
restrict:'E', | |
transclude:true, | |
scope:{ | |
title:'=' | |
}, | |
link: function(scope, element, attrs, accordionCtrl) { | |
accordionCtrl.addGroup(scope); | |
scope.select = function() { | |
accordionCtrl.select(scope); | |
}; | |
scope.toggle = function() { | |
accordionCtrl.toggle(scope); | |
}; | |
scope.$on('$destroy', function (event) { | |
accordionCtrl.removeGroup(scope); | |
}); | |
}, | |
templateUrl:'template/accordion/accordion-group.html', | |
replace:true | |
}; | |
}); | |
/* | |
* dropdownToggle - Provides dropdown menu functionality in place of bootstrap js | |
* @restrict class or attribute | |
* @example: | |
<li class="dropdown"> | |
<a class="dropdown-toggle">My Dropdown Menu</a> | |
<ul class="dropdown-menu"> | |
<li ng-repeat="choice in dropChoices"> | |
<a ng-href="{{choice.href}}>{{choice.text}}</a> | |
</li> | |
</ul> | |
</li> | |
*/ | |
angular.module('ui.bootstrap.dropdownToggle', []).directive('dropdownToggle', | |
['$document', '$location', '$window', function ($document, $location, $window) { | |
var openElement = null, close; | |
return { | |
restrict: 'CA', | |
link: function(scope, element, attrs) { | |
scope.$watch(function dropdownTogglePathWatch(){return $location.path();}, function dropdownTogglePathWatchAction() { | |
if (close) { close(); } | |
}); | |
element.parent().bind('click', function(event) { | |
if (close) { close(); } | |
}); | |
element.bind('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) { | |
if (event) { | |
event.preventDefault(); | |
event.stopPropagation(); | |
} | |
$document.unbind('click', close); | |
element.parent().removeClass('open'); | |
close = null; | |
openElement = null; | |
}; | |
$document.bind('click', close); | |
} | |
}); | |
} | |
}; | |
}]); | |
angular.module('ui.bootstrap.modal', []).directive('modal', ['$parse',function($parse) { | |
var backdropEl; | |
var body = angular.element(document.getElementsByTagName('body')[0]); | |
var defaultOpts = { | |
backdrop: true, | |
escape: true | |
}; | |
return { | |
restrict: 'ECA', | |
link: function(scope, elm, attrs) { | |
var opts = angular.extend(defaultOpts, scope.$eval(attrs.uiOptions || attrs.bsOptions || attrs.options)); | |
var shownExpr = attrs.modal || attrs.show; | |
var setClosed; | |
if (attrs.close) { | |
setClosed = function() { | |
scope.$apply(attrs.close); | |
}; | |
} else { | |
setClosed = function() { | |
scope.$apply(function() { | |
$parse(shownExpr).assign(scope, false); | |
}); | |
}; | |
} | |
elm.addClass('modal'); | |
if (opts.backdrop && !backdropEl) { | |
backdropEl = angular.element('<div class="modal-backdrop"></div>'); | |
backdropEl.css('display','none'); | |
body.append(backdropEl); | |
} | |
function setShown(shown) { | |
scope.$apply(function() { | |
model.assign(scope, shown); | |
}); | |
} | |
function escapeClose(evt) { | |
if (evt.which === 27) { setClosed(); } | |
} | |
function clickClose() { | |
setClosed(); | |
} | |
function close() { | |
if (opts.escape) { body.unbind('keyup', escapeClose); } | |
if (opts.backdrop) { | |
backdropEl.css('display', 'none').removeClass('in'); | |
backdropEl.unbind('click', clickClose); | |
} | |
elm.css('display', 'none').removeClass('in'); | |
body.removeClass('modal-open'); | |
} | |
function open() { | |
if (opts.escape) { body.bind('keyup', escapeClose); } | |
if (opts.backdrop) { | |
backdropEl.css('display', 'block').addClass('in'); | |
backdropEl.bind('click', clickClose); | |
} | |
elm.css('display', 'block').addClass('in'); | |
body.addClass('modal-open'); | |
} | |
scope.$watch(shownExpr, function(isShown, oldShown) { | |
if (isShown) { | |
open(); | |
} else { | |
close(); | |
} | |
}); | |
} | |
}; | |
}]); | |
angular.module('ui.bootstrap.tabs', []) | |
.controller('TabsController', ['$scope', '$element', function($scope, $element) { | |
var panes = $scope.panes = []; | |
$scope.select = function selectPane(pane) { | |
angular.forEach(panes, function(pane) { | |
pane.selected = false; | |
}); | |
pane.selected = true; | |
}; | |
this.addPane = function addPane(pane) { | |
if (!panes.length) { | |
$scope.select(pane); | |
} | |
panes.push(pane); | |
}; | |
this.removePane = function removePane(pane) { | |
var index = panes.indexOf(pane); | |
panes.splice(index, 1); | |
//Select a new pane if removed pane was selected | |
if (pane.selected && panes.length > 0) { | |
$scope.select(panes[index < panes.length ? index : index-1]); | |
} | |
}; | |
}]) | |
.directive('tabs', function() { | |
return { | |
restrict: 'E', | |
transclude: true, | |
scope: {}, | |
controller: 'TabsController', | |
templateUrl: 'template/tabs/tabs.html', | |
replace: true | |
}; | |
}) | |
.directive('pane', function() { | |
return { | |
require: '^tabs', | |
restrict: 'E', | |
transclude: true, | |
scope: { title: '@' }, | |
link: function(scope, element, attrs, tabsCtrl) { | |
tabsCtrl.addPane(scope); | |
scope.$on('$destroy', function() { | |
tabsCtrl.removePane(scope); | |
}); | |
}, | |
templateUrl: 'template/tabs/pane.html', | |
replace: true | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment