-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkao.js
583 lines (506 loc) · 14.7 KB
/
kao.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/**
* Kao v0.9
*
*
* Copyright (c) 2012 Cristobal Chao
* MIT License
**/
(function( window, $, undefined ){
if ( !$.cssHooks ) {
throw("jQuery 1.4.3+ is needed for this plugin to work");
return;
}
function styleSupport( prop ) {
var vendorProp, supportedProp,
capProp = prop.charAt(0).toUpperCase() + prop.slice(1),
prefixes = [ "Moz", "Webkit", "O", "ms" ],
newprefixes = [ "-moz-", "-webkit-", "-o-", "-ms-" ],
div = document.createElement( "div" );
if ( prop in div.style ) {
supportedProp = prop;
} else {
for ( var i = 0; i < prefixes.length; i++ ) {
vendorProp = prefixes[i] + capProp;
if ( vendorProp in div.style ) {
supportedProp = newprefixes[i]+prop;
break;
}
}
}
div = null;
$.support[ prop ] = supportedProp;
return supportedProp;
}
var kao = styleSupport( "transform" );
$.cssHooks.scale = {
get: function( elem, computed, extra ) {
return elem.style[ kao ];
},
set: function( elem, value) {
var _translate = (elem.style[ kao ].indexOf('translate(') > -1)?elem.style[ kao ].split('translate(')[1]:"";
(_translate)?_translate = 'translate('+_translate.split(')')[0]+')':null;
var _rotate = (elem.style[ kao ].indexOf('rotate(') > -1)?elem.style[ kao ].split('rotate(')[1]:"";
(_rotate)?_rotate = ' rotate('+_rotate.split(')')[0]+')':null;
elem.style[ kao ] = "scale("+value.split('px')[0]+")"+_rotate;
}
};
$.cssHooks.translate = {
get: function( elem, computed, extra ) {
return elem.style[ kao ];
},
set: function( elem, value) {
var _scale = (elem.style[ kao ].indexOf('scale(') > -1)?elem.style[ kao ].split('scale(')[1]:"";
var _rotate = (elem.style[ kao ].indexOf('rotate(') > -1)?elem.style[ kao ].split('rotate(')[1]:"";
(_scale)?_scale = 'scale('+_scale.split(')')[0]+')':null;
(_rotate)?_rotate = ' rotate('+_rotate.split(')')[0]+')':null;
if (typeof value[0] === 'string' ){
_valX = value[0].split('(')[1];
_valY = value[1].split(')')[0];
elem.style[ kao ] = "translate("+_valX+","+_valY+") "+_scale+_rotate;
} else {
var _x = value[0];
var _y = value[1];
elem.style[ kao ] = "translate("+_x+"px,"+_y+"px) "+_scale+_rotate;
}
}
};
var $numElemRow = "";
var $x_var;
var $y_var;
var $width;
var $ouWidth;
var $ouHeight;
var $activate = false;
//Allows to create new css rules
$.fn.cssRule = function (rules){
$this = this;
var context=document,stylesheet;
if(typeof context.styleSheets=='object'){
if(context.styleSheets.length){
stylesheet=context.styleSheets[context.styleSheets.length-1];
}
if(context.styleSheets.length){
if(context.createStyleSheet){
stylesheet=context.createStyleSheet();
}else{
context.getElementsByTagName('head')[0].appendChild(context.createElement('style'));
stylesheet=context.styleSheets[context.styleSheets.length-1];
}
}
if(stylesheet.addRule){
stylesheet.addRule($this.selector,rules);
}else{
stylesheet.insertRule($this.selector + '{' + rules + '}', stylesheet.cssRules.length);
}
}
}
function nextPos(pos){
var x = parseInt(pos[0]), y = parseInt(pos[1]);
(2*x < $kao_container.width)?x+=$x_var:y+=$y_var;
pos[0]=x;pos[1]=y;
return pos;
}
function setNumElemRow(num){
if (!$numElemRow){
$numElemRow = num;
}
}
function setWidth(num){
if (!$width){
$width = num;
}
}
function setoWidth(num){
if (!$ouWidth){
$ouWidth = num;
}
}
function setoHeight(num){
if (!$ouHeight){
$ouHeight = num;
}
}
function setXVar(num){
if (!$x_var){
$x_var = num;
}
}
function setYVar(num){
if (!$y_var){
$y_var = num;
}
}
$.fn.getAbsPos = function() {
var strPos = this.css('translate');
return strPos;
}
$.fn.getAbsPos_x = function() {
return parseInt(this.getAbsPos()[0]);
}
$.fn.getAbsPos_y = function() {
return parseInt(this.getAbsPos()[1]);
}
function getLastPos_y(){
var max = -1;
$kao_element.selector.each(function(){
var value = parseInt($(this).getAbsPos_y());
(max < value)?max=value:null;
});
return max;
}
$.fn.getPos_x = function() {
return this.attr('pos').split(',')[0];
}
$.fn.getPos_y = function() {
return this.attr('pos').split(',')[1];
}
function getPos(id_elem){
if (id_elem >= $kao_element.selector.length) {
return $kao_element.next_last_pos;
}else {
return $($kao_element.selector.selector+'[data-id="'+id_elem+'"]').attr('posIni').split(',');
}
}
$.fn.getSavedPos = function(pos) {
return this.attr('pos').split(',');
}
$.fn.setSavedPos = function(pos) {
this.attr('pos',pos);
}
$.fn.savePos = function() {
this.attr('pos',this.getAbsPos());
}
$.fn.savePosIni = function() {
this.attr('posIni',this.getAbsPos());
}
$.fn.setPos = function(pos) {
$this = this;
$(this).css({ translate: pos });
}
$.fn.restartToSavedPos = function() {
$this = this;
$this.setPos($this.getSavedPos());
}
$.fn.isExtended = function() {
return $kao_container.selector.hasClass('extended');
}
$.fn.getDoubleWidth = function() {
return $kao_element.max_width;
}
Array.prototype.findIndex = function(value){
var ctr = "";
for (var i=0; i < this.length; i++) {
if (this[i] == value) {
return i;
}
}
return ctr;
};
$.fn.pushing = function(){
var clonArr = $.extend(true, [], $kao_element.cells);
var psArr = clonArr.findIndex($this.attr('data-id'));
//If the element is on the right, we expand it to the left, and the other way around
if ($this.hasClass('rElem')){
var aux = clonArr[psArr-1];
clonArr[psArr-1] = 'E';
clonArr[psArr] = aux;
}else{
clonArr[psArr] = 'E';
}
clonArr.splice(psArr, 0, 'E');
//Positioning the elements
for (var i=0;i < clonArr.length;i++) {
var val = clonArr[i];
if(val != 'E') {
if (i+1 == clonArr.length) {
$($kao_element.selector.selector+'[data-id="'+val+'"]').css({ translate: $kao_element.last_pos });
} else {
$($kao_element.selector.selector+'[data-id="'+val+'"]').css({ translate: getPos(i) });
}
}
}
}
function add_effect(elem) {
//If there are elements to apply effects
if ($kao_element.subelems_effects) {
$($kao_element.subelems_effects,elem).css({opacity:0}).animate({opacity:1},$kao_element.time_effect);
}
}
function refreshPage(args){
if ($kao_element.filter){
var filterPage = window.location.href.split('#/filter/')[1];
filterPage = (!filterPage)?'all':filterPage;
$filter_elem = $($kao_element.filter+'[href="#/filter/'+filterPage+'"]');
$($kao_element.filter).removeClass('active');
$filter_elem.addClass('active');
$filter_elem.filterElements(args);
}
}
$.fn.filterElements = function(args){
$this = this;
var arrAux = new Array();
if ($this.attr('id') === '.*') {
$kao_element.selector.each(function(){
arrAux.push($(this).attr('data-id'));
});
}else{
$($this.attr('id')).each(function(){
arrAux.push($(this).attr('data-id'));
});
}
$kao_element.selector.each(function(){
if (arrAux.indexOf($(this).attr('data-id')) == -1){
$(this).css({scale:0.001,opacity:0});
}
});
$kao_element.cells = $.extend(true, [], arrAux);
$kao_element.selector.css({opacity:0});
//Positioning the elements
collapse();
for (var i=0;i < arrAux.length;i++) {
var val = arrAux[i];
//if is going to be rElem
if ((i+1)%(parseInt($numElemRow)) == 0) {
$($kao_element.selector.selector+'[data-id="'+val+'"]')
.css({ scale:1,opacity:1,translate: getPos(i), left:0})
.addClass('rElem')
.delay($kao_element.time_effect)
.queue(function() {
$(this).css({left:'auto'}).dequeue();
});
} else /*if not */{
$($kao_element.selector.selector+'[data-id="'+val+'"]')
.css({ scale:1,opacity:1,translate: getPos(i), left:0})
.delay($kao_element.time_effect)
.queue(function() {
$(this).removeClass('rElem').dequeue();
});
}
$($kao_element.selector.selector+'[data-id="'+val+'"]').setSavedPos(getPos(i));
}
$kao_element.last_pos = getPos(i);
}
//The element which calls the function will be extended
//scroll -> Boolean argument: Enable or disable the scrolling which points to the element which is being extended
$.fn.extend = function() {
//Always collapsing before extend
collapse(this);
$this = this;
//Adding the tag to the elements which are going to be expended
$this.tagElements();
//Increasing the width of the element
$this.width($this.getDoubleWidth());
//Pushing other elements
$this.pushing();
//Scrolling if is enabled
if ($kao_element.scroll) {
$('html,body').animate({scrollTop: parseInt($this.getPos_y())+parseInt($kao_container.selector.offset().top)}, $kao_element.time_effect);
}
}
function extendAll(elements) {
$kao_element.selector.width($this.getDoubleWidth());
$kao_element.selector.tagElements(elements);
}
$.fn.collapse = function(){
collapse(this);
}
function collapse(elem) {
add_effect(elem);
$kao_element.selector.each(function(){
$(this).restartToSavedPos();
$(this).unTagElements();
$(this).width($kao_element.width);
});
}
$.fn.unTagElements = function() {
$this = this;
if ($kao_element.subelems_to_expand){
$($kao_element.subelems_to_expand,$this).removeClass('large');
}
$this.removeClass('large');
}
$.fn.tagElements = function() {
$this = this;
if ($kao_element.subelems_to_expand){
$($kao_element.subelems_to_expand,$this).addClass('large');
}
$this.addClass('large');
}
function applyRight($elem){
/*$elem
.css({ left:0})
.addClass('rElem')
.stop()
.delay($kao_element.time_effect)
.queue(function(){
resizingContainer();
$(this).css({left:'auto'}).dequeue();
});*/
$elem
.css({ left:0})
.stop()
.addClass('rElem')
.delay($kao_element.time_effect)
.queue(function() {
$(this).css({left:'auto'}).dequeue();
//resizingContainer();
});
}
function applyLeft($elem){
/*$elem
.css({ left:0})
.removeClass('rElem')
.stop()
.delay($kao_element.time_effect)
.queue(function() {
resizingContainer();
$(this).dequeue();
});*/
$elem
.css({left:0})
.stop()
.delay($kao_element.time_effect)
.queue(function() {
$(this).removeClass('rElem').dequeue();
//resizingContainer();
});
}
function initialize() {
$numElemRow = "";
var varRPos = $kao_element.oWidth - $kao_element.width + parseInt($kao_element.selector.css("border-right-width"));
var x=0, y=0, i=0,
x_var = $kao_element.oWidth + ($kao_element.oWidth-$kao_element.width)/2,
y_var = parseInt($kao_element.selector.css('marginBottom')) + parseInt($kao_element.selector.css('marginTop')) + $kao_element.oHeight;
setXVar(x_var);
setYVar(y_var);
var nRow = 0;
$kao_element.selector.each(function(){
nRow++;
$(this).attr('data-id',$kao_element.cells[i] = i++);
(x+x_var <= $kao_container.width)?((x+x_var == $kao_container.width)?(
setNumElemRow(nRow-1),
applyRight($(this)),
rPos = x
):applyLeft($(this))):(
setNumElemRow(nRow-1),
applyRight($(this).prev()),
applyLeft($(this)),
y+=y_var,
rPos = x,
x=0);
$(this).css({ translate: [x, y] })
x+=x_var;
$(this).savePos();
$(this).savePosIni();
});
(x+x_var > $kao_container.width)?(x=0,y+=y_var,applyRight($kao_element.selector.last())):null;
$kao_element.next_last_pos = [x,y];
$kao_element.last_pos = $kao_element.next_last_pos;
$('.rElem').cssRule('right:'+parseInt($kao_container.width-$kao_element.oWidth)+'px;');
refreshPage('ini');
window.addEventListener("hashchange", refreshPage, false);
}
function ini_kao_container(args){
var _top = (!args.hasOwnProperty('top'))?0:args.offset().top;
$kao_container = null;
$kao_container = {
selector : args,
width : args.width(),
top : _top
}
}
function ini_kao_element(args){
if (args.hasOwnProperty('options')) {
var timeEff = (!args.options.hasOwnProperty('time_effect_expanding'))?1000:args.options.time_effect_expanding;
var enabScroll = (!args.options.hasOwnProperty('scroll'))?false:args.options.scroll;
} else {
var timeEff = 1000;
var enabScroll = false;
}
var filt = (!args.hasOwnProperty('filter'))?null:args.filter;
var act_exp = (!args.hasOwnProperty('action_expand'))?$activate=true:args.action_expand;
var act_col = (!args.hasOwnProperty('action_collapse'))?args.jqelement.selector:args.action_collapse;
setWidth(args.jqelement.width());
setoWidth(args.jqelement.outerWidth());
setoHeight(args.jqelement.outerHeight());
$kao_element_back = args;
$kao_element = null;
$kao_element = {
selector : args.jqelement,
filter : filt,
action_expand : act_exp,
action_collapse : act_col,
width : $width,
oWidth : $ouWidth,
oHeight : $ouHeight,
max_width : 2*$ouWidth - ($ouWidth-$width)/2,
cells : new Array(),
last_pos : new Array(),
next_last_pos : new Array(),
subelems_to_expand : args.subelements_toExpand,
subelems_effects : args.subelements_effects,
time_effect : timeEff,
scroll : enabScroll
}
//if (!$activate) {
//FILTER
if (args.hasOwnProperty('filter')){
$($kao_element.filter).live('click',function(){
//$(this).filterElements();
});
}
(args.hasOwnProperty('action_expand'))?$(this).activate_expanding():null;
if ($kao_element.action_expand != $kao_element.action_collapse) {
$($kao_element.action_collapse).live('click', function(e){
($(this).is($kao_element.selector.selector))?$(this).collapse():$(this).parents($kao_element.selector.selector).collapse();
resizingContainer();
});
}
$activate = true;
//}
}
$.fn.activate_expanding = function() {
$($kao_element.action_expand).unbind().bind('click', function(e){
e.stopPropagation();
($(this).is($kao_element.selector.selector))?(
($(this).hasClass('large'))?$(this).collapse():$(this).extend()
):(
(
$(this).parents($kao_element.selector.selector).hasClass('large'))?
$(this).parents($kao_element.selector.selector).collapse():
$(this).parents($kao_element.selector.selector).extend()
);
resizingContainer();
});
}
function resizeContainer(){
$kao_container.selector.css({'height' : parseInt($kao_element.selector.last().offset().top + $kao_element.oHeight-$kao_element.selector.first().offset().top)+'px'});
}
function resizingContainer(){
setTimeout(resizeContainer,1000);
}
$.fn.kao = function(args) {
$numElemRow = "";
$x_var = null;
$y_var = null;
$width = null;
$ouWidth = null;
$ouHeight = null;
var $activate = false;
if (!args) {
console.log('Missing element/s in arguments on Kao');
return false;
}
$this = this;
ini_kao_container($this);
ini_kao_element(args);
initialize();
resizingContainer();
}
//RESPONSIVENESS
$(window).bind('resize', function() {
$kao_container.selector.kao($kao_element_back);
resizingContainer();
collapse();
});
})( window, jQuery );