1: /// <reference path="jquery-1.5-vsdoc.js" />
2:
3: var fileManager = {
4: startPos: 0
5: , xhr: null
6: , root: ""
7: , holdBGColor: ""
8: , basePath: ""
9: , scrollTop: 0
10: , timeOut: null
11: , wiatImg: null
12: , actions: { IMGS: 0, DIRS: 1, BOTH: 2, MOVE: 3, REMOVE: 4, ADD: 5 }
13: , max: { w: 180, h: 167 }
14: , dataVals: { currentFolder: "" }
15: , dirs: []
16: , request: function () {
17:
18: $("abbr#waitResult").addClass("waitResult");
19:
20: if (fileManager.xhr !== null) {
21: fileManager.xhr.abort();
22: }
23:
24: fileManager.xhr = $.ajax({
25: type: "POST",
26: url: "fileManagerHandler.ashx",
27: data: { p: JSON.stringify(fileManager.dataVals) },
28: dataType: "json",
29: complete: fileManager.handleAsyncComplete,
30: success: fileManager.handleAsyncRequest,
31: error: fileManager.handleAsyncError
32: });
33: }
34: , handleAsyncRequest: function (data) {
35:
36: if (typeof data === "undefined" || data === null || !(data.hasOwnProperty("files") || data.hasOwnProperty("directories"))) {
37: return;
38: }
39:
40: var i, obj = $("#fileMngerImgDsply");
41: obj.empty();
42: if (fileManager.dataVals.action !== fileManager.actions.DIRS && fileManager.dataVals.action !== fileManager.actions.ADD) {
43: if (data.files !== null) {
44: if (data.files.length > 0) {
45: for (i = 0; i < data.files.length; i++) {
46: var img = $(fileManager.displayImgs(data.files[i]));
47:
48: obj.append(img);
49:
50: var imgDsply = img.find(".imgDsply");
51: imgDsply.find("img").css({ "margin-top": (imgDsply.height() - imgDsply.find("img").height()) / 2 });
52:
53: }
54: } else {
55: obj.append("<h2><img src='images/empty.png' alt='Empty Folder' /><div>Empty</div></h2>");
56: }
57: }
58: }
59:
60: if (data.directories !== null) {
61: obj = $("#fileMngerDir");
62:
63: var uObj = obj.find("img[title='" + fileManager.dataVals.currentFolder + "']");
64:
65: if (uObj.length !== 0) {
66: obj = uObj.parent();
67: obj.find("p").remove();
68:
69: fileManager.writeDirs(data.directories, obj);
70:
71: uObj.trigger("click");
72: $("#fileMngerFooter input[type='text']").val("");
73: } else {
74: obj.find("p").remove();
75: fileManager.writeDirs(data.directories, obj);
76: }
77: }
78:
79: }
80: , handleAsyncComplete: function () {
81: fileManager.hideFlyoutCMDs();
82: $("#fileMngerImgDsply").scrollTop(fileManager.scrollTop);
83: $("abbr#waitResult").removeClass("waitResult");
84: }
85: , handleAsyncError: function (xhr, status, e) {
86: if (e !== "abort") {
87: fileManager.hideFlyoutCMDs();
88: $("abbr#waitResult").removeClass("waitResult");
89: $("#fileMngerImgDsply").empty().append("<h2>Error occured while retrieving files. Please retry your request.</h2>");
90: alert(e);
91: }
92: }
93: , displayImgs: function (dt) {
94: var imgBox = document.createElement("div");
95: imgBox.className = "imgBox";
96:
97: var div = document.createElement("div");
98: div.className = "imgDsply";
99:
100: var img = fileManager.writeImg(dt);
101: div.appendChild(img);
102:
103: imgBox.appendChild(div);
104:
105: div = document.createElement("div");
106: div.className = "captionImgDsply";
107: div.innerHTML = "<b>" + dt.name + "</b><br />" + dt.w + "x" + dt.h;
108: imgBox.appendChild(div);
109:
110: $(img).draggable({
111: helper: "clone",
112: opacity: "0.25"
113: });
114: $(imgBox).mouseenter(function () {
115: fileManager.flyoutCMDs(dt, $(this))
116: });
117: return imgBox;
118: }
119: , writeImg: function (data) {
120: var root = fileManager.root + "fileManager/";
121: var ext = $.trim(data.ext);
122: var chgFile = false;
123: var d = fileManager.calcDimensions(data.w, data.h);
124:
125: if (fileManager.fileExts.indexOf(ext) !== -1) {
126: chgFile = true;
127: data.w = 128;
128: data.h = 128;
129: d.w = 128;
130: d.h = 128;
131: }
132:
133: if (data.w < fileManager.max.w && data.h < fileManager.max.h) {
134: d.w = data.w;
135: d.h = data.h;
136: }
137: var qStr = {
138: dmens: { width: data.w, height: data.h },
139: max: { width: d.w, height: d.h },
140: ext: ext
141: }
142:
143: if (chgFile) {
144: qStr.imgName = root + "images/fileicons/" + ext.replace(".", "") + ".png";
145: } else {
146: qStr.imgName = data.path + data.name;
147: }
148:
149: var img = document.createElement("img");
150: img.id = "img_" + data.id;
151: img.src = root + "ThumbHandler.ashx?p=" + JSON.stringify(qStr);
152: img.title = data.name;
153: img.width = d.w;
154: img.height = d.h;
155:
156: img.onclick = function () { fileManager.bigImg({ name: data.name, ext: data.ext, w: data.w, h: data.h }); return false; };
157: return img;
158: }
159: , writeDirs: function (oDir, obj) {
160: var div, img, elem, i;
161: for (i = 0; i < oDir.subDir.length; i++) {
162:
163: elem = $("<p>");
164:
165: $("<img>")
166: .attr({ "src": fileManager.root + "fileManager/images/folder_close.png", "alt": "Directory Collapsed", "title": oDir.subDir[i].path })
167: .droppable({
168: tolerance: "pointer",
169: greedy: true,
170: drop: function (e, ui) {
171: var file = ui.draggable.attr("title");
172: var folder = $(this).attr("title");
173: if (confirm("Are you sure you want to move " + file + " to " + folder)) {
174: var Id = ui.draggable.attr("id");
175: var id = Id.substring(Id.lastIndexOf("_") + 1);
176: fileManager.dataVals.action = fileManager.actions.MOVE;
177: fileManager.dataVals.file = file;
178: fileManager.dataVals.folder = folder;
179: fileManager.dataVals.id = id;
180: fileManager.request();
181: }
182: }
183: }).appendTo(elem);
184:
185: $("<span>").attr("title", oDir.subDir[i].name).hover(function () {
186: fileManager.holdBGColor = $(this).css("background-color");
187: $(this).css("background-color", "#ddd");
188: }, function () {
189: $(this).css("background-color", fileManager.holdBGColor);
190: }).append(oDir.subDir[i].name).appendTo(elem);
191:
192: obj.append(elem);
193:
194: if (oDir.subDir[i].subDir.length !== 0) {
195: fileManager.writeDirs(oDir.subDir[i], elem);
196: }
197: }
198: }
199: , folderClick: function (e) {
200: var obj = $(e.target); //Clicked on folder image.
201:
202: //Test if clicked item is an folder image.
203: //Image elements have a src attribute, and if not then return.
204: var attr = obj.attr("src");
205: if (typeof attr === "undefined" || attr === false) {
206: return false;
207: }
208:
209: var parent = obj.parent();
210: fileManager.toggleFolders(parent);
211:
212: //Was there a control click or command click was raised
213: if (e.metaKey) {
214: parent.find("img:first").attr("src", fileManager.root + "fileManager/images/folder_open.png").end()
215: .find("p").hide();
216: } else {
217: parent.children("p").show();
218: obj.attr("src", fileManager.root + "fileManager/images/folder_open.png");
219: }
220:
221: fileManager.refreshPath(obj.attr("title"), parent);
222:
223: e.stopPropagation();
224: }
225: , refreshPath: function (path, parent) {
226: $("#fileMngerHead div").html(parent.find("span").html() + " Folder"); //Set Header as title of folder
227:
228: //No need to retrieve new data when clicked on same folder.
229: if (fileManager.dataVals.currentFolder === path) {
230: return false;
231: }
232:
233: $("#fileMngerImgDsply").empty().append("<h2><img src='images/uploading.gif' alt='Wait While Loading...' />Retrieving Files...</h2>");
234:
235: fileManager.dataVals.currentFolder = path; //Store current folder path
236:
237: fileManager.breadCrumbsClick(path); //Write header crumbs to current location
238:
239: //Request image data of current folder
240: fileManager.dataVals.action = fileManager.actions.IMGS;
241: fileManager.request();
242: }
243: , toggleFolders: function (obj) {
244:
245: //Close all folders and open any Grand Parents.
246: $("#fileMngerDir img").attr("src", fileManager.root + "fileManager/images/folder_close.png");
247: obj.parents("p").find("img:first").attr("src", fileManager.root + "fileManager/images/folder_open.png");
248:
249: }
250: , breadCrumbsClick: function (path) {
251: var headBox = $("#fileMngerHead span").html("");
252: var sPath = fileManager.root + fileManager.basePath;
253: var p = path.replace(sPath, "").split("/");
254:
255: $.each(p, function (i, value) {
256: if (value !== "") {
257: sPath += value + "/";
258: var path = sPath;
259:
260: $("<img>").attr({ "src": "images/seperator.png", "class": "bcSeperator", "alt": "Bread Crumb Seperator" }).appendTo(headBox);
261:
262: $("<a>").attr({ "href": "#" })
263: .text(value)
264: .click(function (e) {
265: $("#fileMngerDir img[title='" + path + "']").trigger("click");
266: e.stopPropagation();
267: })
268: .appendTo(headBox);
269: }
270: });
271: }
272: , hideFlyoutCMDs: function () {
273: $("#flyoutCMDs").hide();
274: }
275: , calcDimensions: function (w, h) {
276: var p, d = {};
277: if (w > h) {
278: p = h / w;
279: d.w = fileManager.max.w;
280: d.h = Math.floor(fileManager.max.w * p);
281: if (d.h > fileManager.max.h) {
282: d.h = fileManager.max.h;
283: d.w = parseInt(d.h * w / h);
284: }
285: } else {
286: p = w / h;
287: d.h = fileManager.max.h;
288: d.w = Math.floor(d.h * p);
289: if (d.w > fileManager.max.w) {
290: d.w = fileManager.max.w;
291: d.h = parseInt(d.w * h / w);
292: }
293: }
294: return d;
295: }
296: , flyoutCMDs: function (details, imgBox) {
297: try {
298: var img = $(imgBox).find("img");
299: var dt = $.parseJSON(img.attr("src").replace(fileManager.root + "fileManager/ThumbHandler.ashx?p=", ""));
300: var cm = $("#flyoutCMDs img:first");
301: var parent = cm.parent();
302:
303: var sId = img.attr("id");
304: var title = { id: sId.substring(sId.lastIndexOf("_") + 1), imgName: dt.imgName, width: dt.dmens.width, height: dt.dmens.height };
305:
306: title.action = "Remove";
307: cm.attr("title", JSON.stringify(title));
308: title.action = "SV";
309: cm.next().attr("title", JSON.stringify(title));
310:
311: parent.show();
312: var position = imgBox.position();
313: parent.css({ "top": position.top + 12, "left": position.left + 12 });
314: } catch (e) {
315:
316: }
317: }
318: , removeImg: function (e) {
319: try {
320: var t = $.parseJSON($(e.target).attr("title"));
321: var ext = t.imgName.substring(t.imgName.lastIndexOf("."));
322: var name = t.imgName.substring(t.imgName.lastIndexOf("/") + 1);
323:
324: if (t.action === "Remove") {
325: if (confirm("Are you sure you want to remove " + name + "?")) {
326: fileManager.scrollTop = $("#fileMngerImgDsply").scrollTop();
327: fileManager.dataVals.action = fileManager.actions.REMOVE;
328: fileManager.dataVals.file = name;
329: fileManager.dataVals.id = t.id;
330: fileManager.request();
331: }
332: } else {
333: var isType = typeof fileManager_Select;
334: if (isType !== "undefined" && isType === "function") {
335:
336: } else {
337: fileManager.bigImg({ name: name, ext: ext, w: t.width, h: t.height });
338: }
339: }
340: } catch (ex) {
341:
342: }
343: e.stopPropagation();
344: }
345: , bigImg: function (dt) {
346: var parentBox = $("#fileMngerContents");
347: var divBox = $("<div>").addClass("bigImg").css("width", parentBox.width());
348: divBox.appendTo(parentBox);
349:
350: var hVals = { w: fileManager.max.w, h: fileManager.max.h };
351: fileManager.max.w = divBox.width();
352: fileManager.max.h = divBox.height();
353:
354: var d = fileManager.calcDimensions(dt.w, dt.h);
355: divBox.css("padding-top", Math.floor((divBox.height() - d.h) / 2));
356:
357: var qStr = { imgName: fileManager.root + fileManager.dataVals.currentFolder + dt.name, dmens: { width: dt.w, height: dt.h }, max: { width: d.w, height: d.h }, ext: dt.ext };
358:
359: var img = $("<img>").attr({
360: "src": fileManager.root + "fileManager/ThumbHandler.ashx?p=" + JSON.stringify(qStr),
361: "title": dt.name,
362: "width": d.w,
363: "height": d.h
364: });
365:
366: divBox.append(img);
367:
368: fileManager.max.w = hVals.w;
369: fileManager.max.h = hVals.h;
370:
371: $(document).click(function (e) {
372: if ($(e.target).parents("div.imgBox").length === 0) {
373: $(".bigImg").remove();
374: $(document).unbind("click");
375: }
376: });
377:
378: }
379: , goHome: function (e) {
380:
381: $("#fileMngerDir img").attr("src", fileManager.root + "fileManager/images/folder_close.png");
382:
383: if (e.metaKey) {
384: $("#fileMngerDir > p p").hide();
385: }
386: //$("#fileMngerDir img[title='" + path + "']");
387: var path = fileManager.root + fileManager.basePath;
388: if (path === fileManager.dataVals.currentFolder) {
389: return;
390: }
391:
392: $("#fileMngerImgDsply").empty().append("<h2><img src='images/uploading.gif' alt='Wait While Loading...' />Retrieving Files...</h2>");
393:
394: $("#fileMngerHead span").html("");
395: fileManager.dataVals.currentFolder = path;
396: fileManager.dataVals.action = fileManager.actions.IMGS;
397: fileManager.request();
398:
399: $("#fileMngerHead div").html("Home Folder");
400: e.stopPropagation();
401: }
402: , newDir: function (e) {
403: var obj = $(e.target);
404:
405: var attr = obj.attr("src");
406: if (typeof attr === "undefined" || attr === false) {
407: return false;
408: }
409: var txt = $("#fileMngerFooter input[type='text']").val();
410: stipTxt = txt.replace(/^[^a-z0-9_ -]+$/ig, "");
411: if (stipTxt !== "") {
412: fileManager.dataVals.action = fileManager.actions.ADD;
413: fileManager.dataVals.file = txt;
414: fileManager.request();
415: } else {
416: alert("Invalid directory name.");
417: }
418: e.stopPropagation();
419: }
420: , init: function (root, basePath) {
421: if (typeof root === "undefined" || root === "") {
422: alert("Missing to include root path within aspx page");
423: return;
424: }
425: if (typeof basePath === "undefined" || basePath === "") {
426: alert("Missing to include media path within aspx page");
427: return;
428: }
429:
430: basePath = basePath + "/";
431: root = root + "/";
432:
433: fileManager.basePath = basePath.replace(/\/\//g, "/");
434: fileManager.root = root.replace(/\/\//g, "/");
435: fileManager.dataVals.currentFolder = fileManager.root + fileManager.basePath;
436:
437: fileManager.waitImg = document.createElement("img");
438: fileManager.waitImg.src = fileManager.root + "fileManager/images/wait.gif";
439:
440: $("#fileMngerDir").click(fileManager.folderClick);
441: $("#fileMngerImgDsply").scroll(fileManager.hideFlyoutCMDs)
442: .mouseleave(function () { fileManager.timeOut = setTimeout(fileManager.hideFlyoutCMDs, 300); });
443: $("#flyoutCMDs").click(fileManager.removeImg)
444: .mouseenter(function () { clearTimeout(fileManager.timeOut); })
445: $("#imgHome").attr("title", fileManager.dataVals.currentFolder).click(fileManager.goHome);
446: $("#fileMngerFooter img").click(fileManager.newDir);
447:
448: fileManager.dataVals.action = fileManager.actions.BOTH;
449: fileManager.request();
450: }
451: , fileUploaded: function () {
452: fileManager.dataVals.action = fileManager.actions.IMGS;
453: fileManager.request();
454: }
455: , fileExts: ".avi, .chm, .css, .dll, .doc, .docx, .f4v, .fla, .flv, .htm, .html, .ini, .jar, .js, .lasso, .mdb, .mov, .mp3, .mpg, .pdf, .php, .ppt, .py, .rb, .real, .reg, .rtf, .sql, .swf, .txt, .vbs, .wav, .wma, .wmv, .xls, .xml, .xsl, .zip"
456: , imgExts: ".bmp, .gif, .jpeg, .jpg, .png"
457: };