リスト要素を分割するjQueryプラグイン。分割されたすべてのリストの要素セットを返す。
;(function (window, document, $) {
$.fn.devideList = function (options) {
options = $.extend({}, {
capacity: 5, // 1つのリスト内のアイテムの数
listClass: 'divided-list',
itemClass: 'divided-item',
/**
* リストアイテムがひとつ配置される度に実行する。
* @param i {number} アイテムのインデックス
* @param $item {jQuery Object} 配置されたアイテム
* @param nombre {number} アイテムが配置されたページ番号(ノンブル)(1~)
*/
afterSetOne: function (i, $item, nombre) {
$.noop();
},
/**
* リストの分割が終了したときに実行する。
* @param $elm {jQuery Object} 分割されたすべてのリストを含むオブジェクト
*/
afterSetAll: function ($elm) {
$.noop();
}
}, options),
$returned = $();
this.each(function (i) {
var $elm = $(this),
$item = $elm.find('> li'),
listClass = $elm.attr('class'),
$lastList = $elm,
itemLength = $item.length,
listType = this.tagName.toLowerCase(),
nombre = 1;
$returned = $returned.add($elm);
$elm.addClass(options.listClass);
if ($elm.attr('id')) {
var originalId = $elm.attr('id');
$elm.attr('id', originalId + '-' + nombre);
}
$item.each(function (i) {
var currentNombre = nombre;
$(this).addClass(options.itemClass);
if (i >= options.capacity) {
$lastList.append($(this));
}
if ((i + 1) % options.capacity === 0) {
nombre++;
$lastList = $('<' + listType + ' class=' + listClass + '></'+ listType + '>').addClass(options.listClass).insertAfter($lastList);
if (originalId) {
$lastList.attr('id', originalId + '-' + nombre);
}
$returned = $returned.add($lastList);
}
if (typeof options.afterSetOne === 'function') {
options.afterSetOne(i, $(this), currentNombre);
}
});
if (typeof options.callback === 'function') {
afterAll($elm);
}
});
return $returned;
};
}(window, document, jQuery, undefined));