Add form field for FontAwesome icon
authorMatthias Schmidt <gravatronics@live.com>
Sun, 24 Jun 2018 07:13:47 +0000 (09:13 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sun, 24 Jun 2018 07:13:47 +0000 (09:13 +0200)
See #2509

wcfsetup/install/files/acp/templates/__iconFormField.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/form/builder/field/IconFormField.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/acp/templates/__iconFormField.tpl b/wcfsetup/install/files/acp/templates/__iconFormField.tpl
new file mode 100644 (file)
index 0000000..033ea6b
--- /dev/null
@@ -0,0 +1,28 @@
+{include file='__formFieldHeader'}
+
+<span{if $field->getValue()} class="icon icon64 fa-{$field->getValue()}"{/if} id="{@$field->getPrefixedId()}_icon"></span>
+<a href="#" class="button small" id="{@$field->getPrefixedId()}_openIconDialog">{lang}wcf.global.button.edit{/lang}</a>
+<input type="hidden" id="{@$field->getPrefixedId()}" name="{@$field->getPrefixedId()}" value="{$field->getValue()}">
+
+{if $__iconFormFieldIncludeJavaScript}
+       {include file='fontAwesomeJavaScript'}
+{/if}
+
+<script data-relocate="true">
+       require(['WoltLabSuite/Core/Ui/Style/FontAwesome'], function(UiStyleFontAwesome) {
+               var button = elById('{@$field->getPrefixedId()}_openIconDialog');
+               var icon = elById('{@$field->getPrefixedId()}_icon');
+               var input = elById('{@$field->getPrefixedId()}');
+               
+               var callback = function(iconName) {
+                       icon.className = 'icon icon64 fa-' + iconName;
+                       input.value = iconName;
+               };
+               
+               button.addEventListener('click', function() {
+                       UiStyleFontAwesome.open(callback);
+               });
+       });
+</script>
+
+{include file='__formFieldFooter'}
diff --git a/wcfsetup/install/files/lib/system/form/builder/field/IconFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/IconFormField.class.php
new file mode 100644 (file)
index 0000000..9d528cd
--- /dev/null
@@ -0,0 +1,91 @@
+<?php
+namespace wcf\system\form\builder\field;
+use wcf\system\form\builder\field\validation\FormFieldValidationError;
+use wcf\system\style\StyleHandler;
+
+/**
+ * Implementation of a form field for to select a FontAwesome icon.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2018 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Form\Builder\Field
+ * @since      3.2
+ */
+class IconFormField extends AbstractFormField {
+       /**
+        * @inheritDoc
+        */
+       protected $templateName = '__iconFormField';
+       
+       /**
+        * `true` if the global icon-related JavaScript code has already been included
+        * and `false` otherwise
+        * @var bool
+        */
+       protected static $includeJavaScript = true;
+       
+       /**
+        * @inheritDoc
+        */
+       public function getHtmlVariables(): array {
+               $value = static::$includeJavaScript;
+               if (static::$includeJavaScript) {
+                       static::$includeJavaScript = false;
+               }
+               
+               return [
+                       '__iconFormFieldIncludeJavaScript' => $value
+               ];
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function getSaveValue() {
+               if ($this->getValue()) {
+                       return 'fa-' . $this->getValue();
+               }
+               
+               return '';
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function readValue(): IFormField {
+               if ($this->getDocument()->hasRequestData($this->getPrefixedId())) {
+                       $this->__value = $this->getDocument()->getRequestData($this->getPrefixedId());
+               }
+               
+               return $this;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function validate() {
+               parent::validate();
+               
+               if (!$this->getValue()) {
+                       if ($this->isRequired()) {
+                               $this->addValidationError(new FormFieldValidationError('empty'));
+                       }
+               }
+               else if (!in_array($this->getValue(), StyleHandler::getInstance()->getIcons())) {
+                       $this->addValidationError(new FormFieldValidationError(
+                               'invalidValue',
+                               'wcf.global.form.error.noValidSelection'
+                       ));
+               }
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function value($value): IFormField {
+               $value = preg_replace('~^fa-~', '', $value);
+               
+               return parent::value($value);
+       }
+}