Skip to content

Instantly share code, notes, and snippets.

@Tenderfeel
Last active September 19, 2017 03:37
Show Gist options
  • Save Tenderfeel/5cf17987bf56f9d3f8fb1614efa64cdf to your computer and use it in GitHub Desktop.
Save Tenderfeel/5cf17987bf56f9d3f8fb1614efa64cdf to your computer and use it in GitHub Desktop.
/**
* jQuery Pager plugin
* Licensed under the MIT license
* This plugin is refer to how to write "jQuery ScrollSpy Plugin".
*/
(function(factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof exports !== 'undefined') {
module.exports = factory(require('jquery'));
} else {
factory(jQuery);
}
}(function($) {
$.fn.extend({
pager: function pager(options, action) {
// If the options parameter is a string, then assume it's an 'action', therefore swap the parameters around
if (_isString(options)) {
var tempOptions = action;
// Set the action as the option parameter
action = options;
// Set to be the reference action pointed to
options = tempOptions;
}
// override the default options with those passed to the plugin
options = $.extend(true, {}, _defaults, options);
// sanitize the following option with the default value if the predicate fails
_sanitizeOption(options, _defaults, 'namespace', _isString);
_sanitizeOption(options, _defaults, 'elements', _isObject);
_sanitizeOption(options, _defaults, 'step', _isNumeric);
_sanitizeOption(options, _defaults, 'current', _isNumeric);
_sanitizeOption(options, _defaults, 'total', _isNumeric);
_sanitizeOption(options, _defaults, 'onPageing', $.isFunction);
var _current = options.current, //現在のページ
_total = options.total; //総ページ数
if(_isString(action)) {
switch(action) {
case 'set':
if(options.current) {
_current = options.current;
}
if(options.total) {
_current = options.total;
}
_setElements($(this), options, _current, _total);
break;
default:
}
}
return this.each(function each() {
var _this = this;
var $element = $(_this);
var $prev = $element.find(options.elements.prev),
$next = $element.find(options.elements.next),
$current = $element.find(options.elements.current),
$total = $element.find(options.elements.total);
if($prev && $prev.length) {
$prev.toggle(_current > 1);
}
if($next && $next.length) {
$next.toggle(_current < _total);
}
$element.on('click' + options.namespace, options.elements.prev + ',' + options.elements.next, function (e) {
e.preventDefault();
if($(this).hasClass(options.elements.next.replace('.', ''))) {
_current = Math.min(_current + 1, _total);
} else {
_current = Math.max(_current - 1, 1);
}
$current.text(_current);
if($prev && $prev.length) {
$prev.toggle(_current > 1);
}
if($next && $next.length) {
$next.toggle(_current < _total);
}
if (options.onPaging !== null) {
options.onPaging(e, options, _current, _total);
}
});
});
}
});
function _setElements($element, options, current, total) {
var $prev = $element.find(options.elements.prev),
$next = $element.find(options.elements.next),
$current = $element.find(options.elements.current),
$total = $element.find(options.elements.total);
if($prev && $prev.length) {
$prev.toggle(current > 1);
}
if($next && $next.length) {
$next.toggle(current < total);
}
$current.text(current);
$total.text(total);
}
// default options
var _defaults = {
namespace: '.c-pager',
step: 10,
current: 1, //現在のページ
total: 1, //総ページ数
elements: {
prev: '.c-pager__prev',
next: '.c-pager__next',
current: '.c-pager__current',
total: '.c-pager__total'
},
onPaging: null
};
// Methods (Private)
function _isObject(value) {
return $.type(value) === 'object';
}
function _isNumeric(value) {
return $.type(value) === 'number';
}
function _isString(value) {
return $.type(value) === 'string' && $.trim(value).length > 0;
}
function _sanitizeOption(options, defaults, property, predicate) {
if (!predicate(options[property])) {
options[property] = defaults[property];
}
}
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment