@zenginehq/frontend-config
Advanced tools
Comparing version 3.1.0 to 3.2.0
{ | ||
"name": "@zenginehq/frontend-config", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Helper module for implementing Zengine plugin configuration forms.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -5,123 +5,129 @@ plugin.service('wgnConfigInputs', [function () { | ||
var _internalInputTypes = [ | ||
{ | ||
type: 'form', | ||
template: 'wgn-config-input-form', | ||
options: { | ||
exclusive: { | ||
required: false | ||
} | ||
var formInput = { | ||
type: 'form', | ||
template: 'wgn-config-input-form', | ||
options: { | ||
exclusive: { | ||
required: false | ||
} | ||
}, | ||
{ | ||
type: 'field', | ||
options: { | ||
belongsTo: { | ||
required: true | ||
}, | ||
restrict: { | ||
required: false | ||
}, | ||
exclusive: { | ||
required: false | ||
} | ||
} | ||
}; | ||
var fieldInput = { | ||
type: 'field', | ||
options: { | ||
belongsTo: { | ||
required: true | ||
}, | ||
template: 'wgn-config-input-field' | ||
}, | ||
{ | ||
type: 'folder', | ||
options: { | ||
belongsTo: { | ||
required: true | ||
}, | ||
exclusive: { | ||
required: false | ||
} | ||
restrict: { | ||
required: false | ||
}, | ||
template: 'wgn-config-input-folder' | ||
exclusive: { | ||
required: false | ||
} | ||
}, | ||
{ | ||
type: 'text', | ||
options: { | ||
placeholder: { | ||
required: false | ||
} | ||
template: 'wgn-config-input-field' | ||
}; | ||
var folderInput = { | ||
type: 'folder', | ||
options: { | ||
belongsTo: { | ||
required: true | ||
}, | ||
template: 'wgn-config-input-text' | ||
exclusive: { | ||
required: false | ||
} | ||
}, | ||
{ | ||
type: 'number', | ||
options: { | ||
placeholder: { | ||
required: false | ||
} | ||
}, | ||
template: 'wgn-config-input-number' | ||
template: 'wgn-config-input-folder' | ||
}; | ||
var textInput = { | ||
type: 'text', | ||
options: { | ||
placeholder: { | ||
required: false | ||
} | ||
}, | ||
{ | ||
type: 'textarea', | ||
template: 'wgn-config-input-textarea' | ||
template: 'wgn-config-input-text' | ||
}; | ||
var numberInput = { | ||
type: 'number', | ||
options: { | ||
placeholder: { | ||
required: false | ||
} | ||
}, | ||
{ | ||
type: 'select', | ||
template: 'wgn-config-input-number' | ||
}; | ||
var textareaInput = { | ||
type: 'textarea', | ||
template: 'wgn-config-input-textarea' | ||
}; | ||
var dropdownInput = { | ||
type: 'dropdown', | ||
options: { | ||
options: { | ||
options: { | ||
required: true, | ||
validate: function (opts) { | ||
if (!Array.isArray(opts)) { | ||
required: true, | ||
validate: function (opts) { | ||
if (!Array.isArray(opts)) { | ||
return false; | ||
} | ||
var valid = true; | ||
angular.forEach(opts, function (o) { | ||
if (!angular.isObject(o)) { | ||
return false; | ||
} | ||
var valid = true; | ||
if (!('value' in o) || !('label' in o)) { | ||
return false; | ||
} | ||
}); | ||
angular.forEach(opts, function (o) { | ||
if (!angular.isObject(o)) { | ||
return false; | ||
} | ||
return valid; | ||
} | ||
} | ||
}, | ||
template: 'wgn-config-input-dropdown' | ||
}; | ||
if (!('value' in o) || !('label' in o)) { | ||
return false; | ||
} | ||
}); | ||
var markupInput = { | ||
type: 'markup', | ||
options: { | ||
value: { | ||
required: true | ||
} | ||
}, | ||
template: 'wgn-config-input-markup' | ||
}; | ||
return valid; | ||
} | ||
var choiceInput = { | ||
type: 'choice', | ||
options: { | ||
belongsTo: { | ||
required: true | ||
}, | ||
mode: { | ||
required: true, | ||
validate: function (m) { | ||
return m === 'select' || m === 'score'; | ||
} | ||
}, | ||
template: 'wgn-config-input-select' | ||
}, | ||
{ | ||
type: 'markup', | ||
options: { | ||
value: { | ||
required: true | ||
restrict: { | ||
required: false, | ||
validate: function (r) { | ||
return checkAllowedItems(r, ['radio', 'checkbox', 'dropdown']); | ||
} | ||
}, | ||
template: 'wgn-config-input-markup' | ||
exclusive: { | ||
required: false | ||
} | ||
}, | ||
{ | ||
type: 'choice', | ||
options: { | ||
belongsTo: { | ||
required: true | ||
}, | ||
mode: { | ||
required: true, | ||
validate: function (m) { | ||
return m === 'select' || m === 'score'; | ||
} | ||
}, | ||
restrict: { | ||
required: false, | ||
validate: function (r) { | ||
return checkAllowedItems(r, ['radio', 'checkbox', 'dropdown']); | ||
} | ||
}, | ||
exclusive: { | ||
required: false | ||
} | ||
}, | ||
template: 'wgn-config-input-choice' | ||
} | ||
]; | ||
template: 'wgn-config-input-choice' | ||
}; | ||
@@ -134,3 +140,13 @@ /** | ||
srv.all = function () { | ||
return _internalInputTypes; | ||
return [ | ||
formInput, | ||
fieldInput, | ||
folderInput, | ||
textInput, | ||
numberInput, | ||
textareaInput, | ||
dropdownInput, | ||
markupInput, | ||
choiceInput | ||
]; | ||
}; | ||
@@ -137,0 +153,0 @@ |
@@ -103,5 +103,3 @@ plugin.controller('wgnConfigCtrl', ['$scope', '$q', '$routeParams', 'znData', 'znModal', 'znMessage', 'wgnConfigSrv', | ||
'No': { | ||
primary: true, | ||
action: function () { | ||
} | ||
primary: true | ||
}, | ||
@@ -141,2 +139,16 @@ 'Yes': { | ||
if ($scope.settings.toggle && !$scope.editing.config.enabled && !('$id' in $scope.editing.config)) { | ||
znModal({ | ||
title: '', | ||
template: '<p>Your new configuration was saved!</p><p>It must be manually enabled to be active.</p>', | ||
classes: 'config-enable-message', | ||
closeButton: false, | ||
btns: { | ||
'OK': { | ||
primary: true | ||
} | ||
} | ||
}); | ||
} | ||
if ($scope.settings.multi) { | ||
@@ -240,17 +252,14 @@ doDiscardChanges(); | ||
* | ||
* @param {string} formField | ||
* @param {Object} formDef | ||
* @param {string} fieldDefId The field definition id. | ||
*/ | ||
$scope.onSelectForm = function (formField, formDef) { | ||
$scope.onSelectForm = function (fieldDefId) { | ||
/*jshint maxcomplexity:6 */ | ||
if (formField) { | ||
var formId = $scope.editing.config[formField]; | ||
var formId = $scope.editing.config[fieldDefId]; | ||
if (formId && (!(formId in _fields) || !_fields[formId].length)) { | ||
loadFields(formId, formDef); | ||
} | ||
if (formId && (!(formId in _fields) || !_fields[formId].length)) { | ||
loadFields(formId, fieldDefId); | ||
} | ||
if (formId && (!(formId in _folders) || !_folders[formId].length)) { | ||
loadFolders(formId); | ||
} | ||
if (formId && (!(formId in _folders) || !_folders[formId].length)) { | ||
loadFolders(formId); | ||
} | ||
@@ -262,12 +271,11 @@ }; | ||
* | ||
* @param {string} formField | ||
* @param {Object} formDef | ||
* @param {string} fieldDefId The field definition id. | ||
*/ | ||
$scope.initFormField = function (formField, formDef) { | ||
$scope.initFormField = function (fieldDefId) { | ||
if ($scope.loading) { | ||
$scope.$on('wgnConfigInit', function () { | ||
$scope.onSelectForm(formField, formDef); | ||
$scope.options.on('init', function () { | ||
$scope.onSelectForm(fieldDefId); | ||
}); | ||
} else { | ||
$scope.onSelectForm(formField, formDef); | ||
$scope.onSelectForm(fieldDefId); | ||
} | ||
@@ -318,3 +326,3 @@ }; | ||
return getFiltered(fieldDef, formDef, _fields).filter(function (f) { | ||
if (!fieldDef.restrict) { | ||
if (!('restrict' in fieldDef) || !fieldDef.restrict) { | ||
return true; | ||
@@ -401,3 +409,3 @@ } | ||
if (data && angular.isObject(data)) { | ||
angular.extend($scope.editing.config, data); | ||
angular.extend(config, data); | ||
} | ||
@@ -412,3 +420,3 @@ | ||
if (data && angular.isObject(data)) { | ||
angular.extend($scope.editing.config, data); | ||
angular.extend(config, data); | ||
} | ||
@@ -438,11 +446,15 @@ | ||
if (!Object.keys(source).length) { | ||
return []; | ||
} | ||
var filters = []; | ||
// Filter values used in other folder inputs. | ||
// Filter values used in other inputs of the same type. | ||
if (formDef) { | ||
angular.forEach(formDef.fields, function (f) { | ||
// Split into multiple if statements for legibility. | ||
if (f.type === fieldDef.type && f.id !== fieldDef.id && f.type === 'choice') { | ||
if (f.type === fieldDef.type && f.id !== fieldDef.id) { | ||
if (f.exclusive && $scope.editing.config) { | ||
if (f.id + '_source' in $scope.editing.config && $scope.editing.config[f.id + '_source']) { | ||
if (f.type === 'choice' && f.id + '_source' in $scope.editing.config && $scope.editing.config[f.id + '_source']) { | ||
filters.push($scope.editing.config[f.id + '_source']); | ||
@@ -492,5 +504,5 @@ } else if (f.id in $scope.editing.config && $scope.editing.config[f.id]) { | ||
* @param {number} formId The actual form id. | ||
* @param {Object} formDef The page this form belongs to. | ||
* @param {string} fieldDefId The field definition id. | ||
*/ | ||
function loadFields (formId, formDef) { | ||
function loadFields (formId, fieldDefId) { | ||
_fieldsLoading[formId] = true; | ||
@@ -500,6 +512,10 @@ | ||
var fieldTypes = []; | ||
var params = { | ||
formId: formId, | ||
limit: 200, | ||
}; | ||
angular.forEach(formDef.fields, function (field) { | ||
if (field.restrict) { | ||
var res = field.restrict.split('|'); | ||
angular.forEach($scope.options.getDependentFields(fieldDefId), function (f) { | ||
if (f.restrict) { | ||
var res = f.restrict.split('|'); | ||
@@ -514,7 +530,7 @@ angular.forEach(res, function (r) { | ||
return znData('FormFields').query({ | ||
formId: formId, | ||
type: fieldTypes.join('|'), | ||
limit: 200 | ||
}).then(function (results) { | ||
if (fieldTypes.length) { | ||
params.type = fieldTypes.join('|'); | ||
} | ||
return znData('FormFields').query(params).then(function (results) { | ||
_fields[formId] = []; | ||
@@ -678,9 +694,9 @@ | ||
$scope.configs = configs; | ||
if (!$scope.settings.multi) { | ||
$scope.editing.config = $scope.configs; | ||
_originalConfig = angular.copy($scope.configs); | ||
$scope.editing.config = configs; | ||
_originalConfig = angular.copy(configs); | ||
} | ||
$scope.configs = configs; | ||
doRunHook('init', $scope.configs).finally(function () { | ||
@@ -687,0 +703,0 @@ def.resolve(); |
@@ -242,12 +242,7 @@ plugin.service('wgnConfigSettings', ['$q', 'wgnConfigInputs', function ($q, configInputs) { | ||
// @TODO we really want to support multiple callbacks per hook but it can get messy so let's put it on ice. | ||
if (!(event in _hooks)) { | ||
// _hooks[event] = []; | ||
_hooks[event] = cb; | ||
} else { | ||
throw new Error('Config: Only a single listener can subscribe to the event "' + event + '".'); | ||
_hooks[event] = []; | ||
} | ||
// _hooks[event].push(cb); | ||
_hooks[event].push(cb); | ||
return srv; | ||
@@ -257,3 +252,3 @@ }; | ||
/** | ||
* Invokes a hook by executing registered callbacks. | ||
* Invokes a hook by executing registered callbacks in sequence. | ||
* | ||
@@ -264,3 +259,7 @@ * @param {string} event | ||
if (event in _hooks) { | ||
return $q.when(_hooks[event](angular.copy(data))); | ||
return _hooks[event].reduce(function (promise, item, index) { | ||
return promise.then(function(result) { | ||
return $q.when(_hooks[event][index](angular.copy(result))); | ||
}); | ||
}, $q.when(data)); | ||
} | ||
@@ -358,2 +357,23 @@ | ||
/** | ||
* Returns field definitions that belong to a certain form. | ||
* | ||
* @param {string} formDefId | ||
* | ||
* @return {Array<Object>} | ||
*/ | ||
srv.getDependentFields = function (formDefId) { | ||
var fields = []; | ||
angular.forEach(_settings.pages, function (p) { | ||
angular.forEach(p.fields, function (f) { | ||
if ('belongsTo' in f && f.belongsTo === formDefId) { | ||
fields.push(f); | ||
} | ||
}); | ||
}); | ||
return fields; | ||
}; | ||
/** | ||
* Returns a config settings object. | ||
@@ -360,0 +380,0 @@ * This is the final product of this service. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
144915
1351