源碼先放在這里薄啥,以后來補充撞反,大部分注釋寫了鸦列,很好明白租冠。
/*
* angular-ui-bootstrap
* http://angular-ui.github.io/bootstrap/
* Version: 1.2.4 - 2016-03-06
* License: MIT
*/angular.module("ui.bootstrap", ["ui.bootstrap.tpls","ui.bootstrap.collapse"]);
angular.module("ui.bootstrap.tpls", []);
angular.module('ui.bootstrap.collapse', [])
.directive('uibCollapse', ['$animate', '$q', '$parse', '$injector', function($animate, $q, $parse, $injector) {
var $animateCss = $injector.has('$animateCss') ? $injector.get('$animateCss') : null;
return {
link: function(scope, element, attrs) {
var expandingExpr = $parse(attrs.expanding),//假如指令屬性中有expanding方法,則用$attr獲取
expandedExpr = $parse(attrs.expanded),//假如屬性中有expanded方法薯嗤,則用$attr獲取
collapsingExpr = $parse(attrs.collapsing),//假如屬性中有collapsingExpr方法顽爹,則用$attr獲取
collapsedExpr = $parse(attrs.collapsed);//假如屬性中有collapsedExpr方法,則用$attr獲取
//$parse和$eval都是解析字符串骆姐,區(qū)別是$parse是作為單獨的一個服務存在镜粤,$eval是作為單獨的一個scope方法來使用的
if (!scope.$eval(attrs.uibCollapse)) {//如果uib-collapse為false的話,就為元素添加如下樣式和屬性
element.addClass('in')
.addClass('collapse')
.attr('aria-expanded', true)
.attr('aria-hidden', false)
.css({height: 'auto'});
}
//展開方法玻褪,由shouldCollapse負責監(jiān)聽肉渴,并且通過$q來達到展開折疊前后事件發(fā)生的控制
function expand() {
if (element.hasClass('collapse') && element.hasClass('in')) { //如果已經是展開的就返回
return;
}
//查到的資料顯示 resolve是$q.deffer()的方法,不知這里為什么$q怎么回事带射。
$q.resolve(expandingExpr(scope))//成功執(zhí)行的信息黄虱,傳遞下去
.then(function() {
element.removeClass('collapse')
.addClass('collapsing')
.attr('aria-expanded', true)
.attr('aria-hidden', false);
if ($animateCss) {
$animateCss(element, {
addClass: 'in',
easing: 'ease',
to: { height: element[0].scrollHeight + 'px' }
}).start()['finally'](expandDone);
} else {
$animate.addClass(element, 'in', {
to: { height: element[0].scrollHeight + 'px' }
}).then(expandDone);
}
});
}
function expandDone() {
element.removeClass('collapsing')
.addClass('collapse')
.css({height: 'auto'});
expandedExpr(scope);
}
function collapse() {
if (!element.hasClass('collapse') && !element.hasClass('in')) {
return collapseDone();
}
$q.resolve(collapsingExpr(scope))
.then(function() {
element
// IMPORTANT: The height must be set before adding "collapsing" class.
// Otherwise, the browser attempts to animate from height 0 (in
// collapsing class) to the given height here.
.css({height: element[0].scrollHeight + 'px'})
// initially all panel collapse have the collapse class, this removal
// prevents the animation from jumping to collapsed state
.removeClass('collapse')
.addClass('collapsing')
.attr('aria-expanded', false)
.attr('aria-hidden', true);
if ($animateCss) {
$animateCss(element, {
removeClass: 'in',
to: {height: '0'}
}).start()['finally'](collapseDone);
} else {
$animate.removeClass(element, 'in', {
to: {height: '0'}
}).then(collapseDone);
}
});
}
function collapseDone() {
element.css({height: '0'}); // Required so that collapse works when animation is disabled
element.removeClass('collapsing')
.addClass('collapse');
collapsedExpr(scope);
}
//監(jiān)聽展開折疊變化
scope.$watch(attrs.uibCollapse, function(shouldCollapse) {
if (shouldCollapse) {
collapse();
} else {
expand();
}
});
}
};
}]);