117 lines
3.1 KiB

  1. /* =========================================================
  2. * bootstrap-tabdrop.js
  3. * http://www.eyecon.ro/bootstrap-tabdrop
  4. * =========================================================
  5. * Copyright 2012 Stefan Petre
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ========================================================= */
  19. !function( $ ) {
  20. var WinReszier = (function(){
  21. var registered = [];
  22. var inited = false;
  23. var timer;
  24. var resize = function(ev) {
  25. clearTimeout(timer);
  26. timer = setTimeout(notify, 100);
  27. };
  28. var notify = function() {
  29. for(var i=0, cnt=registered.length; i<cnt; i++) {
  30. registered[i].apply();
  31. }
  32. };
  33. return {
  34. register: function(fn) {
  35. registered.push(fn);
  36. if (inited === false) {
  37. $(window).bind('resize', resize);
  38. inited = true;
  39. }
  40. },
  41. unregister: function(fn) {
  42. for(var i=0, cnt=registered.length; i<cnt; i++) {
  43. if (registered[i] == fn) {
  44. delete registered[i];
  45. break;
  46. }
  47. }
  48. }
  49. }
  50. }());
  51. var TabDrop = function(element, options) {
  52. this.element = $(element);
  53. this.dropdown = $('<li class="dropdown hide pull-right tabdrop"><a class="dropdown-toggle" data-toggle="dropdown" href="#">'+options.text+' <b class="caret"></b></a><ul class="dropdown-menu"></ul></li>')
  54. .prependTo(this.element);
  55. if (this.element.parent().is('.tabs-below')) {
  56. this.dropdown.addClass('dropup');
  57. }
  58. WinReszier.register($.proxy(this.layout, this));
  59. this.layout();
  60. };
  61. TabDrop.prototype = {
  62. constructor: TabDrop,
  63. layout: function() {
  64. var collection = [];
  65. this.dropdown.removeClass('hide');
  66. this.element
  67. .append(this.dropdown.find('li'))
  68. .find('>li')
  69. .not('.tabdrop')
  70. .each(function(){
  71. if(this.offsetTop > 0) {
  72. collection.push(this);
  73. }
  74. });
  75. if (collection.length > 0) {
  76. collection = $(collection);
  77. this.dropdown
  78. .find('ul')
  79. .empty()
  80. .append(collection);
  81. if (this.dropdown.find('.active').length == 1) {
  82. this.dropdown.addClass('active');
  83. } else {
  84. this.dropdown.removeClass('active');
  85. }
  86. } else {
  87. this.dropdown.addClass('hide');
  88. }
  89. }
  90. }
  91. $.fn.tabdrop = function ( option ) {
  92. return this.each(function () {
  93. var $this = $(this),
  94. data = $this.data('tabdrop'),
  95. options = typeof option === 'object' && option;
  96. if (!data) {
  97. $this.data('tabdrop', (data = new TabDrop(this, $.extend({}, $.fn.tabdrop.defaults,options))));
  98. }
  99. if (typeof option == 'string') {
  100. data[option]();
  101. }
  102. })
  103. };
  104. $.fn.tabdrop.defaults = {
  105. text: '<i class="icon-align-justify"></i>'
  106. };
  107. $.fn.tabdrop.Constructor = TabDrop;
  108. }( window.jQuery );