jquery.rowOps.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * rowOps,行功能键控件
  3. */
  4. (function($) {
  5. $.fn.rowOps = function() {
  6. var me = $(this),
  7. ops_container = $("<div></div>"),
  8. ops_btn = $("<div></div>"),
  9. clear_bosh = $("<div></div>")
  10. .css("clear", "both"),
  11. ops_box = $("<div></div>")
  12. .addClass("ops_box ops_itemDiv"),
  13. ops_ul = $("<ul></ul>"),
  14. ops_em = $("<em></em>")
  15. .addClass("ops_shadeEm")
  16. hasCommon = false,
  17. num = me.children("a").length,
  18. widthed = 0;
  19. if(num<2){//少于N个按钮,直接显示,不隐藏
  20. me.css('width',num*60);//每个按钮长度60px
  21. return;
  22. }
  23. me.attr("nowrap", "nowrap").append(ops_container);
  24. ops_btn.addClass("ops_btn").attr("href", "javascript:;").attr("title",
  25. "更多操作");
  26. ops_box.append(ops_ul);
  27. ops_container.append(ops_btn).append(clear_bosh).append(ops_box)
  28. .append(ops_em);
  29. //去掉空格、回车和换行符
  30. function trimBr(key){
  31. key = key.replace(/\s+/g, "");
  32. key =key.replace(/[\r\n]/g,"")
  33. return key;
  34. }
  35. me.find("a").each(function(i,n) {
  36. var _self = $(this), isCommon = _self.attr("isCommon");
  37. if (typeof isCommon == "undefined" || isCommon != "true") {
  38. _self.addClass("ops_more");
  39. var ops_li = $("<li></li>").addClass("ops_li").append(_self);
  40. ops_ul.append(ops_li);
  41. var aText =_self.text(), textLength=0;
  42. if(aText.length>0)
  43. aText = trimBr(aText);
  44. // 根据<a>的个数,设置ul的宽度,从而变成横排
  45. textLength = aText.length>0?(aText.length*14):0;
  46. //字的宽度+偏移的宽度+图标的宽度
  47. widthed += (textLength+5+26);
  48. if(i+1==num){
  49. ops_ul.css("width",widthed);
  50. }
  51. } else {
  52. hasCommon = true;
  53. }
  54. });
  55. if (hasCommon) {
  56. ops_container.addClass("ops_container_type1");
  57. } else {
  58. ops_container.addClass("ops_container_type2");
  59. }
  60. var button = ops_btn,
  61. box = ops_box,
  62. shade = ops_em,
  63. table = me
  64. .closest("table");
  65. var otherHide = function() {
  66. table.find(".ops_box").hide();
  67. table.find(".ops_shadeEm").hide();
  68. table.find(".ops_btn").removeClass('ops_active');
  69. };
  70. me.hoverDelay({
  71. outDuring: 1000,
  72. hoverEvent: function(){
  73. otherHide();
  74. box.show();
  75. shade.show();
  76. button.addClass('ops_active');
  77. },
  78. outEvent: function(){
  79. box.hide();
  80. shade.hide();
  81. button.removeClass('ops_active');
  82. }
  83. });
  84. /*
  85. button.mouseup(function(event) {
  86. if (button.hasClass("ops_active")) {
  87. otherHide();
  88. } else {
  89. otherHide();
  90. box.toggle();
  91. shade.toggle();
  92. button.toggleClass('ops_active');
  93. }
  94. event.stopPropagation();
  95. });
  96. $("body").mouseup(function() {
  97. otherHide();
  98. });
  99. */
  100. };
  101. /*
  102. * hoverDuring 鼠标经过的延时时间
  103. outDuring 鼠标移出的延时时间
  104. hoverEvent 鼠标经过执行的方法
  105. outEvent 鼠标移出执行的方法
  106. */
  107. $.fn.hoverDelay = function(options){
  108. var defaults = {
  109. hoverDuring: 200,
  110. outDuring: 200,
  111. hoverEvent: function(){
  112. $.noop();
  113. },
  114. outEvent: function(){
  115. $.noop();
  116. }
  117. };
  118. var sets = $.extend(defaults,options || {});
  119. var hoverTimer, outTimer, that = this;
  120. return $(this).each(function(){
  121. $(this).hover(function(){
  122. clearTimeout(outTimer);
  123. hoverTimer = setTimeout(function(){sets.hoverEvent.apply(that)}, sets.hoverDuring);
  124. },function(){
  125. clearTimeout(hoverTimer);
  126. outTimer = setTimeout(function(){sets.outEvent.apply(that)}, sets.outDuring);
  127. });
  128. });
  129. }
  130. })(jQuery);
  131. $(function(){
  132. $.extend({initRowOps:function(){
  133. /** 初始化 */
  134. try{
  135. $(".rowOps").each(function() {
  136. $(this).rowOps();
  137. });
  138. }catch(e){}
  139. }});
  140. $.initRowOps();
  141. });