‘use strict’;
/**
* @ngdoc service
* @name interceptcms.config
* @description
* # config
* Factory in the interceptcms.
*/
var stateModelDataService = function ($state, dataService, modelService, RESOURCES) {
var setConfig = function(query, params) {
var config = {};
config.searchField = query.field || 'tag';
config.criteria = decodeURIComponent(params.id) || "";
config.skip = query.skip || 0;
config.limit = query.limit || RESOURCES.LIMIT;
config.dir = query.dir || 'asc';
config.sort = {};
config.query = {};
if (config.query.sort) {
config.sort[config.query.sort] = config.dir;
} else {
config.sort[primaryField] = config.dir;
}
return config;
};
var isUpdate = function (array) {
return (array.indexOf(RESOURCES.EDIT) > -1) ? true : false;
};
var isCreate = function (array) {
return (array.indexOf(RESOURCES.CREATE) > -1) ? true : false;
};
return function (callback, dataFiltered, setScopeValue) {
var urlArray = [];
// CONTROLLER VARIABLES
// DYNAMIC URL
if ($state.params.model) {
urlArray.push($state.params.model);
if ($state.params.view) {
urlArray.push($state.params.view);
}
if ($state.params.id) {
urlArray.push($state.params.id);
}
} else {
// EXPLICIT URL
urlArray = $state.current.name.split('/');
}
// SET UP CONFIG
// DEFINE MODEL / ISCREATE / ISUPDATE / ID / BASEURL
var qs = window.location.href.split('?')[1];
var config = {};
config.model = {};
config.model._modelName = urlArray[0];
config.isCreate = isCreate(urlArray);
config.isUpdate = isUpdate(urlArray);
config.id = ($state.params.id) ? "/" + $state.params.id : "";
config.baseUrl = RESOURCES.API_URL + urlArray[0];
config.search = ($state.params.search) ? "/search/" + $state.params.search : "";
config.qs = (qs) ? "?" + qs : "";
if (config.search === "") {
config.qs = config.qs.replace("&dir=", " ");
}
// REQUEST AND RETURN MODEL DEFINITION
// PASSED TO CONTROLLER FUNCTION setScopeValue
if (config.model._modelName !== "") {
modelService.requestModel(config.model._modelName, setScopeValue);
}
// RETURN CONFIG TO CONTROLLER / CALLBACK
callback(config);
// RETURN DATA TO DATAFILTERED FOR OPTIONAL LOCAL FILTERING
if (!config.isCreate && config.model._modelName !== "") {
dataService.getData(config.baseUrl + config.id + config.search + config.qs, config.model, dataFiltered);
}
};
};
angular.module(‘interceptcms’)
.factory(‘stateModelDataService’, stateModelDataService);