|
(function ($) { |
|
$.dualTab = function (element, options){ |
|
// this.root に宣言オブジェクトを格納 |
|
// これを元にfindで要素を探す XSS 対策 |
|
this.root = $(element); |
|
|
|
this.selected = options.selected || "tab_open"; |
|
this.menus_ = options.menus || ".mod_tab_menu"; |
|
this.body_ = options.body || ".mod_tab_body"; |
|
|
|
this.menus = this.root.find(this.menus_); |
|
this.body = this.root.find(this.body_); |
|
|
|
|
|
// タブコンテンツを初期化(非表示にする) |
|
this.body.hide(); |
|
|
|
// メニュークリックの有効化 |
|
this.menuClick(); |
|
|
|
// 初期化時に location.hash を find でチェックする |
|
// 該当する要素が無いか length で確認する |
|
if (this.root.find(location.hash).length > 0) { |
|
this.root.find(location.hash).show(); |
|
|
|
// a[href=#<condition>] に一致する要素の親要素を current に設定する |
|
this.setCurrentTab(location.hash); |
|
|
|
} else { |
|
// 先頭のタブのclassを取得して同じクラス名がついているものにselectedを追加 |
|
// この場合tab1にselectedを追加 |
|
this.setFirstTab(); |
|
} |
|
} |
|
|
|
$.extend($.dualTab.prototype, { |
|
resetDualTab : function () { |
|
// タブメニューを初期化(カレントを消す) |
|
this.menus.removeClass(this.selected); |
|
|
|
// タブコンテンツを初期化(非表示にする) |
|
this.body.hide(); |
|
}, |
|
|
|
setFirstTab : function () { |
|
var target = this.menus.first().find("a").attr("href"); |
|
this.setCurrentMenu(target, false); |
|
this.setCurrentTab(target); |
|
}, |
|
|
|
setCurrentTab : function (target) { |
|
this.menus.find("a[href=" + target + "]").parent().addClass(this.selected); |
|
}, |
|
|
|
setCurrentMenu : function (target, setLocation) { |
|
if (setLocation) { |
|
location.hash = target; |
|
} |
|
this.setCurrentTab(target); |
|
this.root.find(target).fadeIn(); |
|
}, |
|
|
|
menuClick : function () { |
|
var self = this; |
|
|
|
this.menus.find("a").bind("click keypress", function (event) { |
|
event.preventDefault(); |
|
|
|
self.resetDualTab(); |
|
self.setCurrentMenu($(this).attr("href"), true); |
|
|
|
return false; |
|
}); |
|
} |
|
}); |
|
|
|
$.fn.dualTab = function (options) { |
|
return this.each(function(){ |
|
new $.dualTab(this, options) |
|
}); |
|
}; |
|
})(jQuery); |