/** * Yobi, Project Hosting SW * * Copyright 2013 NAVER Corp. * http://yobi.io * * @author Jihan Kim * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ window.yobi = (typeof yobi == "undefined") ? {} : yobi; $yobi = yobi.Common = (function(){ var htVar = { "sScriptPath":"", "rxTrim": /\s+/g }; var htModuleInstance = {}; /** * set JavaScript asset path for loadScript * @param {String} sPath */ function setScriptPath(sPath){ htVar.sScriptPath = sPath; } /** * Create namespace object from String * @param {String} sName namespace string like 'yobi.module.Name' * @returns {Hash Table} container object and last name of argument * @example * var oNS = createNamespace("yobi.module.Name"); * oNS.container[oNS.name] = { ... }; * // oNS.container === yobi.module * // oNS.name === "Name" */ function createNamespace(sNamespace) { var aSpace = sNamespace.split("."); var oParent = window; var sObjectName = null; for ( var i = 0, len = aSpace.length; i < len; i++) { sObjectName = aSpace[i]; if (i == (len - 1)) { break; } if (typeof oParent[sObjectName] !== "object") { oParent[sObjectName] = {}; } oParent = oParent[sObjectName]; } return { "container" : oParent, "name" : sObjectName }; } /** * load module * @param {String} sName * @param {Hash Table} htOptions * @param {Function} fCallback */ function loadModule(sName, htOptions, fCallback){ htOptions = htOptions || {}; if(registerModule(sName, htOptions) === false){ htVar.htTryLoad = htVar.htTryLoad || {}; htVar.htTryLoad[sName] = (typeof htVar.htTryLoad[sName] == "undefined") ? 1 : (++htVar.htTryLoad[sName]); if(htVar.htTryLoad[sName] > 3){ console.log("[Yobi] fail to load module " + sName); return false; } var sURL = htVar.sScriptPath + "service/yobi." + sName + ".js"; var fOnLoad = function(){ loadModule(sName, htOptions, fCallback); }; return loadScript(sURL, fOnLoad); } if(typeof fCallback == "function"){ fCallback(htOptions); } } /** * register module * @param {String} sName * @param {Hash Table} htOptions */ function registerModule(sName, htOptions){ var aNames = sName.split("."); var sDepth = aNames.shift(); var oModule = yobi[sDepth]; while(aNames.length && oModule){ sDepth = aNames.shift(); oModule = oModule[sDepth]; } $("[data-toggle=popover]").popover(); // temporary code for compatibility with nForge var oInstance; if(typeof oModule == "undefined"){ return false; } else if(typeof oModule == "function"){ oInstance = new oModule(htOptions); } else if(typeof oModule == "object"){ oInstance = oModule; oInstance.init(); } return htModuleInstance[sName] = oInstance; } /** * load JavaScript * @param {String} sURL * @param {Function} fCallback callback function on load */ function loadScript(sURL, fCallback){ var elScript = document.createElement("script"); elScript.type = "text/javascript"; elScript.async = true; elScript.src = sURL; // run callback and free memory on load var fOnLoad = function(){ if(typeof fCallback == "function"){ fCallback(); } document.body.removeChild(elScript); elScript = fOnLoad = null; }; // attach onLoad event handler if(elScript.addEventListener) { // for FF elScript.addEventListener("load", fOnLoad, false); } else if(typeof elScript.onload == "undefined"){ elScript.onreadystatechange = function(){ // for IE if(this.readyState === "complete" || this.readyState === "loaded"){ fOnLoad(); } }; } else { // and for other polite browsers elScript.onload = fOnLoad; } document.body.appendChild(elScript); } /** * stop Event * @param {Event} eEvt */ function stopEvent(eEvt) { if(!eEvt){ return; } eEvt.cancelBubble = true; eEvt.returnValue = false; if (eEvt.stopPropagation) { eEvt.stopPropagation(); } if (eEvt.preventDefault) { eEvt.preventDefault(); } } /** * Compute a color contrasted with the given color (lightness). * See http://en.wikipedia.org/wiki/Luma_(video) * @param {String} sColor * @returns {String} * @example dimgray if yellow is given. */ function getContrastColor(sColor){ var oRGB = new RGBColor(sColor); var y709 = (oRGB.r * 0.21) + (oRGB.g * 0.72) + (oRGB.b * 0.07); return (y709 > 192) ? 'dimgray' : 'white'; } /** * Send a request using $.ajaxForm * @param {Hash Table} htOptions * @param {String} htOptions.sURL