(
function
(avalon) {
var
defaults = {
active: 0,
event:
"click"
,
collapsible:
false
,
bottom:
false
,
removable:
false
};
avalon.ui.tabs =
function
(element, id, opts) {
var
el, tabsParent, tabs = [], tabpanels = [];
var
$element = avalon(element);
var
optiOns= avalon.mix({}, defaults);
if
(
typeof
opts ===
"object"
) {
avalon.mix(options, opts.$json || opts);
}
avalon.mix(options, $element.data());
$element.addClass(
"ui-tabs ui-widget ui-widget-content ui-corner-all"
);
while
(el = element.firstChild) {
if
(!tablist && (el.tagName ===
"UL"
|| el.tagName ===
"OL"
)) {
tabsParent = el;
}
if
(el.tagName ===
"DIV"
) {
tabpanels.push(el.innerHTML);
}
element.removeChild(el);
}
for
(
var
i = 0; el = tabsParent.children[i++]; ) {
tabs.push(el.innerHTML);
}
var
tablist =
'
' ms-class-ui-corner-all="!bottom" ms-class-ui-corner-bottom="bottom" ms-each-tab="tabs">'
+
' +
' ms-class-ui-corner-top="!bottom"'
+
' ms-class-ui-corner-bottom="bottom"'
+
' ms-class-ui-tabs-active="active == $index"'
+
' ms-class-ui-state-active="active == $index"'
+
' ms-'
+ options.event +
'="activate"'
+
' ms-hover="ui-state-hover"'
+
' >{{tab|html}}'
;
var
panels =
' +
' ms-class-ui-corner-bottom="!bottom"'
+
' ms-visible="active == $index" >{{panel|html}}
'
;
var
model = avalon.define(id,
function
(vm) {
vm.active = options.active;
vm.collapsible = options.collapsible;
vm.tabs = tabs;
vm.tabpanels = tabpanels;
vm.removable = options.removable;
vm.activate =
function
(e) {
e.preventDefault();
vm.active =
this
.$scope.$index;
};
vm.remove =
function
(e) {
e.preventDefault();
var
index =
this
.$scope.$index;
vm.tabs.removeAt(index);
vm.tabpanels.removeAt(index);
avalon.nextTick(
function
() {
vm.active = 0;
});
};
vm.bottom = options.bottom;
});
avalon.nextTick(
function
() {
element.innerHTML = options.bottom ? panels + tablist : tablist + panels;
element.setAttribute(
"ms-class-ui-tabs-collapsible"
,
"collapsible"
);
element.setAttribute(
"ms-class-tabs-bottom"
,
"bottom"
);
avalon.scan(element, model);
});
return
model;
};
})(window.avalon);