/**
*
* Copyright (C) 2021 Akorbi Digital RMP
*
* This file is part of RunMyProcess SDK-JS.
*
* RunMyProcess SDK-JS is free software: you can redistribute it and/or modify
* it under the terms of 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.
*/
/**
Create a new instance of I18N
@constructor
@property {Array} links - Contains the array of links
@property {JSON} designerDico - Object containing the standard library structure
@property {Array} dicos - Contains the array of applications
@property {String} dicoUrl - Contains the Url of the dico(Internal Use)
@property {String} createUrl - Contains the Url for the creation of dicos(Internal Use)
@see ProjectApp
@see Resource
@example Load i18n from app
function loadI18n(p_app){//App object
var i18n = p_app.i18n;
i18n.load({
onSuccess:function(){
var dico = i18n.dicos[0];
alert(i18n.dicos.length);
i18n.load({
onSuccess:function(){
alert("Dico "+dico.name+" loaded!");
}
});
}
});
};
@example Load designer Dico from process
function loadi18n(p){
var i18n = p.i18n;
i18n.load({
onSuccess:function(){
i18n.loadDesignerDico({
onSuccess:function(){
alert(JSON.stringify(i18n.designerDico));
}
});
}
});
};
*/
function I18n(){
this.links;
this.designerDico;
this.dicos = [];
this.dicoUrl;
this.createUrl;
};
/**
@borrows
Inherits Resource
*/
I18n.prototype = new Resource();
/**
Overrides Resource's loadPreset method.
@method
@see Resource#loadPreset
*/
I18n.prototype.loadPreset = function () {
/**
Overrides Resource's generateUrl method to return the request url
@method
@see Resource#generateUrl
*/
this.generateUrl = function () {
return this.selfUrl;
};
};
/**
Overrides Resource's loadSet method to set local variables after request.
@method
@param {json} rObject - JSON representation of the loaded data.
@see Resource#loadSet
*/
I18n.prototype.loadSet = function (rObject) {
this.createUrl = rObject.links.create;
};
/**
Override Resource load method.
@method
@param {object} options - options to be used during the call<br/>
@param {Resource~onSuccess} options.onSuccess - a callback function called in case of a success
@param {Resource~onFailure} [options.onFailure] - a callback function called in case of a failure
@param {String} [options.baseUrl] - base URL. If not set the current base URL will be used
@see Resource#load
*/
I18n.prototype.load = function(options){
var loadOptions = {};
var father = this;
loadOptions.onSuccess = function(rObject){
father.dicos = [];
var json = rObject;
delete json.links;
if (json){
for (key in json) {
if (json.hasOwnProperty(key)) {
var dico = new I18nDico();
dico.language = json[key].language;
dico.name = json[key].name;
dico.dicoJson = json[key];
dico.selfUrl = json[key].links.self;
father.dicos.push(dico);
}
}
}
options.onSuccess(rObject);
};
loadOptions.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
loadOptions.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
father.ajaxLoad(loadOptions);
};
/**
Load designerDico.
@method
@param {object} options - options to be used during the call<br/>
@param {Resource~onSuccess} options.onSuccess - a callback function called in case of a success
@param {Resource~onFailure} [options.onFailure] - a callback function called in case of a failure
@param {String} [options.baseUrl] - base URL. If not set the current base URL will be used
@see Resource#ajaxLoad
*/
I18n.prototype.loadDesignerDico = function (options) {
var father = this;
var i18nDico = new I18nDico();
i18nDico.selfUrl = father.dicoUrl || father.links.dico;
var loadOptions = {};
loadOptions.onSuccess = function(rObject){
father.designerDico = rObject;
father.createUrl = rObject.links.create;
options.onSuccess(rObject);
};
loadOptions.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
loadOptions.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
i18nDico.ajaxLoad(loadOptions);
};
/**
Create a new dictionary.
@method
@param {object} options - options to be used during the call<br/>
@param {String} options.language - dictionary title
@param {String} options.name - dictionary name
@param {Resource~onSuccess} options.onSuccess - a callback function called in case of a success
@param {Resource~onFailure} [options.onFailure] - a callback function called in case of a failure
@param {String} [options.baseUrl] - base URL. If not set the current base URL will be used
@see updateI18n
@example Create i18n library
function createI18nLib(p_app){
var i18n = p_app.i18n;
i18n.load({
onSuccess:function(){
i18n.createLib({
onSuccess:function(){
alert("Lib "+optLib.language+" created!");
}
});
}
});
};
*/
I18n.prototype.createDico = function (options) {
var father = this;
if (options.dico){
var dicoJson=options.dico;
dicoJson.language=options.language;//changes the language of the dico
dicoJson.name = options.name;//changes the name of the dico
father.createUrl = father.createUrl+options.language;
father.dico = dicoJson;
var optI18n = {};
optI18n.onSuccess = function(rObject){
options.onSuccess(rObject);
};
optI18n.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
optI18n.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
father.updateI18n(optI18n);//updates i18n to set the new library
}else{
var optDico = {};
optDico.onSuccess = function(rObject){
var dicoJson=father.dico;
dicoJson.language=options.language;//changes the language of the dico
dicoJson.name = options.name;//changes the name of the dico
father.dico = dicoJson;
var optI18n = {};
optI18n.onSuccess = options.onSuccess;
optI18n.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
optI18n.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
father.updateI18n(optI18n);//updates i18n to set the new library
};
optDico.onFailure = function(e){
options.eObject=e;
father.errorManager(options);
};
optDico.baseUrl = options.baseUrl || RMPApplication.getBaseUrl();
father.loadDesignerDico(optDico);
}
};
/**
update a i18n with previously set values.
@method
@param {object} options - options to be used during the call<br/>
@param {Resource~onSuccess} options.onSuccess - a callback function called in case of a success
@param {Resource~onFailure} [options.onFailure] - a callback function called in case of a failure
@param {String} [options.baseUrl] - base URL. If not set the current base URL will be used
@see Resource#save
*/
I18n.prototype.updateI18n = function (options) {//Verify Update functionality!
var father = this;
var v_dico=father.dico;
if (v_dico){
/**
Overrides Resource's savePreset method.
@method
@see Resource#savePreset
*/
father.savePreset = function () {
/**
Overrides Resource's generateUrl method to return the save url
@method
@see Resource#generateUrl
*/
father.generateUrl = function () {
return father.createUrl;
};
father.contentType = 'json';
father.xml = JSON.stringify(v_dico);
};
var optSave = {};
father.save(options);
};
};