0930e2a52d8a3ac882277fd64569b6e7745dce0d
[GitHub/WoltLab/WCF.git] /
1 /**
2 * Manages the packages entered in a devtools project required package form field.
3 *
4 * @author Matthias Schmidt
5 * @copyright 2001-2019 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @module WoltLabSuite/Core/Form/Acp/Builder/Field/Devtools/Project/RequiredPackages
8 * @see module:WoltLabSuite/Core/Acp/Form/Builder/Field/Devtools/Project/AbstractPackageList
9 * @since 5.2
10 */
11 define(['./AbstractPackageList', 'Core', 'Language'], function(AbstractPackageList, Core, Language) {
12 "use strict";
13
14 /**
15 * @constructor
16 */
17 function RequiredPackages(formFieldId, existingPackages) {
18 this.init(formFieldId, existingPackages);
19 };
20 Core.inherit(RequiredPackages, AbstractPackageList, {
21 /**
22 * @see WoltLabSuite/Core/Form/Builder/Field/Devtools/Project/AbstractPackageList#init
23 */
24 init: function(formFieldId, existingPackages) {
25 RequiredPackages._super.prototype.init.call(this, formFieldId, existingPackages);
26
27 this._minVersion = elById(this._formFieldId + '_minVersion');
28 if (this._minVersion === null) {
29 throw new Error("Cannot find minimum version form field for packages field with id '" + this._formFieldId + "'.");
30 }
31 this._minVersion.addEventListener('keypress', this._keyPress.bind(this));
32
33 this._file = elById(this._formFieldId + '_file');
34 if (this._file === null) {
35 throw new Error("Cannot find file form field for required field with id '" + this._formFieldId + "'.");
36 }
37 },
38
39 /**
40 * @see WoltLabSuite/Core/Form/Builder/Field/Devtools/Project/AbstractPackageList#_createSubmitFields
41 */
42 _createSubmitFields: function(listElement, index) {
43 RequiredPackages._super.prototype._createSubmitFields.call(this, listElement, index);
44
45 var minVersion = elCreate('input');
46 elAttr(minVersion, 'type', 'hidden');
47 elAttr(minVersion, 'name', this._formFieldId + '[' + index + '][minVersion]')
48 minVersion.value = elData(listElement, 'min-version');
49 this._form.appendChild(minVersion);
50
51 var file = elCreate('input');
52 elAttr(file, 'type', 'hidden');
53 elAttr(file, 'name', this._formFieldId + '[' + index + '][file]')
54 file.value = elData(listElement, 'file');
55 this._form.appendChild(file);
56 },
57
58 /**
59 * @see WoltLabSuite/Core/Form/Builder/Field/Devtools/Project/AbstractPackageList#_emptyInput
60 */
61 _emptyInput: function() {
62 RequiredPackages._super.prototype._emptyInput.call(this);
63
64 this._minVersion.value = '';
65 this._file.checked = false;
66 },
67
68 /**
69 * @see WoltLabSuite/Core/Form/Builder/Field/Devtools/Project/AbstractPackageList#_getInputData
70 */
71 _getInputData: function() {
72 return Core.extend(RequiredPackages._super.prototype._getInputData.call(this), {
73 file: this._file.checked,
74 minVersion: this._minVersion.value
75 });
76 },
77
78 /**
79 * Returns the error element for the minimum version form field.
80 * If `createIfNonExistent` is not given or `false`, `null` is returned
81 * if there is no error element, otherwise an empty error element
82 * is created and returned.
83 *
84 * @param {?boolean} createIfNonExistent
85 * @return {?HTMLElement}
86 */
87 _getMinVersionErrorElement: function(createIfNonExistent) {
88 return this._getErrorElement(this._minVersion, createIfNonExistent);
89 },
90
91 /**
92 * @see WoltLabSuite/Core/Form/Builder/Field/Devtools/Project/AbstractPackageList#_populateListItem
93 */
94 _populateListItem: function(listItem, packageData) {
95 RequiredPackages._super.prototype._populateListItem.call(this, listItem, packageData);
96
97 elData(listItem, 'min-version', packageData.minVersion);
98 elData(listItem, 'file', ~~packageData.file);
99 listItem.innerHTML = ' ' + Language.get('wcf.acp.devtools.project.requiredPackage.requiredPackage', {
100 file: ~~packageData.file,
101 minVersion: packageData.minVersion,
102 packageIdentifier: packageData.packageIdentifier
103 });
104 },
105
106 /**
107 * @see WoltLabSuite/Core/Form/Builder/Field/Devtools/Project/AbstractPackageList#_validateInput
108 */
109 _validateInput: function() {
110 return RequiredPackages._super.prototype._validateInput.call(this) && this._validateVersion(
111 this._minVersion.value,
112 this._getMinVersionErrorElement.bind(this)
113 );
114 }
115 });
116
117 return RequiredPackages;
118 });