<commit_before>/*
 * Rapid Interface Builder (RIB) - A simple WYSIWYG HTML5 app creator
 * Copyright (c) 2011-2012, Intel Corporation.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 */
"use strict";

/**
 * Global object to access project management.
 *
 */
$(function () {
    var fsUtils = $.rib.fsUtils,
    pmUtils = {
        _projectsInfo: {},
        _lastPid: 0,
        _activeProject: null,
        designDirty:false,
        pInfoDirty:false,
        allTags: [],
        ProjectDir: "/projects",
        pidPrefix: "p",
        propertySchema: {
            name: {
                type: "string",
                defaultValue: "Untitled"
            },
            accessDate: {
                type: "number",
                defaultValue:""
            },
            device: {
                type: "object",
                defaultValue: {
                    name: "Phones",
                    screenWidth: 320,
                    screenHeight: 480
                }
            },
            thumbnail: {
                type: "string",
                defaultValue: ""
            }
        }
    };

    /* Asynchronous. init pmUtils.
     * ls the root/projects dir to get all metadata about projects,
     * if the dir does not exist, create it.
     * @param {function()=} success Success callback.
     * @param {function(FileError)=} error Error callback.
     *
     * @return {None}.
     */
    pmUtils.init = function (success, error) {
        var successReadData, errorCreateDir, onEnd,
            dirCount = 0,
            brokenList = [],
            fineList = [];

        onEnd = function () {
            var allHandled;
            allHandled = brokenList.length + fineList.length;
            if (allHandled === dirCount) {
                $.rib.pmUtils.showLastOpened(success, error);
            }
        };
        // success handler for reading projects directory
        successReadData = function (entries) {
            dirCount = entries.length;
            // return if there is no project
            if (dirCount === 0) {
                onEnd();
                return;
            }
            // get Max Pid from entries
            $.each(entries, function (i, e) {
                var num, pid;
                pid = e.name;
                // init _lastPid
                num = pid.substr(pmUtils.pidPrefix.length);
                num = parseInt(num);
                if (pmUtils._lastPid < num) {
                    pmUtils._lastPid = num;
                }
            });
            // fill all projects info into pmUtils._projectsInfo
            $.each(entries, function(index, e) {
                fsUtils.read(e.fullPath + "/pInfo.json", function (text) {
                    var dataObject = $.parseJSON(text);
                    // use the name of project folder as key, it's also PID
                    pmUtils._projectsInfo[e.name] = dataObject;
                    fineList.push(e.name);
                    onEnd();
                }, function () {
                    brokenList.push(e.name);
                    onEnd();
                    console.error("Can't get design file for project:",e.name);
                    // Remove the broken project directory
                    // TODO: may try to restore the project in the future
                    if (e.isDirectory) {
                        e.removeRecursively(function () {
                            console.warn("Broken project removed:",e.name);
                        });
                    }
                });
            });
            return;
        };
        errorCreateDir = function (e) {
            if (e.code === FileError.NOT_FOUND_ERR) {
                // Create a Untitled project and open it in onEnd function
                fsUtils.mkdir(pmUtils.ProjectDir, onEnd, error);
            } else {
                console.error(e.code);
                error && error();
            }
        };
        fsUtils.ls(pmUtils.ProjectDir, successReadData, errorCreateDir);
    }

    /***************** APIs to manipulate projects *************************/
    /* Get the acitve project.
     *
     * @return {String} The pid of current acitve project, or null if none.
     */
    pmUtils.getActive = function () {
        return pmUtils._activeProject;
    };

    /**
     * Get properties of a specified project.
     *
     * @param {String} pid Project id.
     * @return {Object} Object which contains all project properties,
     *                  or null if failed.
     */
    pmUtils.getProperties = function (pid) {
        var pInfo;
        pid && (pInfo = pmUtils._projectsInfo[pid]);
        if (!(pid && pInfo)) {
            console.error("Invalid pid in getProperties, pid:" + pid);
            return null;
        }
        return $.extend(true, {}, pInfo);
    };

    /**
     * Get the schema of a specified property.
     *
     * @param {String} property Name of specified property.
     * @return {Object} Object of property schema, or null if failed.
     */
    pmUtils.getPropertySchema = function (property) {
        var schema = pmUtils.propertySchema[property];<commit_msg>[General] Fix typos<commit_after>/*
 * Rapid Interface Builder (RIB) - A simple WYSIWYG HTML5 app creator
 * Copyright (c) 2011-2012, Intel Corporation.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 */
"use strict";

/**
 * Global object to access project management.
 *
 */
$(function () {
    var fsUtils = $.rib.fsUtils,
    pmUtils = {
        _projectsInfo: {},
        _lastPid: 0,
        _activeProject: null,
        designDirty:false,
        pInfoDirty:false,
        allTags: [],
        ProjectDir: "/projects",
        pidPrefix: "p",
        propertySchema: {
            name: {
                type: "string",
                defaultValue: "Untitled"
            },
            accessDate: {
                type: "number",
                defaultValue:""
            },
            device: {
                type: "object",
                defaultValue: {
                    name: "Phones",
                    screenWidth: 320,
                    screenHeight: 480
                }
            },
            thumbnail: {
                type: "string",
                defaultValue: ""
            }
        }
    };

    /* Asynchronous. init pmUtils.
     * ls the root/projects dir to get all metadata about projects,
     * if the dir does not exist, create it.
     * @param {function()=} success Success callback.
     * @param {function(FileError)=} error Error callback.
     *
     * @return {None}.
     */
    pmUtils.init = function (success, error) {
        var successReadData, errorCreateDir, onEnd,
            dirCount = 0,
            brokenList = [],
            fineList = [];

        onEnd = function () {
            var allHandled;
            allHandled = brokenList.length + fineList.length;
            if (allHandled === dirCount) {
                $.rib.pmUtils.showLastOpened(success, error);
            }
        };
        // success handler for reading projects directory
        successReadData = function (entries) {
            dirCount = entries.length;
            // return if there is no project
            if (dirCount === 0) {
                onEnd();
                return;
            }
            // get Max Pid from entries
            $.each(entries, function (i, e) {
                var num, pid;
                pid = e.name;
                // init _lastPid
                num = pid.substr(pmUtils.pidPrefix.length);
                num = parseInt(num);
                if (pmUtils._lastPid < num) {
                    pmUtils._lastPid = num;
                }
            });
            // fill all projects info into pmUtils._projectsInfo
            $.each(entries, function(index, e) {
                fsUtils.read(e.fullPath + "/pInfo.json", function (text) {
                    var dataObject = $.parseJSON(text);
                    // use the name of project folder as key, it's also PID
                    pmUtils._projectsInfo[e.name] = dataObject;
                    fineList.push(e.name);
                    onEnd();
                }, function () {
                    brokenList.push(e.name);
                    onEnd();
                    console.error("Can't get design file for project:",e.name);
                    // Remove the broken project directory
                    // TODO: may try to restore the project in the future
                    if (e.isDirectory) {
                        e.removeRecursively(function () {
                            console.warn("Broken project removed:",e.name);
                        });
                    }
                });
            });
            return;
        };
        errorCreateDir = function (e) {
            if (e.code === FileError.NOT_FOUND_ERR) {
                // Create a Untitled project and open it in onEnd function
                fsUtils.mkdir(pmUtils.ProjectDir, onEnd, error);
            } else {
                console.error(e.code);
                error && error();
            }
        };
        fsUtils.ls(pmUtils.ProjectDir, successReadData, errorCreateDir);
    }

    /***************** APIs to manipulate projects *************************/
    /* Get the active project.
     *
     * @return {String} The pid of current active project, or null if none.
     */
    pmUtils.getActive = function () {
        return pmUtils._activeProject;
    };

    /**
     * Get properties of a specified project.
     *
     * @param {String} pid Project id.
     * @return {Object} Object which contains all project properties,
     *                  or null if failed.
     */
    pmUtils.getProperties = function (pid) {
        var pInfo;
        pid && (pInfo = pmUtils._projectsInfo[pid]);
        if (!(pid && pInfo)) {
            console.error("Invalid pid in getProperties, pid:" + pid);
            return null;
        }
        return $.extend(true, {}, pInfo);
    };

    /**
     * Get the schema of a specified property.
     *
     * @param {String} property Name of specified property.
     * @return {Object} Object of property schema, or null if failed.
     */
    pmUtils.getPropertySchema = function (property) {
        var schema = pmUtils.propertySchema[property];
{'fraction_non_alphanumeric': 0.11065188803306406, 'fraction_numerical': 0.004133007702423445, 'mean_word_length': 1.386684599865501, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3580112', 'n_tokens_mistral': 2808, 'n_tokens_neox': 2658, 'n_words': 763}