define([ '/common/userObject.js', '/common/common-util.js', '/common/common-hash.js', '/customize/messages.js', '/bower_components/nthen/index.js', ], function (UserObject, Util, Hash, Messages, nThen) { var getConfig = function (Env) { var cfg = {}; for (var k in Env.cfg) { cfg[k] = Env.cfg[k]; } return cfg; }; // Add a shared folder to the list var addProxy = function (Env, id, proxy, leave) { var cfg = getConfig(Env); cfg.sharedFolder = true; cfg.id = id; var userObject = UserObject.init(proxy, cfg); if (userObject.fixFiles) { // Only in outer userObject.fixFiles(); } Env.folders[id] = { proxy: proxy, userObject: userObject, leave: leave }; return userObject; }; // TODO: Remove a shared folder from the list var removeProxy = function (Env, id) { var f = Env.folders[id]; if (!f) { return; } f.leave(); delete Env.folders[id]; }; /* Tools */ var _ownedByMe = function (Env, owners) { return Array.isArray(owners) && owners.indexOf(Env.edPublic) !== -1; }; var _ownedByOther = function (Env, owners) { return Array.isArray(owners) && owners.length && (!Env.edPublic || owners.indexOf(Env.edPublic) === -1); }; var _getUserObjects = function (Env) { var userObjects = [Env.user.userObject]; var foldersUO = Object.keys(Env.folders).map(function (k) { return Env.folders[k].userObject; }); Array.prototype.push.apply(userObjects, foldersUO); return userObjects; }; var _getUserObjectFromId = function (Env, id) { var userObjects = _getUserObjects(Env); var userObject = Env.user.userObject; userObjects.some(function (uo) { if (Object.keys(uo.getFileData(id)).length) { userObject = uo; return true; } }); return userObject; }; var _getUserObjectPath = function (Env, uo) { var fId = Number(uo.id); if (!fId) { return; } var fPath = Env.user.userObject.findFile(fId)[0]; return fPath; }; // Return files data objects associated to a channel for setPadTitle // All occurences are returned, in drive or shared folders var findChannel = function (Env, channel) { var ret = []; Env.user.userObject.findChannels([channel]).forEach(function (id) { ret.push({ data: Env.user.userObject.getFileData(id), userObject: Env.user.userObject }); }); Object.keys(Env.folders).forEach(function (fId) { Env.folders[fId].userObject.findChannels([channel]).forEach(function (id) { ret.push({ fId: fId, data: Env.folders[fId].userObject.getFileData(id), userObject: Env.folders[fId].userObject }); }); }); return ret; }; // Return files data objects associated to a given href for setPadAttribute... var findHref = function (Env, href) { var ret = []; var id = Env.user.userObject.getIdFromHref(href); if (id) { ret.push({ data: Env.user.userObject.getFileData(id), userObject: Env.user.userObject }); } Object.keys(Env.folders).forEach(function (fId) { var id = Env.folders[fId].userObject.getIdFromHref(href); if (!id) { return; } ret.push({ fId: fId, data: Env.folders[fId].userObject.getFileData(id), userObject: Env.folders[fId].userObject }); }); return ret; }; // Return paths linked to a file ID var findFile = function (Env, id) { var ret = []; var userObjects = _getUserObjects(Env); userObjects.forEach(function (uo) { var fPath = _getUserObjectPath(Env, uo); var results = uo.findFile(id); if (fPath) { // This is a shared folder, we have to fix the paths in the results results.forEach(function (p) { Array.prototype.unshift.apply(p, fPath); }); } // Push the results from this proxy Array.prototype.push.apply(ret, results); }); return ret; }; // Returns file IDs corresponding to the provided channels var _findChannels = function (Env, channels, onlyMain) { if (onlyMain) { return Env.user.userObject.findChannels(channels); } var ret = []; var userObjects = _getUserObjects(Env); userObjects.forEach(function (uo) { var results = uo.findChannels(channels); Array.prototype.push.apply(ret, results); }); ret = Util.deduplicateString(ret); return ret; }; var _getFileData = function (Env, id) { var userObjects = _getUserObjects(Env); var data = {}; userObjects.some(function (uo) { data = uo.getFileData(id); if (Object.keys(data).length) { return true; } }); return data; }; // Transform an absolute path into a path relative to the correct shared folder var _resolvePath = function (Env, path) { var res = { id: null, userObject: Env.user.userObject, path: path }; if (!Array.isArray(path) || path.length <= 1) { return res; } var current; var uo = Env.user.userObject; // We don't need to check the last element of the path because we only need to split it // when the path contains an element inside the shared folder for (var i=2; i