Header.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. function Header(calendar, options) {
  2. var t = this;
  3. // exports
  4. t.render = render;
  5. t.destroy = destroy;
  6. t.updateTitle = updateTitle;
  7. t.activateButton = activateButton;
  8. t.deactivateButton = deactivateButton;
  9. t.disableButton = disableButton;
  10. t.enableButton = enableButton;
  11. // locals
  12. var element = $([]);
  13. var tm;
  14. function render() {
  15. tm = options.theme ? 'ui' : 'fc';
  16. var sections = options.header;
  17. if (sections) {
  18. element = $("<table class='fc-header'/>")
  19. .append($("<tr/>")
  20. .append($("<td class='fc-header-left'/>")
  21. .append(renderSection(sections.left)))
  22. .append($("<td class='fc-header-center'/>")
  23. .append(renderSection(sections.center)))
  24. .append($("<td class='fc-header-right'/>")
  25. .append(renderSection(sections.right))));
  26. return element;
  27. }
  28. }
  29. function destroy() {
  30. element.remove();
  31. }
  32. function renderSection(buttonStr) {
  33. if (buttonStr) {
  34. var tr = $("<tr/>");
  35. $.each(buttonStr.split(' '), function(i) {
  36. if (i > 0) {
  37. tr.append("<td><span class='fc-header-space'/></td>");
  38. }
  39. var prevButton;
  40. $.each(this.split(','), function(j, buttonName) {
  41. if (buttonName == 'title') {
  42. tr.append("<td><h2 class='fc-header-title'>&nbsp;</h2></td>");
  43. if (prevButton) {
  44. prevButton.addClass(tm + '-corner-right');
  45. }
  46. prevButton = null;
  47. }else{
  48. var buttonClick;
  49. if (calendar[buttonName]) {
  50. buttonClick = calendar[buttonName]; // calendar method
  51. }
  52. else if (fcViews[buttonName]) {
  53. buttonClick = function() {
  54. button.removeClass(tm + '-state-hover'); // forget why
  55. calendar.changeView(buttonName);
  56. };
  57. }
  58. if (buttonClick) {
  59. if (prevButton) {
  60. prevButton.addClass(tm + '-no-right');
  61. }
  62. var button;
  63. var icon = options.theme ? smartProperty(options.buttonIcons, buttonName) : null;
  64. var text = smartProperty(options.buttonText, buttonName);
  65. if (icon) {
  66. button = $("<div class='fc-button-" + buttonName + " ui-state-default'>" +
  67. "<a><span class='ui-icon ui-icon-" + icon + "'/></a></div>");
  68. }
  69. else if (text) {
  70. button = $("<div class='fc-button-" + buttonName + " " + tm + "-state-default'>" +
  71. "<a><span>" + text + "</span></a></div>");
  72. }
  73. if (button) {
  74. button
  75. .click(function() {
  76. if (!button.hasClass(tm + '-state-disabled')) {
  77. buttonClick();
  78. }
  79. })
  80. .mousedown(function() {
  81. button
  82. .not('.' + tm + '-state-active')
  83. .not('.' + tm + '-state-disabled')
  84. .addClass(tm + '-state-down');
  85. })
  86. .mouseup(function() {
  87. button.removeClass(tm + '-state-down');
  88. })
  89. .hover(
  90. function() {
  91. button
  92. .not('.' + tm + '-state-active')
  93. .not('.' + tm + '-state-disabled')
  94. .addClass(tm + '-state-hover');
  95. },
  96. function() {
  97. button
  98. .removeClass(tm + '-state-hover')
  99. .removeClass(tm + '-state-down');
  100. }
  101. )
  102. .appendTo($("<td/>").appendTo(tr));
  103. if (prevButton) {
  104. prevButton.addClass(tm + '-no-right');
  105. }else{
  106. button.addClass(tm + '-corner-left');
  107. }
  108. prevButton = button;
  109. }
  110. }
  111. }
  112. });
  113. if (prevButton) {
  114. prevButton.addClass(tm + '-corner-right');
  115. }
  116. });
  117. return $("<table/>").append(tr);
  118. }
  119. }
  120. function updateTitle(html) {
  121. element.find('h2.fc-header-title')
  122. .html(html);
  123. }
  124. function activateButton(buttonName) {
  125. element.find('div.fc-button-' + buttonName)
  126. .addClass(tm + '-state-active');
  127. }
  128. function deactivateButton(buttonName) {
  129. element.find('div.fc-button-' + buttonName)
  130. .removeClass(tm + '-state-active');
  131. }
  132. function disableButton(buttonName) {
  133. element.find('div.fc-button-' + buttonName)
  134. .addClass(tm + '-state-disabled');
  135. }
  136. function enableButton(buttonName) {
  137. element.find('div.fc-button-' + buttonName)
  138. .removeClass(tm + '-state-disabled');
  139. }
  140. }