Skip to content

Commit

Permalink
FIX: Fetch jQuery from a CDN since it can't come from admin (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnysideup authored Oct 6, 2023
1 parent d1032f3 commit 55a10fd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 45 deletions.
41 changes: 21 additions & 20 deletions src/Fields/KeyValueField.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,17 @@ public function __construct($name, $title = null, $sourceKeys = [], $sourceValue

public function Field($properties = [])
{
if (Controller::curr() instanceof ContentController) {
Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js');
if (Controller::has_curr()
&& (Controller::curr() instanceof ContentController)
&& MultiValueTextField::config()->get('output_jquery_on_frontend')
) {
Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js');
}
Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js');
Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css');

$nameKey = $this->name.'[key][]';
$nameVal = $this->name.'[val][]';
$nameKey = $this->name . '[key][]';
$nameVal = $this->name . '[val][]';
$fields = [];
$keyFieldPlaceholder = $this->getKeyFieldPlaceholder();
$valueFieldPlaceholder = $this->getValueFieldPlaceholder();
Expand All @@ -96,20 +99,20 @@ public function Field($properties = [])
foreach ($this->value as $i => $v) {
if ($this->readonly) {
$fieldAttr = [
'class' => 'mventryfield mvkeyvalReadonly '.($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id().MultiValueTextField::KEY_SEP.$i,
'class' => 'mventryfield mvkeyvalReadonly ' . ($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id() . MultiValueTextField::KEY_SEP . $i,
'name' => $nameKey,
'tabindex' => $this->getAttribute('tabindex')
];

$keyField = HTML::createTag('span', $fieldAttr, Convert::raw2xml($i));
$fieldAttr['id'] = $this->id().MultiValueTextField::KEY_SEP.$v;
$fieldAttr['id'] = $this->id() . MultiValueTextField::KEY_SEP . $v;
$valField = HTML::createTag('span', $fieldAttr, Convert::raw2xml($v));
$fields[] = $keyField.$valField;
$fields[] = $keyField . $valField;
} else {
$keyField = $this->createSelectList($i, $nameKey, $this->sourceKeys, $i, $keyFieldPlaceholder);
$valField = $this->createSelectList($i, $nameVal, $this->sourceValues, $v, $valueFieldPlaceholder);
$fields[] = $keyField.' '.$valField;
$fields[] = $keyField . ' ' . $valField;
}
}
} else {
Expand All @@ -119,14 +122,12 @@ public function Field($properties = [])
if (!$this->readonly) {
$keyField = $this->createSelectList('new', $nameKey, $this->sourceKeys, '', $keyFieldPlaceholder);
$valField = $this->createSelectList('new', $nameVal, $this->sourceValues, '', $valueFieldPlaceholder);
$fields[] = $keyField.' '.$valField;
// $fields[] = $this->createSelectList('new', $name, $this->source);
$fields[] = $keyField . ' ' . $valField;
// $fields[] = $this->createSelectList('new', $name, $this->source);
}

return '<ul id="'.$this->id().'" class="multivaluefieldlist mvkeyvallist '.$this->extraClass().'"><li>'.implode(
'</li><li>',
$fields
).'</li></ul>';
return '<ul id="' . $this->id() . '" class="multivaluefieldlist mvkeyvallist ' . $this->extraClass() . '"><li>'
. implode('</li><li>', $fields) . '</li></ul>';
}

protected function createSelectList($number, $name, $values, $selected = '', $placeholder = '')
Expand All @@ -136,7 +137,7 @@ protected function createSelectList($number, $name, $values, $selected = '', $pl
[
'selected' => $selected == '' ? 'selected' : '',
'value' => ''
],
],
''
);

Expand All @@ -150,8 +151,8 @@ protected function createSelectList($number, $name, $values, $selected = '', $pl

if (count($values ?? [])) {
$attrs = [
'class' => 'text mventryfield mvdropdown '.($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id().MultiValueTextField::KEY_SEP.$number,
'class' => 'text mventryfield mvdropdown ' . ($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id() . MultiValueTextField::KEY_SEP . $number,
'name' => $name,
'tabindex' => $this->getAttribute('tabindex')
];
Expand All @@ -163,8 +164,8 @@ protected function createSelectList($number, $name, $values, $selected = '', $pl
return HTML::createTag('select', $attrs, $options);
} else {
$attrs = [
'class' => 'text mventryfield mvtextfield '.($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id().MultiValueTextField::KEY_SEP.$number,
'class' => 'text mventryfield mvtextfield ' . ($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id() . MultiValueTextField::KEY_SEP . $number,
'value' => $selected,
'name' => $name,
'tabindex' => $this->getAttribute('tabindex'),
Expand Down
24 changes: 14 additions & 10 deletions src/Fields/MultiValueDropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,26 @@ public function setSource(array $source)

public function Field($properties = [])
{
if (Controller::curr() instanceof ContentController) {
Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js');
if (Controller::has_curr()
&& (Controller::curr() instanceof ContentController)
&& MultiValueTextField::config()->get('output_jquery_on_frontend')
) {
Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js');
}
Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js');
Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css');

$name = $this->name.'[]';
$name = $this->name . '[]';
$fields = [];


if ($this->value) {
foreach ($this->value as $i => $v) {
if ($this->readonly) {
$fieldAttr = [
'class' => 'mventryfield mvdropdownReadonly '.($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id().MultiValueTextField::KEY_SEP.$i,
'class' => 'mventryfield mvdropdownReadonly '
. ($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id() . MultiValueTextField::KEY_SEP . $i,
'name' => $name,
'tabindex' => $this->getAttribute('tabindex')
];
Expand All @@ -77,10 +81,10 @@ public function Field($properties = [])
$fields[] = $this->createSelectList($i + 1, $name, $this->source);
}

return '<ul id="'.$this->id().'" class="multivaluefieldlist '.$this->extraClass().'"><li>'.implode(
return '<ul id="' . $this->id() . '" class="multivaluefieldlist ' . $this->extraClass() . '"><li>' . implode(
'</li><li>',
$fields
).'</li></ul>';
) . '</li></ul>';
}

public function Type()
Expand All @@ -95,7 +99,7 @@ protected function createSelectList($number, $name, $values, $selected = '')
[
'selected' => $selected == '' ? 'selected' : '',
'value' => ''
],
],
''
);

Expand All @@ -108,8 +112,8 @@ protected function createSelectList($number, $name, $values, $selected = '')
}

$attrs = [
'class' => 'mventryfield mvdropdown '.($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id().MultiValueTextField::KEY_SEP.$number,
'class' => 'mventryfield mvdropdown ' . ($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id() . MultiValueTextField::KEY_SEP . $number,
'name' => $name,
'tabindex' => $this->getAttribute('tabindex')
];
Expand Down
11 changes: 7 additions & 4 deletions src/Fields/MultiValueListField.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ public function __construct($name, $title = null, $source = [], $value = null)

public function Field($properties = [])
{
if (Controller::curr() instanceof ContentController) {
Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js');
if (Controller::has_curr()
&& (Controller::curr() instanceof ContentController)
&& MultiValueTextField::config()->get('output_jquery_on_frontend')
) {
Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js');
}
Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js');
Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css');

$name = $this->name.'[]';
$name = $this->name . '[]';

$options = '';
if (!$this->value) {
Expand All @@ -49,7 +52,7 @@ public function Field($properties = [])
}

$attrs = [
'class' => 'mventryfield mvlistbox '.($this->extraClass() ? $this->extraClass() : ''),
'class' => 'mventryfield mvlistbox ' . ($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id(),
'name' => $name,
'tabindex' => $this->getAttribute('tabindex'),
Expand Down
29 changes: 18 additions & 11 deletions src/Fields/MultiValueTextField.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,29 @@ class MultiValueTextField extends FormField

protected $tag = 'input';

/**
* Determines whether jQuery should be added to the frontend via a CDN.
* Set this to false if you already output your own jQuery.
*/
private static bool $output_jquery_on_frontend = true;

public function Field($properties = [])
{
if (Controller::curr() instanceof ContentController) {
Requirements::javascript('silverstripe/admin: thirdparty/jquery/jquery.js');
if (Controller::has_curr()
&& (Controller::curr() instanceof ContentController)
&& self::config()->get('output_jquery_on_frontend')
) {
Requirements::javascript('https://code.jquery.com/jquery-3.6.3.min.js');
}
Requirements::javascript('symbiote/silverstripe-multivaluefield: client/javascript/multivaluefield.js');
Requirements::css('symbiote/silverstripe-multivaluefield: client/css/multivaluefield.css');

$name = $this->name.'[]';
$name = $this->name . '[]';
$fields = [];

$attributes = [
'type' => 'text',
'class' => 'text mvtextfield mventryfield '.($this->extraClass() ? $this->extraClass() : ''),
'class' => 'text mvtextfield mventryfield ' . ($this->extraClass() ? $this->extraClass() : ''),
// 'id' => $this->id(),
'name' => $name,
// 'value' => $this->Value(),
Expand All @@ -48,7 +57,7 @@ public function Field($properties = [])
$fieldAttr = $attributes;
if ($this->value) {
foreach ($this->value as $i => $v) {
$fieldAttr['id'] = $this->id().MultiValueTextField::KEY_SEP.$i;
$fieldAttr['id'] = $this->id() . MultiValueTextField::KEY_SEP . $i;
$fieldAttr['value'] = $v;
if ($this->readonly) {
unset($fieldAttr['value']);
Expand All @@ -62,17 +71,15 @@ public function Field($properties = [])
// add an empty row
if (!$this->readonly) {
// assume next pos equals to the number of existing fields which gives index+1 in a zero-indexed list
$attributes['id'] = $this->id().MultiValueTextField::KEY_SEP.count($fields ?? []);
$attributes['id'] = $this->id() . MultiValueTextField::KEY_SEP . count($fields ?? []);
$fields[] = $this->createInput($attributes);
}

if (count($fields ?? [])) {
return '<ul id="'.$this->id().'" class="multivaluefieldlist '.$this->extraClass().'"><li>'.implode(
'</li><li>',
$fields
).'</li></ul>';
return '<ul id="' . $this->id() . '" class="multivaluefieldlist ' . $this->extraClass() . '"><li>'
. implode('</li><li>', $fields) . '</li></ul>';
} else {
return '<div id="'.$this->id().'" class="multivaluefieldlist '.$this->extraClass().'"></div>';
return '<div id="' . $this->id() . '" class="multivaluefieldlist ' . $this->extraClass() . '"></div>';
}
}

Expand Down

0 comments on commit 55a10fd

Please sign in to comment.