Skip to content

Commit 2fe841a

Browse files
committed
Extend ext storage params to contain default value
Extend the external storage configuration parameters definition to allow to specify a default value Signed-off-by: Vincent Petry <vincent@nextcloud.com>
1 parent 53f0ee1 commit 2fe841a

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

apps/files_external/js/settings.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,14 @@ MountConfigListView.prototype = _.extend({
10531053
newElement = $('<input type="text" class="'+classes.join(' ')+'" data-parameter="'+parameter+'" placeholder="'+ trimmedPlaceholder+'" />');
10541054
}
10551055

1056+
if (placeholder.defaultValue) {
1057+
if (placeholder.type === MountConfigListView.ParameterTypes.BOOLEAN) {
1058+
newElement.find('input').prop('checked', placeholder.defaultValue);
1059+
} else {
1060+
newElement.val(placeholder.defaultValue);
1061+
}
1062+
}
1063+
10561064
if (placeholder.tooltip) {
10571065
newElement.attr('title', placeholder.tooltip);
10581066
}

apps/files_external/lib/Lib/DefinitionParameter.php

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,45 @@ class DefinitionParameter implements \JsonSerializable {
4343
public const FLAG_USER_PROVIDED = 2;
4444

4545
/** @var string name of parameter */
46-
private $name;
46+
private string $name;
4747

4848
/** @var string human-readable parameter text */
49-
private $text;
49+
private string $text;
5050

5151
/** @var string human-readable parameter tooltip */
52-
private $tooltip = '';
52+
private string $tooltip = '';
5353

5454
/** @var int value type, see self::VALUE_* constants */
55-
private $type = self::VALUE_TEXT;
55+
private int $type = self::VALUE_TEXT;
5656

5757
/** @var int flags, see self::FLAG_* constants */
58-
private $flags = self::FLAG_NONE;
58+
private int $flags = self::FLAG_NONE;
59+
60+
/** @var mixed */
61+
private $defaultValue;
5962

6063
/**
61-
* @param string $name
62-
* @param string $text
64+
* @param string $name parameter name
65+
* @param string $text parameter description
66+
* @param mixed $defaultValue default value
6367
*/
64-
public function __construct($name, $text) {
68+
public function __construct(string $name, string $text, $defaultValue = null) {
6569
$this->name = $name;
6670
$this->text = $text;
71+
$this->defaultValue = $defaultValue;
6772
}
6873

6974
/**
7075
* @return string
7176
*/
72-
public function getName() {
77+
public function getName(): string {
7378
return $this->name;
7479
}
7580

7681
/**
7782
* @return string
7883
*/
79-
public function getText() {
84+
public function getText(): string {
8085
return $this->text;
8186
}
8287

@@ -85,7 +90,7 @@ public function getText() {
8590
*
8691
* @return int
8792
*/
88-
public function getType() {
93+
public function getType(): int {
8994
return $this->type;
9095
}
9196

@@ -95,15 +100,31 @@ public function getType() {
95100
* @param int $type
96101
* @return self
97102
*/
98-
public function setType($type) {
103+
public function setType(int $type) {
99104
$this->type = $type;
100105
return $this;
101106
}
102107

108+
/**
109+
* @return mixed default value
110+
*/
111+
public function getDefaultValue() {
112+
return $this->defaultValue;
113+
}
114+
115+
/**
116+
* @param mixed $defaultValue default value
117+
* @return self
118+
*/
119+
public function setDefaultValue($defaultValue) {
120+
$this->defaultValue = $defaultValue;
121+
return $this;
122+
}
123+
103124
/**
104125
* @return string
105126
*/
106-
public function getTypeName() {
127+
public function getTypeName(): string {
107128
switch ($this->type) {
108129
case self::VALUE_BOOLEAN:
109130
return 'boolean';
@@ -119,15 +140,15 @@ public function getTypeName() {
119140
/**
120141
* @return int
121142
*/
122-
public function getFlags() {
143+
public function getFlags(): int {
123144
return $this->flags;
124145
}
125146

126147
/**
127148
* @param int $flags
128149
* @return self
129150
*/
130-
public function setFlags($flags) {
151+
public function setFlags(int $flags) {
131152
$this->flags = $flags;
132153
return $this;
133154
}
@@ -136,7 +157,7 @@ public function setFlags($flags) {
136157
* @param int $flag
137158
* @return self
138159
*/
139-
public function setFlag($flag) {
160+
public function setFlag(int $flag) {
140161
$this->flags |= $flag;
141162
return $this;
142163
}
@@ -145,7 +166,7 @@ public function setFlag($flag) {
145166
* @param int $flag
146167
* @return bool
147168
*/
148-
public function isFlagSet($flag) {
169+
public function isFlagSet(int $flag): bool {
149170
return (bool)($this->flags & $flag);
150171
}
151172

@@ -169,15 +190,20 @@ public function setTooltip(string $tooltip) {
169190
* Serialize into JSON for client-side JS
170191
*/
171192
public function jsonSerialize(): array {
172-
return [
193+
$result = [
173194
'value' => $this->getText(),
174195
'flags' => $this->getFlags(),
175196
'type' => $this->getType(),
176197
'tooltip' => $this->getTooltip(),
177198
];
199+
$defaultValue = $this->getDefaultValue();
200+
if ($defaultValue) {
201+
$result['defaultValue'] = $defaultValue;
202+
}
203+
return $result;
178204
}
179205

180-
public function isOptional() {
206+
public function isOptional(): bool {
181207
return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED);
182208
}
183209

@@ -188,7 +214,7 @@ public function isOptional() {
188214
* @param mixed $value Value to check
189215
* @return bool success
190216
*/
191-
public function validateValue(&$value) {
217+
public function validateValue(&$value): bool {
192218
switch ($this->getType()) {
193219
case self::VALUE_BOOLEAN:
194220
if (!is_bool($value)) {

apps/files_external/tests/DefinitionParameterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ public function testJsonSerialization() {
3636
], $param->jsonSerialize());
3737

3838
$param->setType(Param::VALUE_BOOLEAN);
39+
$param->setDefaultValue(true);
3940
$this->assertEquals([
4041
'value' => 'bar',
4142
'flags' => 0,
4243
'type' => Param::VALUE_BOOLEAN,
4344
'tooltip' => '',
45+
'defaultValue' => true,
4446
], $param->jsonSerialize());
4547

4648
$param->setType(Param::VALUE_PASSWORD);

0 commit comments

Comments
 (0)