收费的阳光 2012-12-11 14:58
浏览 390
已采纳

var func = func || (function(){ .....}());求答

var jagg = jagg || (function () {
    var ctx = context;

    var modules = {};

    var requirs = {};

    var templates = {};

    var initializers = {};

    var bloks = {};

    var data;

    var setData = function (d) {
        data = d;
    };

    var getData = function () {
        return data;
    };

    var getUser = function () {
        return session.get("logged.user");
    };

    var setUser = function (user) {
        session.put("logged.user", user);
    };


var getThemeFile = function (path) {
        var p, index, theme = getUserTheme();
        if(theme.subtheme) {
        p = getThemePath() + "subthemes/" + theme.subtheme + "/" + path;
        index = p.indexOf("?");
        if(new File(p.substring(0, index == -1 ? p.length : index)).isExists()) {
        return p;
        }
}
return getThemePath() + path;
};

    var getThemesPath = function () {
        return "/site/themes/";
    };

    var getThemePath = function () {
        return getThemesPath() + getUserTheme().base + "/";
    };

    var getBlockFile = function (name) {
        return getBlocksDir() + name + "/block.jag";
    };

    var getInitializerFile = function (name) {
        return getThemeFile("templates/" + name + "/initializer.jag");
    };

    var getTemplateFile = function (name) {
        return getThemeFile("templates/" + name + "/template.jag");
     };

    var getTemplatePath = function(themeDir, name) {
        return themeDir + "templates/" + name + "/template.jag";

    };

    var getModuleFile = function (name) {
        return getModulesDir() + name + "/module.jag";
    };

    var getBlocksDir = function () {
        return "/site/blocks/";
    };

    var getThemesDir = function () {
        return "/site/themes/";
    };

    var getModulesDir = function () {
        return "/modules/";
    };

    var getTheme = function () {
       var site = require("/site/conf/site.json"),
       theme = request.getParameter("theme"),
       subtheme = request.getParameter("subtheme");
       return {
             base : theme ? theme : site.theme.base,
             subtheme : subtheme ? subtheme : site.theme.subtheme
        };
    };

     var getUserTheme = function () {
        return session.get("theme") ? session.get("theme") : getTheme();
     };

    var mergeParams = function (extInputs, defInputs) {
        var key, obj;
        extInputs = extInputs || {};
        for (key in defInputs) {
            if (defInputs.hasOwnProperty(key)) {
                obj = extInputs[key];
                if (!obj) {
                    extInputs[key] = defInputs[key];
                }
            }
        }
        return extInputs;
    };

    var renderBlock = function (name, inputs, outputs, populate) {
        //initializeTemplate({name:name, params:null}, jagg);

        var init, fn, blok, log = new Log();

        fn = template(name);
        if (!fn) {
            log.error("Template header and footer includes are missing for : " + name);
        }
        if (populate) {
            blok = block(name);

            if (!inputs) {
                inputs = blok.getInputs ? blok.getInputs() : {};
            } else {
                mergeParams(inputs, blok.getInputs ? blok.getInputs() : null);
            }

            if (blok.getOutputs) {
                outputs = blok.getOutputs(inputs);
            } else if (blok.getInputs) {
                outputs = inputs;
            } else {
                outputs = {};
            }
        }
        init = initializer(name);
        if (init.postInitialize) {
            init.postInitialize(inputs, outputs);
        }
        fn(inputs, outputs, jagg);
    };

    var inheritParent = function (blok, name) {
        var parent = require(getBlockFile(name));
        for (var prop in parent) {
            if (parent.hasOwnProperty(prop)) {
                if (!blok[prop]) {
                    blok[prop] = parent[prop];
                }
            }
        }
    };

    var initializeBlock = function (obj) {
        if (!obj) {
            return;
        }
        var extInputs, defInputs, parent, tmpl, inputBlocks, outputBlocks, outputs, tmplInitializer, bloks, i, length,
                name = obj.name, blok = block(name), log = new Log();

        template(name);
        extInputs = obj.inputs || (obj.inputs = {});
        defInputs = blok.getInputs ? blok.getInputs() : {};
        mergeParams(extInputs, defInputs);

        if (blok.getInputBlocks) {
            inputBlocks = blok.getInputBlocks();
            length = inputBlocks.length;
            for (i = 0; i < length; i++) {
                initializeBlocks(inputBlocks[i], extInputs);
            }
        }

        if (blok.getOutputs) {
            outputs = blok.getOutputs(extInputs);
        } else if (blok.getInputs) {
            outputs = extInputs;
        } else {
            outputs = {};
        }

        obj.outputs = outputs;
        if (blok.getOutputBlocks) {
            outputBlocks = blok.getOutputBlocks();
            length = outputBlocks.length;
            for (i = 0; i < length; i++) {
                initializeBlocks(outputBlocks[i], outputs);
            }
        }

        if (blok.getStaticBlocks) {
            bloks = blok.getStaticBlocks();
            length = bloks.length;
            for (i = 0; i < length; i++) {
                initializeBlock({name:bloks[i], inputs:null});
            }
        }
    };

    // [ "foo", "bar", "mar"]
    // [{ "name" : "foo/bar", params : {}}]
    var initializeBlocks = function (keys, inputs) {
        if (!inputs) {
            return;
        }
        var i, length, values, last;
        if (typeof keys !== "string") {
            length = keys.length;
            values = inputs[keys[0]];
            last = (length == 1);
            if (values instanceof Array) {
                length = values.length;
                for (i = 0; i < length; i++) {
                    if (last) {
                        initializeBlock(values[i]);
                    } else {
                        initializeBlocks(keys.slice(1), values[i]);
                    }
                }
            } else {
                if (last) {
                    initializeBlock(values);
                } else {
                    initializeBlocks(keys.slice(1), values);
                }
            }
            return;
        } else {
            values = inputs[keys];
        }

        if (values instanceof Array) {
            length = values.length;
            for (i = 0; i < length; i++) {
                initializeBlock(values[i]);
            }
        } else {
            initializeBlock(values);
        }
    };

    var insertData = function (jagg, template, parent, name, key, value) {
        var keys, values, data = getData();
        data = data[parent] || (data[parent] = {});
        data = data[name] || (data[name] = {});
        data = data[template] || (data[template] = {});

        keys = data.keys || (data.keys = []);
        values = data.values || (data.values = {});

        keys.push(key);
        values[key] = value;
    };


    var printData = function (tmpls) {
        var key, tmpl, keys, values, i, length;
        for (key in tmpls) {
            if (tmpls.hasOwnProperty(key)) {
                tmpl = tmpls[key];
                keys = tmpl.keys;
                values = tmpl.values;
                length = keys.length;
                for (i = 0; i < length; i++) {
                    print(values[keys[i]]);
                }
            }
        }
    };

    var getUrlMapping = function (path) {
        var urlMap = ctx.get("url.map"), url, configs, i, length, mapping, mappings, file;
        if (urlMap) {
            url = urlMap[path];
            return url ? url : path;
        }
        file = new File("/jaggery.conf");
        file.open("r");
        configs = parse(file.readAll());
        file.close();

        urlMap = {};
        mappings = configs.urlMappings;
        length = mappings.length;
        for (i = 0; i < length; i++) {
            mapping = mappings[i];
            urlMap[mapping.path] = mapping.url;
        }
        ctx.put("url.map", urlMap);
        url = urlMap[path];
        return url ? url : path;
    };

    var getMappedUrl = function (path) {
        return getAbsoluteUrl(getUrlMapping(path));
    };

    var getAbsoluteUrl = function (path) {
        return site.context + path;
    };

    var module = function (name, module) {
        if (module) {
            return modules[name] = module;
        }
        module = modules[name];
        if (module) {
            return module;
        }
        include(getModuleFile(name));
        return modules[name];
    };

    var requir = function (path) {
        var obj = requirs[path];
        return obj ? obj : requirs[path] = require(path);
    };

    var block = function (name, blok) {
        var parent;
        if (blok) {
            return bloks[name] = blok;
        }
        blok = bloks[name];
        if (blok) {
            return blok;
        }
        //we need to include and initialize
        include(getBlockFile(name));
        blok = bloks[name];
        parent = blok.getParent;
        if (parent) {
            parent = parent();
            inheritParent(blok, parent);
        }
        if (blok.initialize) {
            //TODO which to pass into initialize method
            blok.initialize(getData());
        }
        return bloks[name];
    };

    var template = function (name, tmpl) {
        var blok, parent, init;
        if (tmpl) {
            return templates[name] = tmpl;
        }
        tmpl = templates[name];
        if (tmpl) {
            return tmpl;
        }

        blok = block(name);
        parent = blok.getParent;
        if (parent) {
            name = parent();
        }

        tmpl = templates[name];
        if (tmpl) {
            return tmpl;
        }

        include(getTemplateFile(name));
        init = initializer(name);
        if (init.preInitialize) {
            init.preInitialize();
        }
        return templates[name];
    };

    var initializer = function (name, init) {
        var blok, parent;
        if (init) {
            return initializers[name] = init;
        }
        init = initializers[name];
        if (init) {
            return init;
        }

        blok = block(name);
        parent = blok.getParent;
        if (parent) {
            name = parent();
        }

        init = initializers[name];
        if (init) {
            return init;
        }

        include(getInitializerFile(name));
        return initializers[name];
    };

    var render = function (obj) {
        var init, fn, inputs, outputs, name = obj.name, log = new Log(), blok;
        setData(obj);
        initializeBlock(obj);
        include(getTemplateFile(name));
        fn = template(name);
        if (!fn) {
            log.error("Template header and footer includes are missing for : " + name);
        }
        inputs = obj.inputs;
        blok = block(name);
        if (blok.getOutputs) {
            outputs = blok.getOutputs(inputs);
        } else if (blok.getInputs) {
            outputs = inputs;
        } else {
            outputs = {};
        }
        init = initializer(name);
        if (init.postInitialize) {
            init.postInitialize(inputs, outputs);
        }
        fn(inputs, outputs, jagg);
    };

    var includeBlock = function (name, inputs) {
        renderBlock(name, inputs, null, true);
    };

    var includeBlocks = function (bloks) {
        if (!bloks) {
            return;
        }

        var i, d, length;
        if (bloks instanceof Array) {
            length = bloks.length;
            for (i = 0; i < length; i++) {
                d = bloks[i];
                renderBlock(d.name, d.inputs, d.outputs, false);
            }
        } else {
            renderBlock(bloks.name, bloks.inputs, bloks.outputs, false);
        }
    };

    var addHeaderCSS = function (template, key, css) {
        css = '<link type="text/css" rel="stylesheet" href="' + site.context + getThemeFile(css) + '"/>';
        insertData(this, template, "header", "css", key, css);
    };

    var addHeaderCSSCode = function (template, key, css) {
        css = '<style type="text/css">' + css + '</style>';
        insertData(this, template, "header", "css", key, css);
    };

    var addHeaderJS = function (template, key, js) {
        js = '<script type="text/javascript" src="' + site.context + getThemeFile(js) + '"></script>';
        insertData(this, template, "header", "js", key, js);
    };

    var addHeaderJSCode = function (template, key, js) {
        js = '<script type="text/javascript">' + js + '</script>';
        insertData(this, template, "header", "js", key, js);
    };

    var addHeaderCode = function (template, key, code) {
        insertData(this, template, "header", "code", key, code);
    };

    var addFooterCSS = function (template, key, css) {
        css = '<link type="text/css" rel="stylesheet" href="' + site.context + getThemeFile(css) + '"/>';
        insertData(this, template, "footer", "css", key, css);
    };

    var addFooterCSSCode = function (template, key, css) {
        css = '<style type="text/css">' + css + '</style>';
        insertData(this, template, "footer", "css", key, css);
    };

    var addFooterJS = function (template, key, js) {
        js = '<script type="text/javascript" src="' + site.context + getThemeFile(js) + '"></script>';
        insertData(this, template, "footer", "js", key, js);
    };

    var addFooterJSCode = function (template, key, js) {
        js = '<script type="text/javascript">' + js + '</script>';
        insertData(this, template, "footer", "js", key, js);
    };

    var addFooterCode = function (template, key, code) {
        insertData(this, template, "footer", "code", key, code);
    };

    var includeJag = function (path) {
        include(getThemeFile(path));
    };

    return {
        setUser:setUser,
        getUser:getUser,
        block:block,
        module:module,
        initializer:initializer,
        includeBlock:includeBlock,
        includeBlocks:includeBlocks,
        render:render,
        template:template,
        require:requir,
        getAbsoluteUrl:getAbsoluteUrl,
        getMappedUrl:getMappedUrl,
        printData:printData,
        getModulesDir:getModulesDir,
        data:getData,
        addHeaderCSS:addHeaderCSS,
        addHeaderCSSCode:addHeaderCSSCode,
        addHeaderJS:addHeaderJS,
        addHeaderJSCode:addHeaderJSCode,
        addHeaderCode:addHeaderCode,
        addFooterCSS:addFooterCSS,
        addFooterCSSCode:addFooterCSSCode,
        addFooterJS:addFooterJS,
        addFooterJSCode:addFooterJSCode,
        addFooterCode:addFooterCode,
        includeJag:includeJag,
        getUserTheme:getUserTheme,
        getThemeFile:getThemeFile
    };

}());

这个表达式是什么意思??我就没看懂这高人想干什么?特别是中间的这个 || ,变量本身和一个function取||是什么意思??

 

  • 写回答

3条回答 默认 最新

  • jinnianshilongnian 2012-12-11 15:07
    关注

    1、特别是中间的这个 || ,变量本身和一个function取||是什么意思??
    function a(extInputs ) {
    extInputs = extInputs || {};
    alert(extInputs );
    }

       a();
    

    表示是如果extInputs是undefined 则 取{} 做为extInputs的值

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。