-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjsview.js
More file actions
5167 lines (4383 loc) · 171 KB
/
jsview.js
File metadata and controls
5167 lines (4383 loc) · 171 KB
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Hi Developer, this is JavaScriptView
JavaScriptView is an open source project, you can use, modify and adapt it to your needs. Your help is important to improve it.
We want you in the team and collaborate with this little framework. You can email us at [email protected]
Thanks!
*/
//Global vars
//var JSVFullSpinner, JSVStatusFullSpinner = false, JSVActualView, statusActionMenuSide = false, typeProject, tempDevice = 'iOS';
var JSVActualView, statusActionMenuSide = false, typeProject, tempDevice = 'iOS';
//This array save of each view
var JSVDeclareViews = new Array();
//This array save code html of each view ajax loaded
var JSVContainersViews = new Array();
//This array save code html of each view ajax loaded
var JSVNotifications = new Array();
//Keep dom element body in a global variable
var objBody;
$JSView = {
// Register the event listener
//document.addEventListener("backbutton", $JSView.onBackKeyDown, false);
run: function(type){
//We get the type of device to change the height of the head in the case of iOS
if(tempDevice == 'iOS'){
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML += 'jsv-content { top: 64px; }';
style.innerHTML += 'jsv-tabs{ top: 64px; }';
style.innerHTML += '.has-tabs{ top: 109px; }';
style.innerHTML += 'jsv-header { padding-top: 20px; height: 64px }';
document.getElementsByTagName('head')[0].appendChild(style);
}
//Load FastClick
new FastClick(document.body);
//Save type proyect, now we can (null,left,bottom), this corresponds to the menu.
typeProject = type;
//Keep dom element body in a global variable
objBody = $v.select('body');
//Add full spinner div in dom
console.log(spinnerModal);
objBody.innerHTML += spinnerModal;
//Save the reference full spinner div in global var
//JSVfullLoading = $v.select('#JSVcontainerLoading');
},
//DECLARE, INIT, DATA AND GOTO VIEW
declareView: function(e){
//Read json of all views declared
for(var obj in e){
//Keep dom element in a variable
var objElement = $v.select('#' + obj);
//Create the container of all external views to jsv-main
if(!objElement){
//Add to body or container the element jsv-view of the view
objBody.innerHTML += '<jsv-view id="' + obj + '"></jsv-view>';
//Keep dom element in a variable
objElement = $v.select('#' + obj);
//Put all containers out of camera
objElement.classList.add('JSVcontainerRight');
objElement.classList.add('JSVcontainerTransition');
//Save in the array the views declared with its options (url, template, controlator, ...)
if(e.hasOwnProperty(obj)){
JSVDeclareViews[obj] = e[obj];
}
$JSVRequest.do(obj,e[obj].template);
}
}
},
initView: function(e){
//Assign actual view to global var
JSVActualView = e;
//Keep dom element in a variable
var objElement = $v.select('#' + e);
//Put actual view into camera :)
objElement.classList.remove('JSVcontainerRight');
objElement.classList.add('JSVcontainerCenter');
objElement.classList.remove('JSVcontainerBackground');
objElement.classList.add('JSVcontainerForeground');
//Read the template
$JSVRequest.do(e,JSVDeclareViews[e].template,true);
window.history.pushState(e, "Titulo", '/www/index.html#' + e);
},
dataView: function(obj, e) {
// console.group('dataView obj -> ' + obj);
// console.time('dataView');
var contentView = JSVContainersViews[e];
for (var x in obj) {
console.log(x);
console.log(obj[x]);
if(!obj[x]) obj[x]='';
contentView = contentView.replace(new RegExp('{{'+x+'}}', 'g'), obj[x]);
}
//Keep dom element in a variable
var objElement = $v.select('#' + e);
//Remove the previous contents of the container
objElement.innerHTML = ''
//Add the new contents of the container
objElement.innerHTML += contentView;
// console.timeEnd('dataView');
// console.groupEnd();
},
goToView: function(e){
// console.group('goToView e -> ' + e);
// console.time('goToView');
//Keep dom element in a variable
var objMain = $v.select('jsv-main');
var objElement = $v.select('#' + e);
//If the parent of this view YES is jsv-main, we move jsv-main to the left
if($v.select('#' + JSVActualView).parentNode.tagName.toLowerCase() == 'jsv-container'){
//Put the actual view out of camera
objMain.classList.add('JSVcontainerLeft')
objMain.classList.remove('JSVcontainerCenter')
//Put the new view into the camera
objElement.classList.add('JSVcontainerCenter')
objElement.classList.remove('JSVcontainerRight')
//If the parent of this view NO is jsv-main, we move this view to the left
}else{
//Keep dom element in a variable
var objJSVActualView = $v.select('#' + JSVActualView);
//Put the actual view out the camera
objJSVActualView.classList.add('JSVcontainerLeft')
objJSVActualView.classList.remove('JSVcontainerCenter')
//Put the new view into the camera
objElement.classList.add('JSVcontainerCenter')
objElement.classList.remove('JSVcontainerRight')
}
//If the current view is different to the view query now
if(JSVActualView != e){
//Assign actual view to global var
JSVActualView = e;
//Execute the function controller of this view
$JSView.controller[e](e);
//Change the url
window.history.pushState(e, '', '/www/index.html#' + e);
}
/*-----------*/
// console.timeEnd('goToView');
// console.groupEnd();
},
//DECLARE AND ACTION MENU
declareMenu: function(e){
//If we add the jsv-main component to body (This is why we want a menu)
objBody.innerHTML += '<jsv-main></jsv-main>';
//Keep dom element in a variable
var objMain = $v.select('jsv-main');
//Asign element jsv-main from body to objMain var
objMain.innerHTML = '<jsv-container></jsv-container>';
objMain.classList.add('JSVcontainerCenter');
objMain.classList.add('JSVcontainerTransition');
var objContainer = $v.select('jsv-container');
objContainer.classList.add('jsv-main-' + typeProject);
objContainer.classList.add('JSVcontainerCenter');
objContainer.classList.add('JSVcontainerTransition');
//Read json of all views declared
for(var obj in e){
if(obj == 'menu'){
$v.select('jsv-main').innerHTML += '<jsv-menu-' + typeProject + ' id="' + obj + '"></jsv-menu-' + e[obj].type + '>';
$JSVRequest.do(obj, e[obj].template, true);
}else{
//Add the container the jsv-vew element of the view
$v.select('jsv-container').innerHTML += '<jsv-view id="' + obj + '"></jsv-view>';
//Put all containers out the camera
$v.select('#' + obj).classList.add('JSVcontainerBackground');
//Save in array the views declarated with its options (url, template, controlator, ...)
if(e.hasOwnProperty(obj)){
JSVDeclareViews[obj] = e[obj];
}
$JSVRequest.do(obj, e[obj].template);
}
}
},
actionMenu: function(e){
//Keep dom element in a variable
var objContainer = $v.select('jsv-container');
//If execute actionMenu with value in e, is that we want chante the intern view of jsv-main
if (typeof e != 'undefined') {
//Keep dom element in a variable
var objElement = $v.select('#' + e);
var objContainerJSVcontainerForeground = $v.select('jsv-container .JSVcontainerForeground')
objContainerJSVcontainerForeground.classList.add('JSVcontainerBackground');
objContainerJSVcontainerForeground.classList.remove('JSVcontainerForeground');
objElement.classList.remove('JSVcontainerBackground');
objElement.classList.add('JSVcontainerForeground');
//If the current view is different to the view query now
if(JSVActualView != e){
//Assign actual view to global var
JSVActualView = e;
//Execute the function controller of this view
$JSView.controller[e](e);
//Change the url
window.history.pushState(e, '', '/www/index.html#' + e);
}
/*-----------*/
}
//If is the left menu enter here
if(typeProject == 'left'){
if(statusActionMenuSide == false){
objContainer.classList.add('JSVcontainerLeftMenuSide')
objContainer.classList.remove('JSVcontainerCenter')
statusActionMenuSide = true
}else{
objContainer.classList.add('JSVcontainerCenter')
objContainer.classList.remove('JSVcontainerLeftMenuSide')
statusActionMenuSide = false
}
}
},
//DECLARE, OPEN AND CLOSE VIEW MODAL E-> VIEW
declareModal: function(e,options){
//Read json of all views modal declared
for(var obj in e){
//Keep dom element in a variable
var objElement = $v.select('#' + obj);
//Create the content of all external views to jsv-main
if(!objElement){
//Add to body or container the element jsv-view
objBody.innerHTML += '<jsv-modal id="' + obj + '"></jsv-modal>';
objElement = $v.select('#' + obj);
//Put all containers out the camera
objElement.classList.add('JSVcontainerBottom');
objElement.classList.add('JSVcontainerTransition');
//Save in array the views declarated with its options (url, template, controlator, ...)
if(e.hasOwnProperty(obj)){
JSVDeclareViews[obj] = e[obj];
}
$JSVRequest.do(obj,e[obj].template);
}
}
},
openModal: function(e){
//Keep dom element in a variable
var objElement = $v.select('#' + e);
//Change the class and show modal div
objElement.classList.add('JSVcontainerCenter')
objElement.classList.remove('JSVcontainerBottom')
//Assign actual view to global var
JSVActualView = e;
//Execute the function controller of this view
$JSView.controller[e](e);
/*-----------*/
//Change the url
window.history.pushState(e, '', '/www/index.html#'+e);
},
closeModal: function(e){
//Keep dom element in a variable
var objElement = $v.select('#' + e);
//Change the class and hide modal
objElement.classList.add('JSVcontainerBottom')
objElement.classList.remove('JSVcontainerCenter')
//Return to previous url
window.history.back()
setTimeout(function(){
//Assign actual view to global var
JSVActualView = window.history.state
},0);
},
//OPEN AND CLOSE MODAL MENU BOTTOM, E-> VIEW, M-> ID MODAL
openMenuModal: function(e){
var idPatrent = $v.select('#' + e).parentNode.id;
//Change the class and hide modal
$v.select('#' + idPatrent + ' #' + e).classList.add('jsv-modal-menu-show');
//Change the class and show modal div
$v.select('#' + idPatrent + ' #' + e + ' jsv-list').classList.add('JSVcontainerCenter');
//Close menu modal, click background transparent
var liClicked = false
$v.select('#' + idPatrent + ' #' + e + ' jsv-list ').onclick = function(){
liClicked = true;
}
$v.select('#' + idPatrent + ' #' + e).onclick = function(){
if(liClicked == false){
$JSView.closeMenuModal(e);
}
liClicked = false;
}
},
closeMenuModal: function(e){
var idPatrent = $v.select('#' + e).parentNode.id;
//Change the class and hide modal
$v.select('#' + idPatrent + ' #' + e).classList.remove('jsv-modal-menu-show');
$v.select('#' + idPatrent + ' #' + e + ' jsv-list').classList.remove('JSVcontainerCenter');
},
//SHOW NOTIFICATIONS T->text
showNotification: function(t){
var modalId = Date.now();
objBody.innerHTML += '<div id="notification' + modalId + '" class="notification JSVcontainerTop JSVcontainerTransition">' + t + '</div>';
//Put element notification in camera
JSVNotifications[modalId] = setTimeout(function(){
$v.select('#notification' + modalId).classList.add('JSVcontainerCenter');
$v.select('#notification' + modalId).classList.remove('JSVcontainerTop');
//Put element notification out camera and remove element
setTimeout(function(){
$v.select('#notification' + modalId).classList.add('JSVcontainerTop');
$v.select('#notification' + modalId).classList.remove('JSVcontainerCenter');
setTimeout(function(){
$v.select('#notification' + modalId).remove();
clearInterval(JSVNotifications[modalId]);
delete JSVNotifications[modalId];
},250);
},2000);
},100);
},
//INIT PLACEHOLDER FORM
initFormPlaceholder: function(e,form){
//Keep dom element in a variable
var objInputs = $v.selectAll('#' + e + ' #' + form + ' input');
var objTextareas = $v.selectAll('#' + e + ' #' + form + ' textarea');
//Read all severals elements type inputs from form
for(var i = 0; i < objInputs.length; i++){
objInputs[i].onkeyup = function(e){
if(this.value != '')
this.parentElement.childNodes[0].classList.add('show');
else
this.parentElement.childNodes[0].classList.remove('show');
}
}
for(var i = 0; i < objTextareas.length; i++){
objTextareas[i].onkeyup = function(e){
if(this.value != '')
this.parentElement.childNodes[0].classList.add('show');
else
this.parentElement.childNodes[0].classList.remove('show');
}
}
},
//INIT MENU CIRCLE
initMenuCircle: function(e){
//Keep dom element in a variable
var objElement = $v.select('#' + e + ' jsv-menu-circle');
var objElementUl = $v.select('#' + e + ' jsv-menu-circle jsv-list ul');
//Added a style element for animation. It is obligatory for the animation is fluid
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML += '#' + objElement.id + '.JSVmenuCircleOpen { height: ' + (objElementUl.offsetHeight-70) + 'px; }';
document.getElementsByTagName('head')[0].appendChild(style);
//If the user click in ul don´t hide menu, but if click in element parent yes.
var liClicked = false
objElementUl.onclick = function(){
liClicked = true;
}
objElement.onclick = function(){
if(liClicked == false){
if(objElement.classList.contains('JSVmenuCircleOpen') == false){
//Change the class and hide modal
objElement.classList.add('JSVcontainerCenter');
objElement.classList.add('JSVmenuCircleOpen');
}else{
//Change the class and hide modal
objElement.classList.remove('JSVmenuCircleOpen');
objElement.classList.remove('JSVcontainerCenter');
}
}
liClicked = false;
}
},
closeMenuCircle: function(e){
var idPatrent = $v.select('#' + e).parentNode.id;
var objElement = $v.select('#' + idPatrent + ' jsv-menu-circle#' + e);
//Change the class and close menu
objElement.classList.remove('JSVmenuCircleOpen');
objElement.classList.remove('JSVcontainerCenter');
},
//OPEN CLOSE IMAGE POPUP
openImage: function(image,e){
var objElement = $v.select('jsv-image-popup#' + e);
objElement.style.backgroundImage = image.style.backgroundImage;
objElement.classList.add('show');
},
closeImage: function(e){
var objElement = $v.select('jsv-image-popup#' + e);
objElement.classList.remove('show');
setTimeout(function(){
objElement.style.backgroundImage = '';
},250);
},
//ACTION BACK BUTTON
back: function(){
//Return to previous url
window.history.back();
setTimeout(function(){
console.log(window.history.state);
$JSView.returnTo(window.history.state);
},0);
},
onBackKeyDown: function(){
console.log('onBackKeyDown');
$JSView.back();
},
returnTo: function(e){
//Keep dom element in a variable
var objElementJSVActualView = $v.select('#' + JSVActualView);
//If parent of view YES is jsv-main, move to center jsv-main
if($v.select('#' + e).parentNode.tagName.toLowerCase() == 'jsv-container'){
//Keep dom element in a variable
var objMain = $v.select('jsv-main');
//Put all containers out the camera
objElementJSVActualView.classList.add('JSVcontainerRight')
objElementJSVActualView.classList.remove('JSVcontainerCenter')
//Put the new view into camera
objMain.classList.add('JSVcontainerCenter')
objMain.classList.remove('JSVcontainerLeft')
//If parent of view is NOT jsv-main, move to center the view
}else{
//Keep dom element in a variable
var objElement = $v.select('#' + e);
//Put all containers out the camera
objElementJSVActualView.classList.add('JSVcontainerRight')
objElementJSVActualView.classList.remove('JSVcontainerCenter')
//Put the new view into camera
objElement.classList.add('JSVcontainerCenter')
objElement.classList.remove('JSVcontainerLeft')
}
//Assign actual view to global var
JSVActualView = e
},
//This function read all 'a' element and create event onclick to open inappbrowser plugin cordova
parseAllLink: function(e){
$v('a').each(function() {
$v( this ).attr( "onclick", "window.open('"+$v( this ).attr("href")+"', '_blank', 'location=yes')");
$v( this ).removeAttr("href");
});
},
//This function open alert native plugin cordova
openDialog: function(message,title,button){
navigator.notification.alert(
message, // message
null, // callback
title, // title
button // buttonName
);
},
// this function slide using hammerjs
slideHammer:function(id,e){
var elements = $v.selectAll('.JSVscrollerHammer ul li');
var numberElement = elements.length;
var pane_count = numberElement;
var current_pane = 0;
var maxWidth = $v.select(id).parentElement.offsetWidth;
var maxHeight = $v.select(id).parentElement.offsetHeight;
var pane_width = maxWidth;
// indicators
$v.select(id).innerHTML += '<div class="JVSnav"><ul class="JSVindicator"></ul></div>';
var indicator = '';
for (var i = 1; i <= numberElement; i++){
if ( i == 1) indicator += '<li class="active">' + i + '</li>';
else indicator += '<li>' + i + '</li>';
}
$v.select(id + ' .JSVindicator').innerHTML = indicator;
$v.select(id + ' .JSVindicator').style.paddingLeft = (maxWidth - this.innerWidth) + 'px';
// end indicators
for(var i = 0; i < numberElement; i++){
$v.selectAll('.JSVscrollerHammer ul li')[i].style.width = maxWidth + 'px';
$v.selectAll('.JSVscrollerHammer ul li')[i].style.height = maxHeight + 'px';
var touchControl = new Hammer($v.selectAll('.JSVscrollerHammer ul li')[i]);
touchControl.on("swipeleft swiperight dragleft dragright panleft panright panend",function(event,element){
switch(event.type){
case 'panleft':
case 'panright':
var pane_offset = -(100/pane_count) * current_pane;
var drag_offset = ((100/pane_width) * event.deltaX) / pane_count;
var percent = pane_offset+drag_offset;
// slow down at the first and last pane
if((current_pane == 0 && event.direction == Hammer.DIRECTION_RIGHT) ||
(current_pane == pane_count-1 && event.direction == Hammer.DIRECTION_LEFT)) {
drag_offset *= .4;
}
setContainerOffset(percent,false,id,current_pane,event);
break;
case 'swiperight':
previous(event);
break;
case 'swipeleft' :
next(event);
break;
case 'panend':
// more then 50% moved, navigate
if(Math.abs(event.deltaX) > pane_width/2) {
if(event.direction == Hammer.DIRECTION_RIGHT) {
previous(event);
} else {
next(event);
}
}
else {
showPane(current_pane, true);
}
break;
}
});
}
showPane = function(index,animate,id,event){
index = Math.max(0, Math.min(index, pane_count-1));
current_pane = index;
var offset = -((100/pane_count)*current_pane);
setContainerOffset(offset, true,id,current_pane,event);
}
next = function(event){
current_pane++;
return this.showPane(current_pane,true,id,event);
}
previous = function(){
current_pane--;
return this.showPane(current_pane, true,id,event);
}
setContainerOffset = function(percent, animate,id,current_pane,event) {
var container = $v.select('.JSVscrollerHammer ul');
container.style.width = numberElement * maxWidth + 'px';
container.className = "";
if(animate) {
container.className = "animate";
}
var px = ((pane_width * pane_count) / 100) * percent;
container.style.transform = "translate3d("+ percent +"%,0,0) scale3d(1,1,1)";
indicators(id,current_pane);
}
indicators = function(obj,id,current_pane){
$v.select(id + ' .JSVindicator > li.active').className = '';
$v.select(id + ' .JSVindicator > li:nth-child(' + (current_pane+1) + ')').className = 'active';
}
},
// funtion to load Ads Interstitial
interstitial:function(obj,id,e){
var view = e ;
setTimeout(function(view){
var interstitialContainer = $v.select('#interstitialContainer');
var view = $v.select(e);
var maxWidth = $v.select(id).parentElement.offsetWidth;
var maxHeight = $v.select(id).parentElement.offsetHeight;
var div = $v.select('#Adsinterstitial');
div.style.width = (maxWidth - 24) + 'px';
div.style.height = maxHeight + 'px';
var heightFooter = maxHeight - (maxHeight-50);
$v.select('.footerInterstitial').style.height = heightFooter + 'px';
div.style.verticalAlign = maxHeight / 2 + 'px';
div.style.marginTop = (maxHeight/4)-20 + 'px';
$v.select('.contentInterstitial').style.height = (maxHeight/2) + 'px';
$v.select(id).style.zIndex = 0;
$v.select(id).style.position = 'absolute';
$v.select('.contentInterstitial img').src = obj.source;
//show the divs
interstitialContainer.appendChild(div);
$v.select('#' + e).appendChild(interstitialContainer);
//show the divs
div.style.display = 'block';
var duration = obj.duration;
progressBar(obj.duration);
$v.select(".headerInterstitial a").addEventListener('click',function(evt){
evt.preventDefault();
$v.select('#Adsinterstitial').style.display = 'none';
// $v.select('#' + interstitialContainer.id).classList.remove('interstitialContainer');
});
},1000);
progressBar = function(duration){
var progressWidth = 1;
var id = setInterval(function(){
if (progressWidth >= 100) {
clearInterval(id);
// $v.select('#' + interstitialContainer.id).classList.remove('interstitialContainer');
$v.select('#Adsinterstitial').display = 'none';
} else {
progressWidth ++;
$v.select('.progres-bar-container').style.width = progressWidth +'%';
}
},duration)
}
},
//This function create slides
initSlide: function(e, options){
var elements = $v.selectAll(e + ' .JSVscroller ul li');
var numberElement;
var maxWidth;
//Asign width
if(options.responsive == true){
//Responsive
numberElement = elements.length;
maxWidth = $v.select(e).parentElement.offsetWidth;
maxHeight = $v.select(e).parentElement.offsetHeight;
var divs = $v.selectAll(e + ' .JSVscroller ul li');
for (var i = 0; i < numberElement; ++i) {
$v.selectAll(e + ' .JSVscroller ul li')[i].style.width = maxWidth + 'px';
$v.selectAll(e + ' .JSVscroller ul li')[i].style.height = maxHeight + 'px';
}
$v.select(e + ' .JSVscroller').style.width = (maxWidth * numberElement) + 'px';
}else if(options.width && options.height){
//Absolute
$v.select(e).style.width = options.width + 'px';
$v.select(e).style.height = options.height + 'px' ;
numberElement = elements.length;
maxWidth = options.width;
for (var i = 0; i < numberElement; ++i) {
$v.selectAll(e + ' .JSVscroller ul li')[i].style.width = maxWidth + 'px';
}
$v.select(e + ' .JSVscroller').style.width = (maxWidth * numberElement) + 'px';
}else{
console.warn("Debes definir un diseño responsive o absoluto con width y eight.")
}
/*---------------------*/
//Create the indicators
if(options.indicators == true){
$v.select(e).innerHTML += '<div class="JVSnav"><ul class="JSVindicator"></ul></div>';
var indicator = '';
for (var i = 1; i <= numberElement; i++){
if ( i == 1) indicator += '<li class="active">' + i + '</li>';
else indicator += '<li>' + i + '</li>';
}
$v.select(e + ' .JSVindicator').innerHTML = indicator;
$v.select(e + ' .JSVindicator').style.paddingLeft = (maxWidth - this.innerWidth) + 'px';
}
/*---------------------*/
//Add slide to array
new iScroll(e, {
snap: true,
momentum: false,
hScrollbar: false,
onScrollEnd: function () {
if(options.indicators == true){
$v.select(e + ' .JSVindicator > li.active').className = '';
$v.select(e + ' .JSVindicator > li:nth-child(' + (this.currPageX+1) + ')').className = 'active';
}
}
});
/*---------------------*/
},
//This function create slides ADVERTISING
initSlideAdvertising: function(e, options){
var elements = $v.selectAll(e + ' .JSVscrollerAd ul li');
var numberElement;
var maxWidth;
//Asign width
if(options.responsive == true){
//Responsive
numberElement = elements.length;
maxWidth = $v.select(e).parentElement.offsetWidth;
maxHeight = $v.select(e).parentElement.offsetHeight;
var divs = $v.selectAll(e + ' .JSVscrollerAd ul li');
for (var i = 0; i < numberElement; ++i) {
$v.selectAll(e + ' .JSVscrollerAd ul li')[i].style.width = maxWidth + 'px';
$v.selectAll(e + ' .JSVscrollerAd ul li')[i].style.height = maxHeight + 'px';
}
$v.select(e + ' .JSVscrollerAd').style.width = (maxWidth * numberElement) + 'px';
}else if(options.width && options.height){
//Absolute
$v.select(e).style.width = options.width + 'px';
$v.select(e).style.height = options.height + 'px' ;
numberElement = elements.length;
maxWidth = options.width;
maxHeight = options.height;
for (var i = 0; i < numberElement; ++i) {
$v.selectAll(e + ' .JSVscrollerAd ul li')[i].style.width = (maxWidth-60) + 'px';
$v.selectAll(e + ' .JSVscrollerAd ul li')[i].style.height = maxHeight + 'px';
}
$v.select(e + ' .JSVscrollerAd').style.width = ((maxWidth-40) * numberElement)-20 + 'px';
}else{
console.warn("Debes definir un diseño responsive o absoluto con width y eight.")
}
/*---------------------*/
//Add slide to array
new iScroll(e, {
snap: true,
momentum: false,
hScrollbar: false,
advertising: true,
onScrollStart: function () {
console.log(this.currPageX+1)
}
});
/*---------------------*/
},
//This function start the tabs
initTabs: function(e, options){
for (var i = 0; i < $v.selectAll('jsv-tabs jsv-tab').length; ++i) {
$v.selectAll('jsv-tabs jsv-tab')[i].addEventListener('click', function() {
//Active the tab touched
$v.select('#' + e + ' jsv-tabs > jsv-tab.active').classList.remove('active');
this.classList.add('active');
//Active jsv-content of asigned jsv-tab
litems=this.parentNode.getElementsByTagName('jsv-tab')
for (i=0;i<litems.length;i++){
if (litems.item(i)==this){
$v.select('#' + e + ' jsv-content > jsv-tab.active').classList.remove('active');
$v.selectAll('#' + e + ' jsv-content > jsv-tab')[i].classList.add('active');
}
}
}, false);
}
},
//This function query urls externals with several parameters
query: function(type, url, header, data){
// Return a new promise.
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.open(type, url);
req.onload = function() {
if (req.status == 200 || req.status == 0) {
resolve(req.response);
}
else {
reject(Error(req.statusText));
}
};
req.onerror = function() {
reject(Error("Network Error"));
};
req.send();
});
},
//This function add refresh and loadmore function to scroll view
initScroll: function (obj, e){
//THIS FUNCTION IS NOW IN TEST
//obj -> type, url, header, data, increase, refresh, loadmore
//Now only can use "type" and "url"
// console.group('initLoad');
// console.time('initLoad');
//Save item to repeat from list
var item = $v.select('#' + e + ' jsv-content jsv-list').innerHTML;
//Remove the previous contents of the container
$v.select('#' + e + ' jsv-content jsv-list').innerHTML = '';
var contentView;
if(obj.type == undefined && obj.url == undefined ){
if (typeof obj.isArray === 'undefined') {
//Code depending on result
console.log(obj);
// console.group('query');
// console.time('query');
contentView = '';
var resultQuery = obj;
for (key in resultQuery) {
itemInter = item;
for (var x in resultQuery[key]) {
if(!resultQuery[key][x]) resultQuery[key][x]='';
itemInter = itemInter.replace(new RegExp('{{'+x+'}}', 'g'), resultQuery[key][x]);
}
contentView += itemInter;
}
//Add the new contents of the container
$v.select('#' + e + ' jsv-content jsv-list').innerHTML += contentView;
// console.timeEnd('query');
// console.groupEnd();
}
}
if(obj.type != undefined && obj.url != undefined ){
//Load init data from external service
$JSView.query(obj.type, obj.url).then(function(result) {
//Code depending on result
console.log(JSON.parse(result));
// console.group('query');
// console.time('query');
contentView = '';
var resultQuery = JSON.parse(result);
for (key in resultQuery) {
itemInter = item;
for (var x in resultQuery[key]) {
if(!resultQuery[key][x]) resultQuery[key][x]='';
itemInter = itemInter.replace(new RegExp('{{'+x+'}}', 'g'), resultQuery[key][x]);
}
contentView += itemInter;
}
//Add the new contents of the container
$v.select('#' + e + ' jsv-content jsv-list').innerHTML += contentView;
// console.timeEnd('query');
// console.groupEnd();
}).catch(function() {
//An error occurred
console.log('An error occurred');
});
}
if(obj.refresh == true){
$v.select('#' + e + ' jsv-content').style.top = '24px';
var starty = 0;
var actionReload = false;
//Add spinner
$v.select('#' + e + ' jsv-content jsv-refresh').innerHTML = spinner;
$v.select('#' + e + ' jsv-content').addEventListener('touchstart', function(element){
if(actionReload == false) starty = element.changedTouches[0].pageY;
}, false)
$v.select('#' + e + ' jsv-content').addEventListener('touchmove', function(element){
if($v.select('#' + e + ' jsv-content').scrollTop == 0 && element.changedTouches[0].pageY > starty){
$v.select('#' + e + ' jsv-content').style.webkitTransform = 'translate3d(0px,' + (element.changedTouches[0].pageY - starty) + 'px,0px)';
$v.select('#' + e + ' jsv-content').style.transform = 'translate3d(0px,' + (element.changedTouches[0].pageY - starty) + 'px,0px)';
actionReload = true;
}
}, false)
$v.select('#' + e + ' jsv-content').addEventListener('touchend', function(element){
//If actionReload is true and scroll positionY is more than 44px
if(actionReload == true && element.changedTouches[0].pageY > (starty+44)){
$v.select('#' + e + ' jsv-content').classList.add('JSVcontainerTransition');
$v.select('#' + e + ' jsv-content').style.webkitTransform = 'translate3d(0px,44px,0px)';
$v.select('#' + e + ' jsv-content').style.transform = 'translate3d(0px,44px,0px)';
$JSView.query(obj.type, obj.url).then(function(result) {
//Code depending on result
console.log(JSON.parse(result));
// console.group('query');
// console.time('query');
contentView = '';
var resultQuery = JSON.parse(result);
for (key in resultQuery) {
itemInter = item;
for (var x in resultQuery[key]) {
if(!resultQuery[key][x]) resultQuery[key][x]='';
itemInter = itemInter.replace(new RegExp('{{'+x+'}}', 'g'), resultQuery[key][x]);
}
contentView += itemInter;
}
//Add the new contents of the container
$v.select('#' + e + ' jsv-content jsv-list').innerHTML = contentView;
//Return to init position
$v.select('#' + e + ' jsv-content').style.webkitTransform = 'translate3d(0px,0px,0px)';
$v.select('#' + e + ' jsv-content').style.transform = 'translate3d(0px,0px,0px)';
//$v.select('#' + e + ' jsv-content').style.transform = 'initial';
setTimeout(function(){
$v.select('#' + e + ' jsv-content').classList.remove('JSVcontainerTransition');
actionReload = false;
},250)
// console.timeEnd('query');
// console.groupEnd();
}).catch(function() {
// an error occurred
console.log('An error occurred');
});
//If actionReload is false and scroll positionY is less than 44px
}else{
$v.select('#' + e + ' jsv-content').classList.add('JSVcontainerTransition');
//Return to init position
$v.select('#' + e + ' jsv-content').style.webkitTransform = 'translate3d(0px,0px,0px)';
$v.select('#' + e + ' jsv-content').style.transform = 'translate3d(0px,0px,0px)';
//$v.select('#' + e + ' jsv-content').style.transform = 'initial';
setTimeout(function(){
$v.select('#' + e + ' jsv-content').classList.remove('JSVcontainerTransition');
actionReload = false;
},250)
}
}, false)
}
if(obj.loadmore == true){
//Add spinner
$v.select('#' + e + ' jsv-content jsv-loadmore').innerHTML = spinner;
$v.select('#' + e + ' jsv-content').onscroll = function(){
if(($v.select('#' + e + ' jsv-content').offsetHeight + $v.select('#' + e + ' jsv-content').scrollTop) == $v.select('#' + e + ' jsv-content').scrollHeight){
$JSView.query(obj.type, obj.url).then(function(result) {
//Code depending on result
console.log(JSON.parse(result));
// console.group('query');
// console.time('query');
contentView = '';
var resultQuery = JSON.parse(result);
for (key in resultQuery) {
itemInter = item;
for (var x in resultQuery[key]) {
if(!resultQuery[key][x]) resultQuery[key][x]='';
itemInter = itemInter.replace(new RegExp('{{'+x+'}}', 'g'), resultQuery[key][x]);
}
contentView += itemInter;
}
//Add the new contents of the container
$v.select('#' + e + ' jsv-content jsv-list').innerHTML += contentView;
// console.timeEnd('query');
// console.groupEnd();
}).catch(function() {
// an error occurred
console.log('An error occurred');
});
}
}
}
}
}
//This function show or hide modal Spinner
$JSVspinner = {
show: function(){
var JSVfullSpinner = $v.select('jsv-fullspinner');
JSVfullSpinner.classList.add('JSVshowLoading');
JSVfullSpinner.classList.remove('JSVhideLoading');
JSVstatusFullSpinner = true;
},
hide: function(){
var JSVfullSpinner = $v.select('jsv-fullspinner');
JSVfullSpinner.classList.add('JSVhideLoading');
JSVfullSpinner.classList.remove('JSVshowLoading');
JSVstatusFullSpinner = false;
}
}