{"size": 3138, "ext": "js", "max_stars_repo_path": "app/lib/cosmos/js/services/utils.js", "max_stars_repo_name": "mmrobbin/myproject", "max_stars_repo_stars_event_min_datetime": "2017-07-29T01:29:00.000Z", "max_stars_repo_stars_event_max_datetime": "2017-07-29T01:29:00.000Z", "max_issues_repo_path": "app/lib/cosmos/js/services/utils.js", "max_issues_repo_name": "mmrobbin/myproject", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/lib/cosmos/js/services/utils.js", "max_forks_repo_name": "mmrobbin/myproject", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 36.0689655172, "max_line_length": 96, "alphanum_fraction": 0.4305289994}
/**
 * Created by maruf on 12/14/14.
 */


services.factory('cosmos.utils', ['CosmosService', 'cosmos.cachedloader', 'cosmos.settings',
    function (CosmosService, cachedloader, settings) {
    return{
        siteWideCounter : 0,
        setAppAsDefault: function (app, onSuccess, onError) {

            if (app && app.id) {
                var uri = "/service/cosmos.globalsettings/";
                CosmosService.get(uri, function (data) {
                        if (data && data.length > 0) {
                            var gs = data[0];
                            uri = uri + gs._id + '/';
                            var putdata = {"defaultappid": app.id};
                            CosmosService.put(uri, putdata, onSuccess, onError);
                        }
                        else {
                            //global settings not found - create one
                            var putdata = {"defaultappid": app.id};
                            CosmosService.post(uri, putdata, onSuccess, onError);
                        }
                    },
                    onError);
            }
            else {
                if (onError) {
                    onError(404, "Application not found");
                }
            }
        },

        setPageAsDefault: function (app, page, onSuccess, onError) {
            if (app && app._id) {
                var uri = "/service/cosmos.applications/"+app._id;
                CosmosService.get(uri, function (data) {
                        if (data) {
                            var loadedApp = data;
                            loadedApp.settings.indexPageId = page._id;
                            var putdata = { "settings" : loadedApp.settings};
                            CosmosService.put(uri, putdata, onSuccess, onError);
                        }
                        else {
                            onError(404, "Application not found");
                        }
                    },
                    onError);
            }
            else {
                if (onError) {
                    onError(404, "Application not found");
                }
            }
        },

        getAllPages: function (app, successCallback, errorCallback) {
            var appCache = "Page._Cosmos_All_Pages_";
            var appUrl = '/service/' + app.settings.objectmap.pageconfigobject + '/';
            cachedloader.get(appCache, appUrl,
                function (pages) {
                    successCallback(pages);
                },
                function (data, status) {
                    errorCallback(data, status);
                }
            );
        },

        getNextValue: function(value){
            if(value){
                return value +1;
            }
            this.siteWideCounter +=1;
            return this.siteWideCounter;
        },

        getCapchaSiteKey: function(){
            var globalSettings = cachedloader.getFromCache("Settings._Cosmos_Global_Settings_");
            if(globalSettings){
                return globalSettings[0]["recapchasitekey"];
            }
        }
    }
}]);