Comparing version 0.1.2 to 0.1.3
# Changelog | ||
### 0.1.2 (Jan 3, 2015) | ||
## 0.1.3 (Jun 13, 2016) | ||
- Added `$.fn.completer.noConflict()`. | ||
- Fixed a bug of the `destroy` method. | ||
- Improve code style. | ||
## 0.1.2 (Jan 3, 2015) | ||
- Add "destroy" method. | ||
@@ -9,3 +17,3 @@ - Improve suggestion mode. | ||
### 0.1.1 (Dec 7, 2014) | ||
## 0.1.1 (Dec 7, 2014) | ||
@@ -17,2 +25,2 @@ - Fix the bug of removing event listeners. | ||
### 0.1.0 (Aug 8, 2014) | ||
## 0.1.0 (Aug 9, 2014) |
/*! | ||
* Completer v0.1.2 | ||
* Completer v0.1.3 | ||
* https://github.com/fengyuanchen/completer | ||
* | ||
* Copyright 2014-2015 Fengyuan Chen | ||
* Copyright (c) 2014-2016 Fengyuan Chen | ||
* Released under the MIT license | ||
* | ||
* Date: 2015-01-03T12:17:43.862Z | ||
* Date: 2016-06-13T12:43:37.946Z | ||
*/ | ||
@@ -26,36 +26,40 @@ | ||
var $window = $(window), | ||
$document = $(document), | ||
Completer = function (element, options) { | ||
this.$element = $(element); | ||
this.defaults = $.extend({}, Completer.defaults, this.$element.data(), $.isPlainObject(options) ? options : {}); | ||
this.init(); | ||
}, | ||
var $window = $(window); | ||
var $document = $(document); | ||
var NAMESPACE = 'completer'; | ||
var EVENT_RESIZE = 'resize'; | ||
var EVENT_MOUSE_DOWN = 'mousedown'; | ||
toRegexp = function (s) { | ||
if (typeof s === 'string' && s !== '') { | ||
s = espace(s); | ||
function Completer(element, options) { | ||
this.$element = $(element); | ||
this.options = $.extend({}, Completer.DEFAULTS, $.isPlainObject(options) && options); | ||
this.init(); | ||
} | ||
return new RegExp(s + '+[^' + s + ']*$', 'i'); | ||
} | ||
function espace(s) { | ||
return s.replace(/([\.\$\^\{\[\(\|\)\*\+\?\\])/g, '\\$1'); | ||
} | ||
return null; | ||
}, | ||
function toRegexp (s) { | ||
if (typeof s === 'string' && s !== '') { | ||
s = espace(s); | ||
espace = function (s) { | ||
return s.replace(/([\.\$\^\{\[\(\|\)\*\+\?\\])/g, '\\$1'); | ||
}, | ||
return new RegExp(s + '+[^' + s + ']*$', 'i'); | ||
} | ||
toArray = function (s) { | ||
if (typeof s === 'string') { | ||
s = s.replace(/[\{\}\[\]"']+/g, '').split(/\s*,+\s*/); | ||
} | ||
return null; | ||
} | ||
s = $.map(s, function (n) { | ||
return typeof n !== 'string' ? n.toString() : n; | ||
}); | ||
function toArray(s) { | ||
if (typeof s === 'string') { | ||
s = s.replace(/[\{\}\[\]"']+/g, '').split(/\s*,+\s*/); | ||
} | ||
return s; | ||
}; | ||
s = $.map(s, function (n) { | ||
return typeof n !== 'string' ? n.toString() : n; | ||
}); | ||
return s; | ||
} | ||
Completer.prototype = { | ||
@@ -65,9 +69,9 @@ constructor: Completer, | ||
init: function () { | ||
var defaults = this.defaults, | ||
data = toArray(defaults.source); | ||
var options = this.options, | ||
data = toArray(options.source); | ||
if (data.length > 0) { | ||
this.data = data; | ||
this.regexp = toRegexp(defaults.separator); | ||
this.$completer = $(defaults.template); | ||
this.regexp = toRegexp(options.separator); | ||
this.$completer = $(options.template); | ||
this.$completer.hide().appendTo('body'); | ||
@@ -116,10 +120,11 @@ this.place(); | ||
attach: function (val) { | ||
var separator = this.defaults.separator, | ||
regexp = this.regexp, | ||
part = regexp ? val.match(regexp) : null, | ||
matched = [], | ||
all = [], | ||
that = this, | ||
reg, | ||
item; | ||
var options = this.options; | ||
var separator = options.separator; | ||
var regexp = this.regexp; | ||
var part = regexp ? val.match(regexp) : null; | ||
var matched = []; | ||
var all = []; | ||
var that = this; | ||
var reg; | ||
var item; | ||
@@ -145,3 +150,3 @@ if (part) { | ||
if (this.defaults.position === 'top') { | ||
if (options.position === 'top') { | ||
matched = matched.reverse(); | ||
@@ -154,5 +159,5 @@ } | ||
suggest: function (val) { | ||
var reg = new RegExp(espace(val), 'i'), | ||
that = this, | ||
matched = []; | ||
var reg = new RegExp(espace(val), 'i'); | ||
var that = this; | ||
var matched = []; | ||
@@ -177,3 +182,3 @@ $.each(this.data, function (i, n) { | ||
template: function (text) { | ||
var tag = this.defaults.itemTag; | ||
var tag = this.options.itemTag; | ||
@@ -189,5 +194,5 @@ return ('<' + tag + '>' + text + '</' + tag + '>'); | ||
if (html) { | ||
filter = this.defaults.position === 'top' ? ':last' : ':first'; | ||
filter = this.options.position === 'top' ? ':last' : ':first'; | ||
this.$completer.html(html); | ||
this.$completer.children(filter).addClass(this.defaults.selectedClass); | ||
this.$completer.children(filter).addClass(this.options.selectedClass); | ||
this.show(); | ||
@@ -200,4 +205,4 @@ } else { | ||
complete: function () { | ||
var defaults = this.defaults, | ||
val = defaults.filter(this.$element.val()).toString(); | ||
var options = this.options; | ||
var val = options.filter(this.$element.val()).toString(); | ||
@@ -209,3 +214,3 @@ if (val === '') { | ||
if (defaults.suggest) { | ||
if (options.suggest) { | ||
this.suggest(val); | ||
@@ -218,3 +223,5 @@ } else { | ||
keydown: function (e) { | ||
if (e.keyCode === 13) { | ||
var keyCode = e.keyCode || e.which || e.charCode; | ||
if (keyCode === 13) { | ||
e.stopPropagation(); | ||
@@ -226,3 +233,3 @@ e.preventDefault(); | ||
keyup: function (e) { | ||
var keyCode = e.keyCode; | ||
var keyCode = e.keyCode || e.which || e.charCode; | ||
@@ -237,7 +244,7 @@ if (keyCode === 13 || keyCode === 38 || keyCode === 40) { | ||
mouseover: function (e) { | ||
var defaults = this.defaults, | ||
selectedClass = defaults.selectedClass, | ||
var options = this.options; | ||
var selectedClass = options.selectedClass, | ||
$target = $(e.target); | ||
if ($target.is(defaults.itemTag)) { | ||
if ($target.is(options.itemTag)) { | ||
$target.addClass(selectedClass).siblings().removeClass(selectedClass); | ||
@@ -255,3 +262,3 @@ } | ||
this.$element.val(val); | ||
this.defaults.complete(); | ||
this.options.complete(); | ||
this.hide(); | ||
@@ -261,4 +268,4 @@ }, | ||
toggle: function (keyCode) { | ||
var selectedClass = this.defaults.selectedClass, | ||
$selected = this.$completer.find('.' + selectedClass); | ||
var selectedClass = this.options.selectedClass; | ||
var $selected = this.$completer.find('.' + selectedClass); | ||
@@ -295,14 +302,14 @@ switch (keyCode) { | ||
place: function () { | ||
var $element = this.$element, | ||
offset = $element.offset(), | ||
left = offset.left, | ||
top = offset.top, | ||
height = $element.outerHeight(), | ||
width = $element.outerWidth(), | ||
styles = { | ||
minWidth: width, | ||
zIndex: this.defaults.zIndex | ||
}; | ||
var $element = this.$element; | ||
var offset = $element.offset(); | ||
var left = offset.left; | ||
var top = offset.top; | ||
var height = $element.outerHeight(); | ||
var width = $element.outerWidth(); | ||
var styles = { | ||
minWidth: width, | ||
zIndex: this.options.zIndex | ||
}; | ||
switch (this.defaults.position) { | ||
switch (this.options.position) { | ||
case 'right': | ||
@@ -334,4 +341,4 @@ styles.left = left + width; | ||
this.$completer.show(); | ||
$window.on('resize', $.proxy(this.place, this)); | ||
$document.on('mousedown', $.proxy(this.hide, this)); | ||
$window.on(EVENT_RESIZE, $.proxy(this.place, this)); | ||
$document.on(EVENT_MOUSE_DOWN, $.proxy(this.hide, this)); | ||
}, | ||
@@ -341,18 +348,22 @@ | ||
this.$completer.hide(); | ||
$window.off('resize', this.place); | ||
$document.off('mousedown', this.hide); | ||
$window.off(EVENT_RESIZE, this.place); | ||
$document.off(EVENT_MOUSE_DOWN, this.hide); | ||
}, | ||
destroy: function () { | ||
var $this = this.$element; | ||
this.hide(); | ||
this.disable(); | ||
this.$element.off({ | ||
$this.off({ | ||
focus: this.enable, | ||
blur: this.disable | ||
}); | ||
$this.removeData(NAMESPACE); | ||
} | ||
}; | ||
Completer.defaults = { | ||
Completer.DEFAULTS = { | ||
itemTag: 'li', | ||
@@ -366,5 +377,3 @@ position: 'bottom', // or 'right' | ||
zIndex: 1, | ||
complete: $.noop, | ||
filter: function (val) { | ||
@@ -376,20 +385,29 @@ return val; | ||
Completer.setDefaults = function (options) { | ||
$.extend(Completer.defaults, options); | ||
$.extend(Completer.DEFAULTS, options); | ||
}; | ||
// Save the other completer | ||
Completer.other = $.fn.completer; | ||
// Register as jQuery plugin | ||
$.fn.completer = function (options) { | ||
var args = [].slice.call(arguments, 1), | ||
result; | ||
$.fn.completer = function (option) { | ||
var args = [].slice.call(arguments, 1); | ||
var result; | ||
this.each(function () { | ||
var $this = $(this), | ||
data = $this.data('completer'), | ||
fn; | ||
var $this = $(this); | ||
var data = $this.data(NAMESPACE); | ||
var options; | ||
var fn; | ||
if (!data) { | ||
$this.data('completer', (data = new Completer(this, options))); | ||
if (/destroy/.test(option)) { | ||
return; | ||
} | ||
options = $.extend({}, $this.data(), $.isPlainObject(option) && option); | ||
$this.data(NAMESPACE, (data = new Completer(this, options))); | ||
} | ||
if (typeof options === 'string' && $.isFunction((fn = data[options]))) { | ||
if (typeof option === 'string' && $.isFunction(fn = data[option])) { | ||
result = fn.apply(data, args); | ||
@@ -399,3 +417,3 @@ } | ||
return (typeof result !== 'undefined' ? result : this); | ||
return typeof result !== 'undefined' ? result : this; | ||
}; | ||
@@ -406,6 +424,11 @@ | ||
// No conflict | ||
$.fn.completer.noConflict = function () { | ||
$.fn.completer = Completer.other; | ||
return this; | ||
}; | ||
$(function () { | ||
$('[data-toggle="completer"],[completer]').completer(); | ||
$('[data-toggle="completer"]').completer(); | ||
}); | ||
}); |
/*! | ||
* Completer v0.1.2 | ||
* Completer v0.1.3 | ||
* https://github.com/fengyuanchen/completer | ||
* | ||
* Copyright 2014-2015 Fengyuan Chen | ||
* Copyright (c) 2014-2016 Fengyuan Chen | ||
* Released under the MIT license | ||
* | ||
* Date: 2015-01-03T12:17:43.862Z | ||
* Date: 2016-06-13T12:43:37.946Z | ||
*/ | ||
!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){"use strict";var b=a(window),c=a(document),d=function(b,c){this.$element=a(b),this.defaults=a.extend({},d.defaults,this.$element.data(),a.isPlainObject(c)?c:{}),this.init()},e=function(a){return"string"==typeof a&&""!==a?(a=f(a),new RegExp(a+"+[^"+a+"]*$","i")):null},f=function(a){return a.replace(/([\.\$\^\{\[\(\|\)\*\+\?\\])/g,"\\$1")},g=function(b){return"string"==typeof b&&(b=b.replace(/[\{\}\[\]"']+/g,"").split(/\s*,+\s*/)),b=a.map(b,function(a){return"string"!=typeof a?a.toString():a})};d.prototype={constructor:d,init:function(){var b=this.defaults,c=g(b.source);c.length>0&&(this.data=c,this.regexp=e(b.separator),this.$completer=a(b.template),this.$completer.hide().appendTo("body"),this.place(),this.$element.attr("autocomplete","off").on({focus:a.proxy(this.enable,this),blur:a.proxy(this.disable,this)}),this.$element.is(":focus")&&this.enable())},enable:function(){this.active||(this.active=!0,this.$element.on({keydown:a.proxy(this.keydown,this),keyup:a.proxy(this.keyup,this)}),this.$completer.on({mousedown:a.proxy(this.mousedown,this),mouseover:a.proxy(this.mouseover,this)}))},disable:function(){this.active&&(this.active=!1,this.$element.off({keydown:this.keydown,keyup:this.keyup}),this.$completer.off({mousedown:this.mousedown,mouseover:this.mouseover}))},attach:function(b){var c,d,e=this.defaults.separator,g=this.regexp,h=g?b.match(g):null,i=[],j=[],k=this;h&&(h=h[0],b=b.replace(g,""),c=new RegExp("^"+f(h),"i")),a.each(this.data,function(a,f){f=e+f,d=k.template(b+f),c&&c.test(f)?i.push(d):j.push(d)}),i=i.length?i.sort():j,"top"===this.defaults.position&&(i=i.reverse()),this.fill(i.join(""))},suggest:function(b){var c=new RegExp(f(b),"i"),d=this,e=[];a.each(this.data,function(a,b){c.test(b)&&e.push(b)}),e.sort(function(a,c){return a.indexOf(b)-c.indexOf(b)}),a.each(e,function(a,b){e[a]=d.template(b)}),this.fill(e.join(""))},template:function(a){var b=this.defaults.itemTag;return"<"+b+">"+a+"</"+b+">"},fill:function(a){var b;this.$completer.empty(),a?(b="top"===this.defaults.position?":last":":first",this.$completer.html(a),this.$completer.children(b).addClass(this.defaults.selectedClass),this.show()):this.hide()},complete:function(){var a=this.defaults,b=a.filter(this.$element.val()).toString();return""===b?void this.hide():void(a.suggest?this.suggest(b):this.attach(b))},keydown:function(a){13===a.keyCode&&(a.stopPropagation(),a.preventDefault())},keyup:function(a){var b=a.keyCode;13===b||38===b||40===b?this.toggle(b):this.complete()},mouseover:function(b){var c=this.defaults,d=c.selectedClass,e=a(b.target);e.is(c.itemTag)&&e.addClass(d).siblings().removeClass(d)},mousedown:function(b){b.stopPropagation(),b.preventDefault(),this.setValue(a(b.target).text())},setValue:function(a){this.$element.val(a),this.defaults.complete(),this.hide()},toggle:function(a){var b=this.defaults.selectedClass,c=this.$completer.find("."+b);switch(a){case 40:c.removeClass(b),c=c.next();break;case 38:c.removeClass(b),c=c.prev();break;case 13:this.setValue(c.text())}0===c.length&&(c=this.$completer.children(40===a?":first":":last")),c.addClass(b)},place:function(){var a=this.$element,c=a.offset(),d=c.left,e=c.top,f=a.outerHeight(),g=a.outerWidth(),h={minWidth:g,zIndex:this.defaults.zIndex};switch(this.defaults.position){case"right":h.left=d+g,h.top=e;break;case"left":h.right=b.innerWidth()-d,h.top=e;break;case"top":h.left=d,h.bottom=b.innerHeight()-e;break;default:h.left=d,h.top=e+f}this.$completer.css(h)},show:function(){this.$completer.show(),b.on("resize",a.proxy(this.place,this)),c.on("mousedown",a.proxy(this.hide,this))},hide:function(){this.$completer.hide(),b.off("resize",this.place),c.off("mousedown",this.hide)},destroy:function(){this.hide(),this.disable(),this.$element.off({focus:this.enable,blur:this.disable})}},d.defaults={itemTag:"li",position:"bottom",source:[],selectedClass:"completer-selected",separator:"",suggest:!1,template:'<ul class="completer-container"></ul>',zIndex:1,complete:a.noop,filter:function(a){return a}},d.setDefaults=function(b){a.extend(d.defaults,b)},a.fn.completer=function(b){var c,e=[].slice.call(arguments,1);return this.each(function(){var f,g=a(this),h=g.data("completer");h||g.data("completer",h=new d(this,b)),"string"==typeof b&&a.isFunction(f=h[b])&&(c=f.apply(h,e))}),"undefined"!=typeof c?c:this},a.fn.completer.Constructor=d,a.fn.completer.setDefaults=d.setDefaults,a(function(){a('[data-toggle="completer"],[completer]').completer()})}); | ||
!function(a){"function"==typeof define&&define.amd? | ||
// AMD. Register as anonymous module. | ||
define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){"use strict";function b(c,d){this.$element=a(c),this.options=a.extend({},b.DEFAULTS,a.isPlainObject(d)&&d),this.init()}function c(a){return a.replace(/([\.\$\^\{\[\(\|\)\*\+\?\\])/g,"\\$1")}function d(a){return"string"==typeof a&&""!==a?(a=c(a),new RegExp(a+"+[^"+a+"]*$","i")):null}function e(b){return"string"==typeof b&&(b=b.replace(/[\{\}\[\]"']+/g,"").split(/\s*,+\s*/)),b=a.map(b,function(a){return"string"!=typeof a?a.toString():a})}var f=a(window),g=a(document),h="completer",i="resize",j="mousedown";b.prototype={constructor:b,init:function(){var b=this.options,c=e(b.source);c.length>0&&(this.data=c,this.regexp=d(b.separator),this.$completer=a(b.template),this.$completer.hide().appendTo("body"),this.place(),this.$element.attr("autocomplete","off").on({focus:a.proxy(this.enable,this),blur:a.proxy(this.disable,this)}),this.$element.is(":focus")&&this.enable())},enable:function(){this.active||(this.active=!0,this.$element.on({keydown:a.proxy(this.keydown,this),keyup:a.proxy(this.keyup,this)}),this.$completer.on({mousedown:a.proxy(this.mousedown,this),mouseover:a.proxy(this.mouseover,this)}))},disable:function(){this.active&&(this.active=!1,this.$element.off({keydown:this.keydown,keyup:this.keyup}),this.$completer.off({mousedown:this.mousedown,mouseover:this.mouseover}))},attach:function(b){var d,e,f=this.options,g=f.separator,h=this.regexp,i=h?b.match(h):null,j=[],k=[],l=this;i&&(i=i[0],b=b.replace(h,""),d=new RegExp("^"+c(i),"i")),a.each(this.data,function(a,c){c=g+c,e=l.template(b+c),d&&d.test(c)?j.push(e):k.push(e)}),j=j.length?j.sort():k,"top"===f.position&&(j=j.reverse()),this.fill(j.join(""))},suggest:function(b){var d=new RegExp(c(b),"i"),e=this,f=[];a.each(this.data,function(a,b){d.test(b)&&f.push(b)}),f.sort(function(a,c){return a.indexOf(b)-c.indexOf(b)}),a.each(f,function(a,b){f[a]=e.template(b)}),this.fill(f.join(""))},template:function(a){var b=this.options.itemTag;return"<"+b+">"+a+"</"+b+">"},fill:function(a){var b;this.$completer.empty(),a?(b="top"===this.options.position?":last":":first",this.$completer.html(a),this.$completer.children(b).addClass(this.options.selectedClass),this.show()):this.hide()},complete:function(){var a=this.options,b=a.filter(this.$element.val()).toString();return""===b?void this.hide():void(a.suggest?this.suggest(b):this.attach(b))},keydown:function(a){var b=a.keyCode||a.which||a.charCode;13===b&&(a.stopPropagation(),a.preventDefault())},keyup:function(a){var b=a.keyCode||a.which||a.charCode;13===b||38===b||40===b?this.toggle(b):this.complete()},mouseover:function(b){var c=this.options,d=c.selectedClass,e=a(b.target);e.is(c.itemTag)&&e.addClass(d).siblings().removeClass(d)},mousedown:function(b){b.stopPropagation(),b.preventDefault(),this.setValue(a(b.target).text())},setValue:function(a){this.$element.val(a),this.options.complete(),this.hide()},toggle:function(a){var b=this.options.selectedClass,c=this.$completer.find("."+b);switch(a){ | ||
// Down | ||
case 40:c.removeClass(b),c=c.next();break; | ||
// Up | ||
case 38:c.removeClass(b),c=c.prev();break; | ||
// Enter | ||
case 13:this.setValue(c.text())}0===c.length&&(c=this.$completer.children(40===a?":first":":last")),c.addClass(b)},place:function(){var a=this.$element,b=a.offset(),c=b.left,d=b.top,e=a.outerHeight(),g=a.outerWidth(),h={minWidth:g,zIndex:this.options.zIndex};switch(this.options.position){case"right":h.left=c+g,h.top=d;break;case"left":h.right=f.innerWidth()-c,h.top=d;break;case"top":h.left=c,h.bottom=f.innerHeight()-d;break; | ||
// case 'bottom': | ||
default:h.left=c,h.top=d+e}this.$completer.css(h)},show:function(){this.$completer.show(),f.on(i,a.proxy(this.place,this)),g.on(j,a.proxy(this.hide,this))},hide:function(){this.$completer.hide(),f.off(i,this.place),g.off(j,this.hide)},destroy:function(){var a=this.$element;this.hide(),this.disable(),a.off({focus:this.enable,blur:this.disable}),a.removeData(h)}},b.DEFAULTS={itemTag:"li",position:"bottom",// or 'right' | ||
source:[],selectedClass:"completer-selected",separator:"",suggest:!1,template:'<ul class="completer-container"></ul>',zIndex:1,complete:a.noop,filter:function(a){return a}},b.setDefaults=function(c){a.extend(b.DEFAULTS,c)}, | ||
// Save the other completer | ||
b.other=a.fn.completer, | ||
// Register as jQuery plugin | ||
a.fn.completer=function(c){var d,e=[].slice.call(arguments,1);return this.each(function(){var f,g,i=a(this),j=i.data(h);if(!j){if(/destroy/.test(c))return;f=a.extend({},i.data(),a.isPlainObject(c)&&c),i.data(h,j=new b(this,f))}"string"==typeof c&&a.isFunction(g=j[c])&&(d=g.apply(j,e))}),"undefined"!=typeof d?d:this},a.fn.completer.Constructor=b,a.fn.completer.setDefaults=b.setDefaults, | ||
// No conflict | ||
a.fn.completer.noConflict=function(){return a.fn.completer=b.other,this},a(function(){a('[data-toggle="completer"]').completer()})}); |
{ | ||
"name": "completer", | ||
"description": "A jQuery auto complete plugin.", | ||
"version": "0.1.2", | ||
"description": "A simple jQuery auto complete plugin.", | ||
"version": "0.1.3", | ||
"license": "MIT", | ||
"repository": "fengyuanchen/completer", | ||
"homepage": "https://fengyuanchen.github.io/completer", | ||
"author": { | ||
"name": "Fengyuan Chen", | ||
"url": "http://chenfengyuan.com" | ||
}, | ||
"keywords": [ | ||
@@ -12,3 +19,3 @@ "auto", | ||
"css", | ||
"javacript", | ||
"javascript", | ||
"front-end", | ||
@@ -18,38 +25,21 @@ "web", | ||
], | ||
"main": "dist/completer.js", | ||
"author": { | ||
"name": "Fengyuan Chen", | ||
"url": "https://github.com/fengyuanchen" | ||
}, | ||
"homepage": "https://github.com/fengyuanchen/completer", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/fengyuanchen/completer.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/fengyuanchen/completer/issues" | ||
}, | ||
"license": { | ||
"type": "MIT", | ||
"url": "https://github.com/fengyuanchen/completer/blob/master/LICENSE.md" | ||
}, | ||
"dependencies": { | ||
"jquery": ">= 1.9.0" | ||
"jquery": ">= 1.9.1" | ||
}, | ||
"devDependencies": { | ||
"grunt": "~0.4.5", | ||
"grunt-contrib-clean": "~0.6.0", | ||
"grunt-contrib-copy": "~0.7.0", | ||
"grunt-contrib-watch": "~0.6.1", | ||
"grunt-contrib-jshint": "~0.10.0", | ||
"grunt-contrib-uglify": "~0.6.0", | ||
"grunt-contrib-csslint": "~0.3.1", | ||
"grunt-contrib-cssmin": "~0.10.0", | ||
"grunt-contrib-htmlmin": "~0.3.0", | ||
"grunt-autoprefixer": "~2.0.0", | ||
"grunt-replace": "~0.8.0", | ||
"grunt-csscomb": "~3.0.0", | ||
"grunt-jscs": "~1.0.0", | ||
"load-grunt-tasks": "~1.0.0" | ||
"grunt": "~1.0.1", | ||
"grunt-contrib-clean": "~1.0.0", | ||
"grunt-contrib-copy": "~1.0.0", | ||
"grunt-contrib-watch": "~1.0.0", | ||
"grunt-contrib-jshint": "~1.0.0", | ||
"grunt-contrib-uglify": "~1.0.1", | ||
"grunt-contrib-csslint": "~1.0.0", | ||
"grunt-contrib-cssmin": "~1.0.1", | ||
"grunt-contrib-htmlmin": "~1.4.0", | ||
"grunt-autoprefixer": "~3.0.4", | ||
"grunt-replace": "~1.0.1", | ||
"grunt-csscomb": "~3.1.0", | ||
"grunt-jscs": "~3.0.0", | ||
"load-grunt-tasks": "~3.5.0" | ||
} | ||
} |
@@ -5,3 +5,3 @@ /*! | ||
* | ||
* Copyright 2014-@YEAR Fengyuan Chen | ||
* Copyright (c) 2014-@YEAR Fengyuan Chen | ||
* Released under the MIT license | ||
@@ -27,36 +27,40 @@ * | ||
var $window = $(window), | ||
$document = $(document), | ||
Completer = function (element, options) { | ||
this.$element = $(element); | ||
this.defaults = $.extend({}, Completer.defaults, this.$element.data(), $.isPlainObject(options) ? options : {}); | ||
this.init(); | ||
}, | ||
var $window = $(window); | ||
var $document = $(document); | ||
var NAMESPACE = 'completer'; | ||
var EVENT_RESIZE = 'resize'; | ||
var EVENT_MOUSE_DOWN = 'mousedown'; | ||
toRegexp = function (s) { | ||
if (typeof s === 'string' && s !== '') { | ||
s = espace(s); | ||
function Completer(element, options) { | ||
this.$element = $(element); | ||
this.options = $.extend({}, Completer.DEFAULTS, $.isPlainObject(options) && options); | ||
this.init(); | ||
} | ||
return new RegExp(s + '+[^' + s + ']*$', 'i'); | ||
} | ||
function espace(s) { | ||
return s.replace(/([\.\$\^\{\[\(\|\)\*\+\?\\])/g, '\\$1'); | ||
} | ||
return null; | ||
}, | ||
function toRegexp (s) { | ||
if (typeof s === 'string' && s !== '') { | ||
s = espace(s); | ||
espace = function (s) { | ||
return s.replace(/([\.\$\^\{\[\(\|\)\*\+\?\\])/g, '\\$1'); | ||
}, | ||
return new RegExp(s + '+[^' + s + ']*$', 'i'); | ||
} | ||
toArray = function (s) { | ||
if (typeof s === 'string') { | ||
s = s.replace(/[\{\}\[\]"']+/g, '').split(/\s*,+\s*/); | ||
} | ||
return null; | ||
} | ||
s = $.map(s, function (n) { | ||
return typeof n !== 'string' ? n.toString() : n; | ||
}); | ||
function toArray(s) { | ||
if (typeof s === 'string') { | ||
s = s.replace(/[\{\}\[\]"']+/g, '').split(/\s*,+\s*/); | ||
} | ||
return s; | ||
}; | ||
s = $.map(s, function (n) { | ||
return typeof n !== 'string' ? n.toString() : n; | ||
}); | ||
return s; | ||
} | ||
Completer.prototype = { | ||
@@ -66,9 +70,9 @@ constructor: Completer, | ||
init: function () { | ||
var defaults = this.defaults, | ||
data = toArray(defaults.source); | ||
var options = this.options, | ||
data = toArray(options.source); | ||
if (data.length > 0) { | ||
this.data = data; | ||
this.regexp = toRegexp(defaults.separator); | ||
this.$completer = $(defaults.template); | ||
this.regexp = toRegexp(options.separator); | ||
this.$completer = $(options.template); | ||
this.$completer.hide().appendTo('body'); | ||
@@ -117,10 +121,11 @@ this.place(); | ||
attach: function (val) { | ||
var separator = this.defaults.separator, | ||
regexp = this.regexp, | ||
part = regexp ? val.match(regexp) : null, | ||
matched = [], | ||
all = [], | ||
that = this, | ||
reg, | ||
item; | ||
var options = this.options; | ||
var separator = options.separator; | ||
var regexp = this.regexp; | ||
var part = regexp ? val.match(regexp) : null; | ||
var matched = []; | ||
var all = []; | ||
var that = this; | ||
var reg; | ||
var item; | ||
@@ -146,3 +151,3 @@ if (part) { | ||
if (this.defaults.position === 'top') { | ||
if (options.position === 'top') { | ||
matched = matched.reverse(); | ||
@@ -155,5 +160,5 @@ } | ||
suggest: function (val) { | ||
var reg = new RegExp(espace(val), 'i'), | ||
that = this, | ||
matched = []; | ||
var reg = new RegExp(espace(val), 'i'); | ||
var that = this; | ||
var matched = []; | ||
@@ -178,3 +183,3 @@ $.each(this.data, function (i, n) { | ||
template: function (text) { | ||
var tag = this.defaults.itemTag; | ||
var tag = this.options.itemTag; | ||
@@ -190,5 +195,5 @@ return ('<' + tag + '>' + text + '</' + tag + '>'); | ||
if (html) { | ||
filter = this.defaults.position === 'top' ? ':last' : ':first'; | ||
filter = this.options.position === 'top' ? ':last' : ':first'; | ||
this.$completer.html(html); | ||
this.$completer.children(filter).addClass(this.defaults.selectedClass); | ||
this.$completer.children(filter).addClass(this.options.selectedClass); | ||
this.show(); | ||
@@ -201,4 +206,4 @@ } else { | ||
complete: function () { | ||
var defaults = this.defaults, | ||
val = defaults.filter(this.$element.val()).toString(); | ||
var options = this.options; | ||
var val = options.filter(this.$element.val()).toString(); | ||
@@ -210,3 +215,3 @@ if (val === '') { | ||
if (defaults.suggest) { | ||
if (options.suggest) { | ||
this.suggest(val); | ||
@@ -219,3 +224,5 @@ } else { | ||
keydown: function (e) { | ||
if (e.keyCode === 13) { | ||
var keyCode = e.keyCode || e.which || e.charCode; | ||
if (keyCode === 13) { | ||
e.stopPropagation(); | ||
@@ -227,3 +234,3 @@ e.preventDefault(); | ||
keyup: function (e) { | ||
var keyCode = e.keyCode; | ||
var keyCode = e.keyCode || e.which || e.charCode; | ||
@@ -238,7 +245,7 @@ if (keyCode === 13 || keyCode === 38 || keyCode === 40) { | ||
mouseover: function (e) { | ||
var defaults = this.defaults, | ||
selectedClass = defaults.selectedClass, | ||
var options = this.options; | ||
var selectedClass = options.selectedClass, | ||
$target = $(e.target); | ||
if ($target.is(defaults.itemTag)) { | ||
if ($target.is(options.itemTag)) { | ||
$target.addClass(selectedClass).siblings().removeClass(selectedClass); | ||
@@ -256,3 +263,3 @@ } | ||
this.$element.val(val); | ||
this.defaults.complete(); | ||
this.options.complete(); | ||
this.hide(); | ||
@@ -262,4 +269,4 @@ }, | ||
toggle: function (keyCode) { | ||
var selectedClass = this.defaults.selectedClass, | ||
$selected = this.$completer.find('.' + selectedClass); | ||
var selectedClass = this.options.selectedClass; | ||
var $selected = this.$completer.find('.' + selectedClass); | ||
@@ -296,14 +303,14 @@ switch (keyCode) { | ||
place: function () { | ||
var $element = this.$element, | ||
offset = $element.offset(), | ||
left = offset.left, | ||
top = offset.top, | ||
height = $element.outerHeight(), | ||
width = $element.outerWidth(), | ||
styles = { | ||
minWidth: width, | ||
zIndex: this.defaults.zIndex | ||
}; | ||
var $element = this.$element; | ||
var offset = $element.offset(); | ||
var left = offset.left; | ||
var top = offset.top; | ||
var height = $element.outerHeight(); | ||
var width = $element.outerWidth(); | ||
var styles = { | ||
minWidth: width, | ||
zIndex: this.options.zIndex | ||
}; | ||
switch (this.defaults.position) { | ||
switch (this.options.position) { | ||
case 'right': | ||
@@ -335,4 +342,4 @@ styles.left = left + width; | ||
this.$completer.show(); | ||
$window.on('resize', $.proxy(this.place, this)); | ||
$document.on('mousedown', $.proxy(this.hide, this)); | ||
$window.on(EVENT_RESIZE, $.proxy(this.place, this)); | ||
$document.on(EVENT_MOUSE_DOWN, $.proxy(this.hide, this)); | ||
}, | ||
@@ -342,18 +349,22 @@ | ||
this.$completer.hide(); | ||
$window.off('resize', this.place); | ||
$document.off('mousedown', this.hide); | ||
$window.off(EVENT_RESIZE, this.place); | ||
$document.off(EVENT_MOUSE_DOWN, this.hide); | ||
}, | ||
destroy: function () { | ||
var $this = this.$element; | ||
this.hide(); | ||
this.disable(); | ||
this.$element.off({ | ||
$this.off({ | ||
focus: this.enable, | ||
blur: this.disable | ||
}); | ||
$this.removeData(NAMESPACE); | ||
} | ||
}; | ||
Completer.defaults = { | ||
Completer.DEFAULTS = { | ||
itemTag: 'li', | ||
@@ -367,5 +378,3 @@ position: 'bottom', // or 'right' | ||
zIndex: 1, | ||
complete: $.noop, | ||
filter: function (val) { | ||
@@ -377,20 +386,29 @@ return val; | ||
Completer.setDefaults = function (options) { | ||
$.extend(Completer.defaults, options); | ||
$.extend(Completer.DEFAULTS, options); | ||
}; | ||
// Save the other completer | ||
Completer.other = $.fn.completer; | ||
// Register as jQuery plugin | ||
$.fn.completer = function (options) { | ||
var args = [].slice.call(arguments, 1), | ||
result; | ||
$.fn.completer = function (option) { | ||
var args = [].slice.call(arguments, 1); | ||
var result; | ||
this.each(function () { | ||
var $this = $(this), | ||
data = $this.data('completer'), | ||
fn; | ||
var $this = $(this); | ||
var data = $this.data(NAMESPACE); | ||
var options; | ||
var fn; | ||
if (!data) { | ||
$this.data('completer', (data = new Completer(this, options))); | ||
if (/destroy/.test(option)) { | ||
return; | ||
} | ||
options = $.extend({}, $this.data(), $.isPlainObject(option) && option); | ||
$this.data(NAMESPACE, (data = new Completer(this, options))); | ||
} | ||
if (typeof options === 'string' && $.isFunction((fn = data[options]))) { | ||
if (typeof option === 'string' && $.isFunction(fn = data[option])) { | ||
result = fn.apply(data, args); | ||
@@ -400,3 +418,3 @@ } | ||
return (typeof result !== 'undefined' ? result : this); | ||
return typeof result !== 'undefined' ? result : this; | ||
}; | ||
@@ -407,6 +425,11 @@ | ||
// No conflict | ||
$.fn.completer.noConflict = function () { | ||
$.fn.completer = Completer.other; | ||
return this; | ||
}; | ||
$(function () { | ||
$('[data-toggle="completer"],[completer]').completer(); | ||
$('[data-toggle="completer"]').completer(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
45452
13
1221
2
0
1
2
Updatedjquery@>= 1.9.1