From 12bbeeff0f42d75c5e6cfdb1251f5b1a1bda637d Mon Sep 17 00:00:00 2001 From: Donna Wu Date: Fri, 10 Aug 2012 16:29:24 +0800 Subject: [PATCH] [Serialize] Add 'find', 'add', 'remove' sandbox header functions --- src/js/serialize.js | 180 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 147 insertions(+), 33 deletions(-) diff --git a/src/js/serialize.js b/src/js/serialize.js index 5abbbc66..a709b407 100644 --- a/src/js/serialize.js +++ b/src/js/serialize.js @@ -36,6 +36,12 @@ var DEBUG = true, $(function () { + /* Variable definition */ + var headerPropertyMap = { + css: 'css', + js: 'libs' + }; + /** * Generate HTML from ADM tree. * @@ -747,7 +753,139 @@ $(function () { return $.rib.fsUtils.pathToUrl(fullPath); } - /***************** export functions out *********************/ + function indexOfArray(array, value) { + var i, index = -1; + if (typeof value !== 'object') { + return $.inArray(array, value); + } else { + for (i = 0; i < array.length; i++) { + if (JSON.stringify(array[i]) === JSON.stringify(value)) { + index = i; + } + } + return index; + } + } + + /** + * Check if the sandbox file is a design header, such as css, js file. + * + * @param {String} type Type of the specified header. + * @param {String} filePath Sandbox path of header file. + * + * @return {Boolean} Return true if find the file in header list, else false. + * Notes: filePath If the file is in project directory, relative path should + * be used. A path beginning with '/' will be considered as absolut + * path in sandbox. + */ + function isSandboxHeader(type, filePath) { + var property, array, design, index; + + if (!(type && filePath && (filePath.length > 0))) { + dumplog('Error: Invalid parameter(s) in isSandboxHeader.'); + return false; + } + property = headerPropertyMap[type]; + if (!property) { + dumplog('Error: No header:' + type + 'in design.'); + return false; + } + design = ADM.getDesignRoot(); + array = $.merge([], design.getProperty(property)); + index = indexOfArray(array, { + inSandbox: true, + value: filePath + }); + if (index > -1) { + return true; + } else { + return false; + } + } + + /** + * Add sandbox header file in current design, such as css, js file. + * + * @param {String} type Type of the specified header. + * @param {String} filePath Sandbox path of header file. + * + * @return {Boolean} Return true if added the header, else return false. + * + * Notes: filePath If the file is in project directory, relative path should + * be used. A path beginning with '/' will be considered as absolut + * path in sandbox. + */ + function addSandboxHeader(type, filePath) { + var property, array, design, index; + + if (!(type && filePath && (filePath.length > 0))) { + dumplog('Error: Invalid parameter(s) in addSandboxHeader.'); + return false; + } + property = headerPropertyMap[type]; + if (!property) { + dumplog('Error: No header:' + type + 'in design.'); + return false; + } + design = ADM.getDesignRoot(); + array = $.merge([], design.getProperty(property)); + index = indexOfArray(array, { + inSandbox: true, + value: filePath + }); + if (index === -1) { + array.push({ + inSandbox: true, + value: filePath + }); + // set the new array back + design.setProperty(property, array); + } else { + dumplog('warning:"' + filePath +'" is already in ' + type + ' list.'); + } + return true; + } + + /** + * Remove sandbox header file in current design, such as css, js file. + * + * @param {String} type Type of the specified header. + * @param {String} filePath Sandbox path of header file. + * + * @return {Boolean} Return true if deleted the header, else return false. + * + * Notes: filePath If the file is in project directory, relative path should + * be used. A path beginning with '/' will be considered as absolut + * path in sandbox. + */ + function removeSandboxHeader(type, filePath) { + var property, array, design, index; + + if (!(type && filePath && (filePath.length > 0))) { + dumplog('Error: Invalid parameter(s) in removeSandboxHeader.'); + return false; + } + property = headerPropertyMap[type]; + if (!property) { + dumplog('Error: No header:' + type + 'in design.'); + return false; + } + design = ADM.getDesignRoot(); + array = $.merge([], design.getProperty(property)); + index = indexOfArray(array, { + inSandbox: true, + value: filePath + }); + if (index > -1) { + array.splice(index, 1); + // set the new array back + design.setProperty(property, array); + } else { + dumplog('warning: not find:"' + filePath +'" in ' + type + ' list.'); + } + return true; + } + /** * Add custom file to current active project. * It will save the content in project folder. If the parent directy of @@ -761,7 +899,7 @@ $(function () { * * @return {None} */ - $.rib.addCustomFile = function (filePath, type, contents, success, error) { + function addCustomFile(filePath, type, contents, success, error) { var destPath, addToDesign, projectDir; projectDir = $.rib.pmUtils.getProjectDir(); // If it is relative path, then add the project folder path @@ -770,42 +908,14 @@ $(function () { } else { destPath = filePath; } - addToDesign = function (type, value) { - var design, array, property, propertyMap, i, temp; - propertyMap = { - css: 'css', - js: 'libs' - }; - design = ADM.getDesignRoot(); - property = propertyMap[type]; - temp = $.extend(true, {}, { - property: property, - value: design.getProperty(property) - }); - array = temp.value; - for (i = 0; i < array.length; i++) { - // If the value is in headers, then just return. - if (JSON.stringify(array[i]) === JSON.stringify(value)) { - return; - } - } - // If the value is not in array, then push the value in the list - array.push(value); - // set the new array back - design.setProperty(property, array); - return; - }; // Write contents to sandbox $.rib.fsUtils.write(destPath, contents, function (newFile) { - var headerValue = { - 'inSandbox': true, - 'value': filePath - }; - addToDesign(type, headerValue); + addSandboxHeader(type, filePath); success && success(newFile); }, error); - }; + } + /***************** export functions out *********************/ // Export serialization functions into $.rib namespace $.rib.generateHTML = generateHTML; $.rib.serializeADMSubtreeToDOM = serializeADMSubtreeToDOM; @@ -813,4 +923,8 @@ $(function () { $.rib.JSONToProj = JSONToProj; $.rib.getDesignHeaders = getDesignHeaders; $.rib.exportPackage = exportPackage; + $.rib.isSandboxHeader = isSandboxHeader; + $.rib.addSandboxHeader = addSandboxHeader; + $.rib.removeSandboxHeader = removeSandboxHeader; + $.rib.addCustomFile = addCustomFile; });