From 6a3615bd39e9dde7ee5a9564096a95599da52435 Mon Sep 17 00:00:00 2001 From: Lindsey DiLoreto Date: Sat, 2 Jul 2016 17:25:07 -0700 Subject: [PATCH 1/2] Added new "Triggers" button type --- .../ButtonBox_TriggersFieldType.php | 198 ++++++++++++++++++ buttonbox/resources/js/settings-triggers.js | 32 +++ buttonbox/templates/triggers/field.html | 28 +++ 3 files changed, 258 insertions(+) create mode 100644 buttonbox/fieldtypes/ButtonBox_TriggersFieldType.php create mode 100644 buttonbox/resources/js/settings-triggers.js create mode 100644 buttonbox/templates/triggers/field.html diff --git a/buttonbox/fieldtypes/ButtonBox_TriggersFieldType.php b/buttonbox/fieldtypes/ButtonBox_TriggersFieldType.php new file mode 100644 index 0000000..1993a1a --- /dev/null +++ b/buttonbox/fieldtypes/ButtonBox_TriggersFieldType.php @@ -0,0 +1,198 @@ +getSettings(); + $options = $this->getTranslatedOptions(); + + // If this is a new entry, look for a default option + if ($this->isFresh()) + { + $value = $this->getDefaultValue(); + } + + craft()->templates->includeCssResource('buttonbox/css/buttonbox.css'); + craft()->templates->includeJsResource('buttonbox/js/buttonbox.js'); + craft()->templates->includeJs('new Craft.ButtonBoxButtons("'.craft()->templates->namespaceInputId($name).'");'); + + return craft()->templates->render('buttonbox/triggers/field', array( + 'name' => $name, + 'value' => $value, + 'options' => $options, + 'displayAsGraphic' => $settings->displayAsGraphic, + 'displayFullwidth' => $settings->displayFullwidth + )); + } + + /** + * @inheritDoc BaseElementFieldType::getSettingsHtml() + * + * @return string|null + */ + public function getSettingsHtml() + { + + craft()->templates->includeJsResource('buttonbox/js/settings-triggers.js'); + + $settings = $this->getSettings(); + $options = $this->getOptions(); + + if (!$options) + { + // Give it a default row + $options = array( + array( + 'label' => '', + 'showLabel' => false, + 'imageUrl' => '', + 'type' => '', + 'link' => '', + 'newWindow' => false, + ) + ); + } + + $table = craft()->templates->renderMacro('_includes/forms', 'editableTableField', array( + array( + 'label' => $this->getOptionsSettingsLabel(), + 'instructions' => Craft::t('Image urls can be relative e.g. /admin/resources/buttonbox/images/align-left.png'), + 'id' => 'options', + 'name' => 'options', + 'addRowLabel' => Craft::t('Add a trigger'), + 'cols' => array( + 'label' => array( + 'heading' => Craft::t('Option Label'), + 'type' => 'singleline' + ), + 'showLabel' => array( + 'heading' => Craft::t('Show Label?'), + 'type' => 'checkbox', + 'class' => 'thin' + ), + 'imageUrl' => array( + 'heading' => Craft::t('Image URL'), + 'type' => 'singleline' + ), + 'type' => array( + 'heading' => Craft::t('Trigger Type'), + 'class' => 'thin triggerType', + 'type' => 'select', + 'options' => array( + 'link' => 'Link', + 'js' => 'JavaScript' + ), + ), + 'value' => array( + 'heading' => Craft::t('HREF or Custom JS'), + 'type' => 'singleline', + 'class' => 'code triggerValue' + ), + 'newWindow' => array( + 'heading' => Craft::t('New window?'), + 'type' => 'checkbox', + 'class' => 'thin newWindow' + ), + ), + 'rows' => $options + ) + )); + + $displayAsGraphic = craft()->templates->renderMacro('_includes/forms', 'checkboxField', array( + array( + 'label' => Craft::t('Display as graphic'), + 'instructions' => Craft::t('This will take the height restrictions off the buttons to allow for larger images.'), + 'id' => 'displayAsGraphic', + 'name' => 'displayAsGraphic', + 'class' => 'displayAsGraphic', + 'value' => 1, + 'checked' => $settings->displayAsGraphic + ) + )); + + $displayFullwidth = craft()->templates->renderMacro('_includes/forms', 'checkboxField', array( + array( + 'label' => Craft::t('Display full width'), + 'instructions' => Craft::t('Allow the button group to be fullwidth, useful for allowing larger graphics to be more responsive.'), + 'id' => 'displayFullwidth', + 'name' => 'displayFullwidth', + 'class' => 'displayFullwidth', + 'value' => 1, + 'checked' => $settings->displayFullwidth + ) + )); + + return $displayAsGraphic . $displayFullwidth . $table; + } + + // Protected Methods + // ========================================================================= + + /** + * @inheritDoc BaseOptionsFieldType::getOptionsSettingsLabel() + * + * @return string + */ + protected function getOptionsSettingsLabel() + { + return Craft::t('Trigger Buttons'); + } + + /** + * @inheritDoc BaseSavableComponentType::defineSettings() + * + * @return array + */ + protected function defineSettings() + { + return array_merge(parent::defineSettings(), array( + 'displayAsGraphic' => AttributeType::Bool, + 'displayFullwidth' => AttributeType::Bool, + 'options' => array(AttributeType::Mixed, 'default' => array()) + )); + } +} diff --git a/buttonbox/resources/js/settings-triggers.js b/buttonbox/resources/js/settings-triggers.js new file mode 100644 index 0000000..5cdfe23 --- /dev/null +++ b/buttonbox/resources/js/settings-triggers.js @@ -0,0 +1,32 @@ + +// Configure all triggers, based on type +function configureTriggers() { + var $select, $checkbox, $textarea; + $('#types-ButtonBox_Triggers-options tbody tr').each(function (i, tr) { + // Get row elements + $select = $(tr).find('td.triggerType select'); + $checkbox = $(tr).find('td.newWindow input'); + $textarea = $(tr).find('td.triggerValue textarea'); + // Configure row + if ('js' == $select.val()) { + $checkbox.hide(); + $textarea.attr('placeholder', 'myFunction();'); + } else { + $checkbox.show(); + $textarea.attr('placeholder', 'http://www.example.com'); + } + }); +} + +// On page load +$(function () { + configureTriggers(); + // When a trigger type changes + $('#types-ButtonBox_Triggers-options').on('change', 'td.triggerType select', function () { + configureTriggers(); + }); + // When a new row is added + $('#types-ButtonBox_Triggers-options-field div.btn.add.icon').on('click', function () { + configureTriggers(); + }); +}); \ No newline at end of file diff --git a/buttonbox/templates/triggers/field.html b/buttonbox/templates/triggers/field.html new file mode 100644 index 0000000..147ad3b --- /dev/null +++ b/buttonbox/templates/triggers/field.html @@ -0,0 +1,28 @@ +{%- set options = (options is defined ? options : []) %} +{%- set value = (value is defined ? value : null) %} + +
+ + {% for key, option in options %} + + {% set optionLabel = (option.label is defined ? option.label : option) %} + {% set optionValue = (option.value is defined ? option.value : '#') %} + {% set optionType = (option.type is defined ? option.type : 'link') %} + {% set optionShowLabel = (option.showLabel is defined ? option.showLabel : true) %} + {% set optionNewWindow = (option.newWindow is defined ? option.newWindow : false) %} + + {% set imageAndLabel %} + {% if option.imageUrl %}{% endif %} + {% if option.imageUrl and optionShowLabel %} {% endif %} + {{ optionShowLabel ? optionLabel }} + {% endset %} + + {% if 'js' == optionType %} + + {% else %} + {{ imageAndLabel|raw }} + {% endif %} + + {% endfor %} + +
From 5ba2e58550d1a66a1b06d1a82ea36210d5d9da2c Mon Sep 17 00:00:00 2001 From: Lindsey DiLoreto Date: Tue, 9 Aug 2016 16:49:50 -0700 Subject: [PATCH 2/2] Parse element tags in links --- buttonbox/fieldtypes/ButtonBox_TriggersFieldType.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/buttonbox/fieldtypes/ButtonBox_TriggersFieldType.php b/buttonbox/fieldtypes/ButtonBox_TriggersFieldType.php index 1993a1a..6177114 100644 --- a/buttonbox/fieldtypes/ButtonBox_TriggersFieldType.php +++ b/buttonbox/fieldtypes/ButtonBox_TriggersFieldType.php @@ -60,6 +60,11 @@ public function getInputHtml($name, $value) craft()->templates->includeJsResource('buttonbox/js/buttonbox.js'); craft()->templates->includeJs('new Craft.ButtonBoxButtons("'.craft()->templates->namespaceInputId($name).'");'); + // Parse element tags in links + foreach ($options as $i => $opt) { + $options[$i]['value'] = craft()->templates->renderObjectTemplate($opt['value'], $this->element); + } + return craft()->templates->render('buttonbox/triggers/field', array( 'name' => $name, 'value' => $value,