Header.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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' style='width:100%'/>")
  19. .append(
  20. $("<tr/>")
  21. .append(renderSection('left'))
  22. .append(renderSection('center'))
  23. .append(renderSection('right'))
  24. );
  25. return element;
  26. }
  27. }
  28. function destroy() {
  29. element.remove();
  30. }
  31. function renderSection(position) {
  32. var e = $("<td class='fc-header-" + position + "'/>");
  33. var buttonStr = options.header[position];
  34. if (buttonStr) {
  35. $.each(buttonStr.split(' '), function(i) {
  36. if (i > 0) {
  37. e.append("<span class='fc-header-space'/>");
  38. }
  39. var prevButton;
  40. $.each(this.split(','), function(j, buttonName) {
  41. if (buttonName == 'title') {
  42. e.append("<span class='fc-header-title'><h2>&nbsp;</h2></span>");
  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. // smartProperty allows different text per view button (ex: "Agenda Week" vs "Basic Week")
  60. var themeIcon = smartProperty(options.themeButtonIcons, buttonName);
  61. var normalIcon = smartProperty(options.buttonIcons, buttonName);
  62. var defaultText = smartProperty(options.defaultButtonText, buttonName);
  63. var customText = smartProperty(options.buttonText, buttonName);
  64. var html;
  65. if (customText) {
  66. html = htmlEscape(customText);
  67. }
  68. else if (themeIcon && options.theme) {
  69. html = "<span class='ui-icon ui-icon-" + themeIcon + "'></span>";
  70. }
  71. else if (normalIcon && !options.theme) {
  72. html = "<span class='fc-icon fc-icon-" + normalIcon + "'></span>";
  73. }
  74. else {
  75. html = htmlEscape(defaultText || buttonName);
  76. }
  77. var button = $(
  78. "<span class='fc-button fc-button-" + buttonName + " " + tm + "-state-default'>" +
  79. html +
  80. "</span>"
  81. )
  82. .click(function() {
  83. if (!button.hasClass(tm + '-state-disabled')) {
  84. buttonClick();
  85. }
  86. })
  87. .mousedown(function() {
  88. button
  89. .not('.' + tm + '-state-active')
  90. .not('.' + tm + '-state-disabled')
  91. .addClass(tm + '-state-down');
  92. })
  93. .mouseup(function() {
  94. button.removeClass(tm + '-state-down');
  95. })
  96. .hover(
  97. function() {
  98. button
  99. .not('.' + tm + '-state-active')
  100. .not('.' + tm + '-state-disabled')
  101. .addClass(tm + '-state-hover');
  102. },
  103. function() {
  104. button
  105. .removeClass(tm + '-state-hover')
  106. .removeClass(tm + '-state-down');
  107. }
  108. )
  109. .appendTo(e);
  110. disableTextSelection(button);
  111. if (!prevButton) {
  112. button.addClass(tm + '-corner-left');
  113. }
  114. prevButton = button;
  115. }
  116. }
  117. });
  118. if (prevButton) {
  119. prevButton.addClass(tm + '-corner-right');
  120. }
  121. });
  122. }
  123. return e;
  124. }
  125. function updateTitle(html) {
  126. element.find('h2')
  127. .html(html);
  128. }
  129. function activateButton(buttonName) {
  130. element.find('span.fc-button-' + buttonName)
  131. .addClass(tm + '-state-active');
  132. }
  133. function deactivateButton(buttonName) {
  134. element.find('span.fc-button-' + buttonName)
  135. .removeClass(tm + '-state-active');
  136. }
  137. function disableButton(buttonName) {
  138. element.find('span.fc-button-' + buttonName)
  139. .addClass(tm + '-state-disabled');
  140. }
  141. function enableButton(buttonName) {
  142. element.find('span.fc-button-' + buttonName)
  143. .removeClass(tm + '-state-disabled');
  144. }
  145. }