/* Top toolbar area with buttons and title
----------------------------------------------------------------------------------------------------------------------*/
// TODO: rename all header-related things to "toolbar"
function Header(calendar, options) {
var t = this;
// exports
t.render = render;
t.destroy = destroy;
t.updateTitle = updateTitle;
t.activateButton = activateButton;
t.deactivateButton = deactivateButton;
t.disableButton = disableButton;
t.enableButton = enableButton;
t.getViewsWithButtons = getViewsWithButtons;
// locals
var el = $();
var viewsWithButtons = [];
var tm;
function render() {
var sections = options.header;
tm = options.theme ? 'ui' : 'fc';
if (sections) {
el = $("
")
.append(renderSection('left'))
.append(renderSection('right'))
.append(renderSection('center'))
.append('');
return el;
}
}
function destroy() {
el.remove();
}
function renderSection(position) {
var sectionEl = $('');
var buttonStr = options.header[position];
if (buttonStr) {
$.each(buttonStr.split(' '), function(i) {
var groupChildren = $();
var isOnlyButtons = true;
var groupEl;
$.each(this.split(','), function(j, buttonName) {
var buttonClick;
var themeIcon;
var normalIcon;
var defaultText;
var customText;
var innerHtml;
var classes;
var button;
if (buttonName == 'title') {
groupChildren = groupChildren.add($('
')); // we always want it to take up height
isOnlyButtons = false;
}
else {
if (calendar[buttonName]) { // a calendar method
buttonClick = function() {
calendar[buttonName]();
};
}
else if (fcViews[buttonName]) { // a view name
buttonClick = function() {
calendar.changeView(buttonName);
};
viewsWithButtons.push(buttonName);
}
if (buttonClick) {
// smartProperty allows different text per view button (ex: "Agenda Week" vs "Basic Week")
themeIcon = smartProperty(options.themeButtonIcons, buttonName);
normalIcon = smartProperty(options.buttonIcons, buttonName);
defaultText = smartProperty(options.defaultButtonText, buttonName);
customText = smartProperty(options.buttonText, buttonName);
if (customText) {
innerHtml = htmlEscape(customText);
}
else if (themeIcon && options.theme) {
innerHtml = "";
}
else if (normalIcon && !options.theme) {
innerHtml = "";
}
else {
innerHtml = htmlEscape(defaultText || buttonName);
}
classes = [
'fc-' + buttonName + '-button',
tm + '-button',
tm + '-state-default'
];
button = $( // type="button" so that it doesn't submit a form
''
)
.click(function() {
// don't process clicks for disabled buttons
if (!button.hasClass(tm + '-state-disabled')) {
buttonClick();
// after the click action, if the button becomes the "active" tab, or disabled,
// it should never have a hover class, so remove it now.
if (
button.hasClass(tm + '-state-active') ||
button.hasClass(tm + '-state-disabled')
) {
button.removeClass(tm + '-state-hover');
}
}
})
.mousedown(function() {
// the *down* effect (mouse pressed in).
// only on buttons that are not the "active" tab, or disabled
button
.not('.' + tm + '-state-active')
.not('.' + tm + '-state-disabled')
.addClass(tm + '-state-down');
})
.mouseup(function() {
// undo the *down* effect
button.removeClass(tm + '-state-down');
})
.hover(
function() {
// the *hover* effect.
// only on buttons that are not the "active" tab, or disabled
button
.not('.' + tm + '-state-active')
.not('.' + tm + '-state-disabled')
.addClass(tm + '-state-hover');
},
function() {
// undo the *hover* effect
button
.removeClass(tm + '-state-hover')
.removeClass(tm + '-state-down'); // if mouseleave happens before mouseup
}
);
groupChildren = groupChildren.add(button);
}
}
});
if (isOnlyButtons) {
groupChildren
.first().addClass(tm + '-corner-left').end()
.last().addClass(tm + '-corner-right').end();
}
if (groupChildren.length > 1) {
groupEl = $('');
if (isOnlyButtons) {
groupEl.addClass('fc-button-group');
}
groupEl.append(groupChildren);
sectionEl.append(groupEl);
}
else {
sectionEl.append(groupChildren); // 1 or 0 children
}
});
}
return sectionEl;
}
function updateTitle(text) {
el.find('h2').text(text);
}
function activateButton(buttonName) {
el.find('.fc-' + buttonName + '-button')
.addClass(tm + '-state-active');
}
function deactivateButton(buttonName) {
el.find('.fc-' + buttonName + '-button')
.removeClass(tm + '-state-active');
}
function disableButton(buttonName) {
el.find('.fc-' + buttonName + '-button')
.attr('disabled', 'disabled')
.addClass(tm + '-state-disabled');
}
function enableButton(buttonName) {
el.find('.fc-' + buttonName + '-button')
.removeAttr('disabled')
.removeClass(tm + '-state-disabled');
}
function getViewsWithButtons() {
return viewsWithButtons;
}
}