-
Notifications
You must be signed in to change notification settings - Fork 1
/
taggy.js
319 lines (290 loc) · 11.7 KB
/
taggy.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Taggy 0.1.1
(function() {
var Taggy = function(params){
var selector = document.querySelectorAll(params);
this.length = selector.length;
this.version = '0.0.5';
for(var i = 0; i < this.length; i++){
this[i] = selector[i];
var divId = _taggy.generateId('taggy-container');
var div = document.createElement('div');
this[i].divId = divId;
div.setAttribute('id', divId);
div.setAttribute('class', 'taggy-container');
var styles = 'max-width: ' + this[i].naturalWidth + 'px';
styles += ';max-height:' + this[i].naturalHeight + 'px;';
div.setAttribute('style', styles);
this[i].parentNode.insertBefore(div, this[i].nextSibling);
this[i].setAttribute('class', 'taggy-img');
div.appendChild(this[i]);
}
return this;
};
var _taggy = function(params){
return new Taggy(params);
};
_taggy.counter = 0;
_taggy.generateId = function(prefix){
return prefix + '-' + this.counter++;
};
_taggy.fn = Taggy.prototype = {
getCoords: function(cb){
if(typeof cb !== 'function') return;
__cb = cb;
var len = this.length;
while (len--) {
this[len].addEventListener('click', __taggyListener);
}
return this;
},
removeListener: function(){
var len = this.length;
while (len--) {
this[len].removeEventListener('click', __taggyListener);
}
return this;
},
setCoords: function(data, cb){
if(!data) return this;
var isArray = Object.prototype.toString.call(data) === '[object Array]';
if(!isArray && typeof data !== 'object') return this;
if(!isArray) data = [data];
var len = this.length;
while(len--){
var div = document.getElementById(this[len].divId);
data.forEach(function(coords){
var span = document.createElement('span');
span.setAttribute('class', 'taggy-coord');
var styles = 'left: ' + coords.x + '%;top: ' + coords.y + '%;';
span.setAttribute('style', styles);
if(typeof coords.options === 'object'){
span = __parseOptions(span, coords, div, cb);
}
div.appendChild(span);
});
}
return this;
},
stopTagsListeners: function(){
__tagListeners = false;
return this;
},
restartTagsListeners: function(){
__tagListeners = true;
return this;
},
removeTag: function(tag){
var len = this.length;
while(len--){
var div = document.getElementById(this[len].divId);
div.removeChild(tag);
}
},
removeAll: function(){
var len = this.length;
while(len--){
var div = document.getElementById(this[len].divId);
var spans = div.getElementsByClassName('taggy-coord');
var length = spans.length;
while(spans[0]){
spans[0].parentNode.removeChild(spans[0]);
}
var modals = div.getElementsByClassName('taggy-modal');
length = modals.length;
while(modals[0]){
modals[0].parentNode.removeChild(modals[0]);
}
}
return this;
}
};
var __cb = function(){};
var __tagListeners = true;
var __taggyListener = function(event){
var coords = __getCoords.call(this, event);
return __cb(coords);
};
var __getCoords = function(event){
var absoluteX = event.offsetX - this.offsetLeft;
var absoluteY = event.offsetY - this.offsetTop;
var relativeX = (absoluteX * 100) / this.offsetWidth;
var relativeY = (absoluteY * 100) / this.offsetHeight;
return {x: relativeX, y: relativeY};
};
var __parseOptions = function(span, coords, div, cb){
var options = coords.options;
if(options.id) span.setAttribute('id', options.id);
if(options.class) span.className += ' ' + options.class;
var type = options.type;
if(type !== 'get-drop' && type !== 'set') type = 'get';
span.dataset.id = _taggy.generateId('tag');
if(type === 'set'){
if(typeof cb !== 'function') cb = function(){};
span.addEventListener('click', function(event){
if(!__tagListeners) return;
__tagListeners = false;
__resetCoordsTexts(div);
span.className += ' taggy-input iluminate';
if(typeof options.modal === 'object' || options.modal === true){
span.className += ' taggy-modal';
var acceptBtn, cancelBtn, hideCancelBtn;
var showTextarea = !!options.modal.textarea;
if(typeof options.modal === 'object'){
acceptBtn = options.modal.acceptBtn;
hideCancelBtn = options.modal.hideCancelBtn;
cancelBtn = options.modal.cancelBtn;
}
var acceptBtnHtml = '<a class="button accept"' + (hideCancelBtn ? 'style="width: 100%;"' : '') + '>';
var modal = [
'<div class="confirm-box"><div class="confirm-dialog"><div class="confirm-content">',
'<div class="confirm-title taggy-sm">', options.title || '', '</div>',
'<input type="text" class="taggy-input"',
options.text ? ' value="' + options.text + '"' : '', '>',
showTextarea ? '<textarea class="taggy-textarea">' + (options.description || '') + '</textarea>' : '',
'<div class="confirm-buttons">',
acceptBtnHtml, acceptBtn || 'Accept', '</a>',
hideCancelBtn ? '' : '<a class="button cancel">' + (cancelBtn || 'Cancel') + '</a>',
'</div>', '</div></div></div>', '<div class="confirm-modal"></div>'
].join('');
span.innerHTML = modal;
var input = span.querySelectorAll('input[type="text"].taggy-input')[0];
input.addEventListener('click', function(event){
event.stopPropagation();
});
var textarea = span.querySelectorAll('textarea.taggy-textarea')[0];
if(textarea){
textarea.addEventListener('click', function(event){
event.stopPropagation();
});
}
var acceptBtn = span.querySelectorAll('a.button.accept')[0];
acceptBtn.addEventListener('click', function(event){
event.stopPropagation();
__tagListeners = true;
var description = null;
if(textarea){
description = textarea.value;
cb(input.value || null, description, coords);
}else{
cb(input.value || null, coords);
}
__resetCoordsTexts(div);
});
if(hideCancelBtn) return;
var cancelBtn = span.querySelectorAll('a.button.cancel')[0];
cancelBtn.addEventListener('click', function(event){
span.classList.remove('iluminate');
__tagListeners = true;
event.stopPropagation();
__resetCoordsTexts(div);
});
}
});
}else if(type === 'get-drop'){
if(typeof cb !== 'function') cb = function(){};
span.addEventListener('click', function(event){
if(!__tagListeners) return;
__resetCoordsTexts(div);
span.className += ' taggy-input iluminate';
if(typeof options.modal === 'object' || options.modal === true){
var acceptBtn, cancelBtn, hideCancelBtn, list;
if(typeof options.modal === 'object'){
acceptBtn = options.modal.acceptBtn;
hideCancelBtn = options.modal.hideCancelBtn;
cancelBtn = options.modal.cancelBtn;
}
list = options.list || [];
var acceptBtnHtml = '<a class="button accept"' + (hideCancelBtn ? 'style="width: 100%;"' : '') + '>';
var modal = [
'<div class="confirm-box"><div class="confirm-dialog"><div class="confirm-content">',
'<div class="confirm-title taggy-sm">', options.text || '', '</div>',
'<div id="optionSelector"></div>',
'<div class="confirm-buttons">',
acceptBtnHtml, acceptBtn || 'Accept', '</a>',
hideCancelBtn ? '' : '<a class="button cancel">' + (cancelBtn || 'Cancel') + '</a>',
'</div>', '</div></div></div>', '<div class="confirm-modal"></div>'
].join('');
span.innerHTML = modal;
var ul = document.createElement('ul');
ul.setAttribute('id','optList');
var radioInput, optionSelected;
document.getElementById('optionSelector').appendChild(ul);
list.forEach(renderOptionsList);
function renderOptionsList(element, index, arr){
var li = document.createElement('li');
var opt = document.createElement('input');
opt.value = element;
li.setAttribute('class','item');
opt.setAttribute('type', 'radio');
opt.setAttribute('name', 'taggy-radio');
ul.appendChild(li);
li.append(opt);
radioInput = document.createTextNode(element);
li.innerHTML = li.innerHTML + element;
li.addEventListener('click', function(event){
event.stopPropagation();
optionSelected = event.currentTarget.getElementsByTagName('input')[0].value;
});
};
var acceptBtn = span.querySelectorAll('a.button.accept')[0];
acceptBtn.addEventListener('click', function(event){
span.classList.remove('iluminate');
event.stopPropagation();
cb(optionSelected, coords);
__resetCoordsTexts(div);
});
if(hideCancelBtn) return;
var cancelBtn = span.querySelectorAll('a.button.cancel')[0];
cancelBtn.addEventListener('click', function(event){
span.classList.remove('iluminate');
event.stopPropagation();
__resetCoordsTexts(div);
});
}
});
}else if(type === 'get'){
if(typeof options.text !== 'string') return span;
span.addEventListener('click', function(event){
if(!__tagListeners) return;
__tagListeners = false;
__resetCoordsTexts(div);
span.innerHTML = options.text;
span.className += ' text iluminate';
if(typeof options.modal === 'object' || options.modal === true){
span.className += ' modal-taggy';
var acceptBtn, showTextarea;
if(typeof options.modal === 'object') acceptBtn = options.modal.acceptBtn;
showTextarea = !!options.description;
var modal = [
'<div class="confirm-box"><div class="confirm-dialog"><div class="confirm-content">',
'<div class="taggy-get">',
'<div class="taggy-text">', options.text || '', '</div>',
'<div class="taggy-description">', options.description || '', '</div>',
'</div>',
'<div class="confirm-buttons">',
'<a class="button accept" style="width: 100%;">', acceptBtn || 'Accept', '</a>',
'</div>', '</div></div></div>', '<div class="confirm-modal"></div>'
].join('');
span.innerHTML = modal;
var acceptBtn = span.querySelectorAll('a.button.accept')[0];
acceptBtn.addEventListener('click', function(event){
__tagListeners = true;
span.classList.remove('iluminate');
event.stopPropagation();
__resetCoordsTexts(div);
});
}
});
}
return span;
};
var __resetCoordsTexts = function(div, removeModal){
var coordsSpans = div.querySelectorAll('span.taggy-coord');
for(var i = 0; i < coordsSpans.length; i++){
coordsSpans[i].innerHTML = '';
coordsSpans[i].className = coordsSpans[i].className.replace(/\btext\b/, '');
coordsSpans[i].className = coordsSpans[i].className.replace(/\bmodal\b/, '');
}
};
if(!window.Taggy) window.Taggy = _taggy;
})();