Plato on Github
Report Home
space/_folder_info.js
Maintainability
61.52
Lines of code
273
Difficulty
19.13
Estimated Errors
1.74
Function weight
By Complexity
By SLOC
/*global Y, window, YUI */ /** *
* #folder-info. * ~/miiicasa/static/index/space/_folder_info.js *
* * Spec: http://produce.corp.miiicasa.com/spec/music/20120627/ * Spec: http://produce.corp.miiicasa.com/spec/videos/20120627/ * Spec: http://produce.corp.miiicasa.com/spec/documents/20120627/ * Mockup: http://produce.corp.miiicasa.com/ued/charming/music_sub_g.png * Mockup: http://produce.corp.miiicasa.com/ued/charming/videos_sub.png * Mockup: http://produce.corp.miiicasa.com/ued/charming/docs_sub_g.png * * @author Vivian Huang * @created 2012/04/30 * @module space * @submodule _folder_info */ YUI.add("space/_folder_info", function (Y) { var _api, _node, _lang, _deviceInfo, // holding deviceInfo _deviceConnector, _editable, // Y.Editable _currentPath, //=========================== // Shortcut //=========================== Lang, MIIICASA = window.MIIICASA || {}, isLan, util, PAGE, currentPath, //=========================== // Constants //=========================== MODULE_ID = "#folder-info", CONST, //=========================== // Private Methods //=========================== _refreshPage, _syncFileInfo, _renameFolder, //=========================== // Public Methods //=========================== init, onviewload; //=========================== // Private Methods //=========================== /** * Refresh Page after editing folder name. * * @method _refreshPage * @private * @param folderName{String} folder name after editing * @return void */ _refreshPage = function (folderName) { _api.log("_refreshPage(folderName) is executed."); var url = Y.substitute(PAGE.baseBackURL, { CURRENT_DIR : encodeURIComponent(folderName) }); window.location.href = url; }; /** * After renaming or deleting folder or files, send the request to delete record in database. * Also, delete shared record. * * @method _syncFileInfo * @private * @param data{Object} The data synchronized with comments and sharing. * @param callback{Function} The callback function. * @return void */ _syncFileInfo = function (data, callback) { _api.log("_syncFileInfo() is executed."); var url = "/space/q/sync_file_info", data = util.composeData(data), callback = callback, cfg; cfg = { method: "POST", data: data, on: { complete: function (id, o, a) { var response = Y.JSON.parse(o.responseText); if (response.status === "ok") { _api.log("_syncFileInfo() is success."); if (callback) { callback(); } } else { _api.log("_syncFileInfo() is failed.", "error", MODULE_ID); } } } }; Y.io(url, cfg); }; /** * Rename album name. * * @method _renameFolder * @private * @param folderName{String} folder name after editing * @return void */ _renameFolder = function (folderName) { var afterRenameFolder, failureHandler, timeoutHandler, attr; _currentPath = currentPath; var baseFolderName = _currentPath.split("/").pop(); afterRenameFolder = function (o) { _api.log("afterRenameFolder() is executed."); if (o.status === "ok") { var syncData, syncCallback; // send a request to server for shared folder rename. syncData = { did : MIIICASA.env.did, act : "rename", src_folderpath : util.dirname(currentPath), filelist : "[\"" + baseFolderName + "\"]", filename : folderName }; syncCallback = function () { _refreshPage(folderName); }; _syncFileInfo(syncData, syncCallback); } else { if (o.errno === 6001) { _api.broadcast("show-access-fail-request"); return; } else if (!Lang.isUndefined(_lang.deviceMessage[o.errno])) { attr = { title : _lang.oops, content : _lang.deviceMessage[o.errno] }; _api.alert(attr); } else { attr = { title : _lang.oops, content : _lang.deviceMessage["5001"] }; _api.alert(attr); } _editable.hide(); return; } }; failureHandler = function () { _api.log("failureHandler() is executed.", "error"); }; timeoutHandler = function () { _api.log("timeoutHandler() is executed.", "error"); }; // currentPath = "/USB_JetFlash_Transcend_16GB_XAZBOVM_1/miiiCasa_Music/Vivian 2012"; _deviceConnector.renameFile({ src_fullfilename : _currentPath, dst_filename : folderName }, { on: { success: afterRenameFolder, failure: failureHandler, timeout: timeoutHandler }, timeout: CONST.CLIENT_TIMEOUT }); }; //=========================== // Public Methods //=========================== /** * Module initialization. * * @event init * @param api {Y.Sandbox} Module API * @public * @return void */ init = function (sandbox) { _api = sandbox; _api.log("init() is executed."); _lang = { oops : _api.getTrans("oops", "Oops"), ok : _api.getTrans("ok_button", "Ok"), cancel : _api.getTrans("cancel_button", "Cancel") }; _api.broadcast("get-device-message", { callback : function (msg) { _lang.deviceMessage = msg; _lang.accessFailDesc = msg["6001"]; _lang.noStorageDesc = msg["5002"]; } }); }; /** * Module content ready. * * @method onviewload * @public * @return void */ onviewload = function () { _api.log("onviewload() is executed."); _node = _api.getViewNode(); Lang = Y.Lang; _deviceInfo = Y.MiiiCasa.deviceInfo; CONST = Y.MiiiCasa.CONST; util = Y.MiiiCasa.util; isLan = Y.MiiiCasa.isLan; PAGE = MIIICASA.PAGE.folderInfo; currentPath = PAGE.currentPath; // create device connector instance and performs getFileList _deviceConnector = new Y.DeviceConnector({ wsip : isLan ? _deviceInfo.wsip : CONST.API_URL, did : isLan ? null : PAGE.did, isLan : isLan, tok : _deviceInfo.token }); if (Y.MiiiCasa.isShare) { return; } // Edit folder name. var mediaType = MIIICASA.env.phrases.miiicasa_media, tooltip = ""; if (mediaType === "miiiCasa_Music") { tooltip = _api.getTrans("edit_album_tooltip", "Rename Album"); } else if (mediaType === "miiiCasa_Videos" || mediaType === "miiiCasa_Documents") { tooltip = _api.getTrans("edit_folder_tooltip", "Rename Folder"); } _editable = new Y.Editable({ "selector" : ".folder-name-editable", "node" : _node, "emptyDefault" : tooltip, "tooltip" : tooltip, "validateRule" : "required|max_length[70]|filename", "sync" : true }); _editable.on("submit", function (e) { _renameFolder(e.details[0].value); e.preventDefault(); }); }; _api = new Y.Module({ selector: MODULE_ID, langModule: "space", init: init, on: { viewload: onviewload } }); }, "0.0.1", { "group" : "space", "js" : "space/_folder_info.js", "css" : "space/_folder_info.css", "requires": [ "module", "module-intl", "module-popup", "editable", "substitute", "io-base", "space", "space/_device_message", "device-connector", "node-base" ] });