This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.PreviewFactory.js
189 lines (166 loc) · 6.24 KB
/
class.PreviewFactory.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
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
/**
* Responsible for generating previews of nodes
*/
Class.create("PreviewFactory", {
sequencialLoading : true,
imagesHash : $H(),
_crtImageIndex: 0,
_thumbSize: 64,
initialize : function(options){
this.imagesHash = $H();
},
clear : function(){
this.imagesHash = $H();
},
generateBasePreview: function(ajxpNode){
return AbstractEditor.prototype.getPreview(ajxpNode);
},
setThumbSize : function(tSize){
this._thumbSize = tSize;
},
enrichBasePreview: function(ajxpNode, mainObject){
var editors = ajaxplorer.findEditorsForMime((ajxpNode.isLeaf()?ajxpNode.getAjxpMime():"mime_folder"), true);
if(editors && editors.length)
{
this._crtImageIndex ++;
var imgIndex = this._crtImageIndex;
mainObject.IMAGE_ELEMENT.writeAttribute("data-is_loaded", "false");
mainObject.IMAGE_ELEMENT.writeAttribute("id", "ajxp_image_"+imgIndex);
var crtIndex = this._crtImageIndex;
ajaxplorer.loadEditorResources(editors[0].resourcesManager);
var editorClass = Class.getByName(editors[0].editorClass);
if(editorClass){
var oImageToLoad = {
index:"ajxp_image_"+crtIndex,
ajxpNode:ajxpNode,
editorClass:editorClass,
mainObject:mainObject
};
if(this.sequencialLoading){
this.imagesHash.set(oImageToLoad.index, oImageToLoad);
}else{
this.loadImage(oImageToLoad);
}
}
}
},
/**
* Queue processor for thumbnail async loading
*/
loadNextImage: function(){
if(this.imagesHash && this.imagesHash.size())
{
if(this.loading) return;
var oImageToLoad = this.imagesHash.unset(this.imagesHash.keys()[0]);
oImageToLoad.PFacLoader = new Image();
oImageToLoad.PFacLoader.onerror = this.loadNextImage.bind(this);
var loader = function(){
var img = oImageToLoad.mainObject.IMAGE_ELEMENT || $(oImageToLoad.index);
if(img == null || oImageToLoad.PFacLoader == null) return;
var newImg = oImageToLoad.editorClass.prototype.getPreview(oImageToLoad.ajxpNode);
newImg.setAttribute("data-is_loaded", "true");
if(img.parentNode) {
img.parentNode.replaceChild(newImg, img);
}
oImageToLoad.mainObject.IMAGE_ELEMENT = newImg;
this.resizeThumbnail(newImg);
oImageToLoad.PFacLoader = null;
this.loadNextImage();
}.bind(this);
oImageToLoad.PFacLoader.src = oImageToLoad.editorClass.prototype.getThumbnailSource(oImageToLoad.ajxpNode);
if(oImageToLoad.PFacLoader.readyState && oImageToLoad.PFacLoader.readyState == "complete"){
loader();
}else{
oImageToLoad.PFacLoader.onload = loader;
}
}else{
//if(oImageToLoad.PFacLoader) oImageToLoad.PFacLoader = null;
}
},
/**
*
* @param oImageToLoad
*/
loadImage : function(oImageToLoad){
var imageLoader = new Image();
imageLoader.editorClass = oImageToLoad.editorClass;
imageLoader.src = imageLoader.editorClass.prototype.getThumbnailSource(oImageToLoad.ajxpNode);
var loader = function(){
var img = oImageToLoad.mainObject.IMAGE_ELEMENT || $(oImageToLoad.index);
if(img == null || imageLoader == null) return;
var newImg = imageLoader.editorClass.prototype.getPreview(oImageToLoad.ajxpNode);
newImg.setAttribute("data-is_loaded", "true");
img.parentNode.replaceChild(newImg, img);
oImageToLoad.mainObject.IMAGE_ELEMENT = newImg;
this.resizeThumbnail(newImg);
delete imageLoader;
}.bind(this);
if(imageLoader.readyState && imageLoader.readyState == "complete"){
loader();
}else{
imageLoader.onload = loader;
}
},
/**
* Resize the thumbnails
* @param element HTMLElement Optionnal, if empty all thumbnails are resized.
*/
resizeThumbnail: function(element){
var thumbSize = this._thumbSize;
var defaultMargin = 5;
var tW, tH, mT, mB;
if(element.resizePreviewElement && element.getAttribute("data-is_loaded") == "true")
{
element.resizePreviewElement({width:thumbSize, height:thumbSize, margin:defaultMargin});
}
else
{
if(thumbSize >= 64)
{
tW = tH = 64;
mT = parseInt((thumbSize - 64)/2) + defaultMargin;
mB = thumbSize+(defaultMargin*2)-tH-mT-1;
}
else
{
tW = tH = thumbSize;
mT = mB = defaultMargin;
}
element.setStyle({width:tW+'px', height:tH+'px', marginTop:mT+'px', marginBottom:mB+'px'});
}
},
renderSimpleLink: function(ajxpNodeOrPath, label){
var p;
if(typeof ajxpNodeOrPath == "string"){
p = ajxpNodeOrPath;
}else{
p = ajxpNodeOrPath.getPath();
}
var link = new Element('span', {className:'simple_goto_link', 'data-ajaxplorerGoto':p}).update(label?label:p);
link.observe("click", function(e){
this.goTo(p);
}.bind(ajaxplorer));
return link;
},
renderUserLink: function(userId){
}
});