-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.uploaderList.js
115 lines (74 loc) · 3.17 KB
/
jquery.uploaderList.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
uploaderList 1.0 - jQuery plugin
written by O! Interactive, Acuna
http://ointeractive.ru
Copyright (c) 2016 O! Interactive, Acuna (http://ointeractive.ru)
Dual licensed under the MIT (MIT-LICENSE.txt)
and GPL (GPL-LICENSE.txt) licenses.
Built for jQuery library
http://jquery.com
Dependencies: jquery.ui.touch, jquery.ui.sortable
1.0 06.11.2016
Первый приватный релиз
*/
(function ($) {
$.fn.uploaderList = function (options) {
options = $.extend ({
'elem': 'uploader-list',
'content': {},
'onClick': function () {},
'onDelete': function () {},
'startFrom': 1,
'sortableOnStart': function () {},
'sortableOnStop': function () {},
'sortableOnUpdate': function () {},
'sortableOnTouch': function () {},
}, options);
var
self = this,
elem = $(self).find ('.' + options['elem']),
image, images = '';
if (!elem.length)
$(self).append ('<ul class="' + options['elem'] + '"></ul>');
elem = $(self).find ('.' + options['elem']);
if (options['class']) elem.addClass (options['class']);
$.each (options['content'], function (i, value) {
if (isUrl (value['image']))
image = '<a href="' + value['image'] + '" class="image-link"><img alt="" src="' + value['thumb'] + '" class="image"/></a><div class="delete-link"><a href="#"></a></div>'; else image = value['image'];
images += '<li data-id="' + (parseInt (i) + parseInt (options['startFrom'])) + '"' + (value['id'] ? ' data-item-id="' + value['id'] + '"' : '') + '>' + image + '</li>';
});
elem.html (images);
elem.find ('li').each (function (i) {
var el = this;
$(this).find ('.image-link').click (function () {
options['onClick'] (this, i);
});
$(this).find ('.delete-link').click (function () {
options['onDelete'] (this, i);
});
});
if (options['sortableOnStart'] || options['sortableOnStop'] || options['sortableOnUpdate'])
elem.sortable ({
'start': function (event, ui) {
var oldPos = $(ui.item).data ('id');
options['sortableOnStart'] (event, ui, oldPos);
},
'stop': function (event, ui) {
var oldPos = $(ui.item).data ('id');
var newPos = (ui.item.index () + parseInt (options['startFrom']));
if (newPos == oldPos)
options['sortableOnTouch'] (event, ui, newPos);
else
options['sortableOnStop'] (event, ui, newPos, oldPos);
},
'update': function (event, ui) {
var oldPos = $(ui.item).data ('id');
var newPos = ui.item.index ();
if (newPos != oldPos)
options['sortableOnUpdate'] (event, ui, newPos, oldPos);
},
});
if ($.isFunction (options['onOpen']))
options['onOpen'] (self, options);
};
})($);