forked from ExactTarget/fuelux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpillbox.js
72 lines (51 loc) · 1.5 KB
/
pillbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Fuel UX Pillbox
* https://github.com/ExactTarget/fuelux
*
* Copyright (c) 2012 ExactTarget
* Licensed under the MIT license.
*/
define(['require','jquery'],function(require) {
var $ = require('jquery');
// PILLBOX CONSTRUCTOR AND PROTOTYPE
var Pillbox = function (element, options) {
this.$element = $(element);
this.options = $.extend({}, $.fn.pillbox.defaults, options);
this.$element.on('click', 'li', $.proxy(this.itemclicked, this));
};
Pillbox.prototype = {
constructor: Pillbox,
items: function() {
return this.$element.find('li').map(function() {
var $this = $(this);
return $.extend({ text: $this.text() }, $this.data());
}).get();
},
itemclicked: function (e) {
$(e.currentTarget).remove();
e.preventDefault();
}
};
// PILLBOX PLUGIN DEFINITION
$.fn.pillbox = function (option) {
var methodReturn;
var $set = this.each(function () {
var $this = $(this);
var data = $this.data('pillbox');
var options = typeof option === 'object' && option;
if (!data) $this.data('pillbox', (data = new Pillbox(this, options)));
if (typeof option === 'string') methodReturn = data[option]();
});
return (methodReturn === undefined) ? $set : methodReturn;
};
$.fn.pillbox.defaults = {};
$.fn.pillbox.Constructor = Pillbox;
// PILLBOX DATA-API
$(function () {
$('body').on('mousedown.pillbox.data-api', '.pillbox', function (e) {
var $this = $(this);
if ($this.data('pillbox')) return;
$this.pillbox($this.data());
});
});
});