From b3cd28ee14ab87e232e341be0f64ef225c2e06a8 Mon Sep 17 00:00:00 2001 From: Mats Mikkel Rummelhoff Date: Wed, 6 Jul 2016 14:36:53 +0200 Subject: [PATCH] Adds support for Solspace Calendar, and relational fieldtypes as toggle fields. Bump to 1.0.5 --- README.md | 47 +++++++++--- reasons/ReasonsPlugin.php | 64 +++++++++++----- .../reasons-6124c2e40a6cb71616a5.js | 1 - .../reasons-6738d5f6a670e6e80807.js | 8 ++ reasons/resources/rev-manifest.json | 4 +- ...-a008e38a64.css => reasons-2b40a218fb.css} | 0 releases.json | 12 +++ source/javascripts/modules/builder.js | 27 +++++-- source/javascripts/modules/render.js | 76 ++++++++++++++++--- source/javascripts/reasons.js | 43 +++++++++-- 10 files changed, 224 insertions(+), 58 deletions(-) delete mode 100644 reasons/resources/javascripts/reasons-6124c2e40a6cb71616a5.js create mode 100644 reasons/resources/javascripts/reasons-6738d5f6a670e6e80807.js rename reasons/resources/stylesheets/{reasons-a008e38a64.css => reasons-2b40a218fb.css} (100%) diff --git a/README.md b/README.md index 70308c6..ac2e3dc 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Reasons v. 1.0.4 for Craft CMS ![Craft 2.5](https://img.shields.io/badge/craft-2.5-red.svg?style=flat-square) +# Reasons v. 1.0.5 for Craft CMS ![Craft 2.5](https://img.shields.io/badge/craft-2.5-red.svg?style=flat-square) _Supercharge your field layouts with conditionals._ @@ -9,10 +9,8 @@ Inspired by Advanced Custom Fields for WordPress, Reasons adds simple conditiona _Big props to [Benjamin Fleming](https://github.com/benjamminf) for some good ideas that I stole, and helpful chatting on Slack. Thanks!_ ## Recent updates -* Support for all native element types (Entries, Assets, Categories, Tags, Users and Global Sets) -* Support for [Tag Manager](https://github.com/boboldehampsink/tagmanager) -* Support for all native element editor modals -* Support for Live Preview +* Support for all native relational fields (Entries, Assets, Categories, Tags and Users) as toggle fields +* Support for Solspace Calendar ## Requirements @@ -24,8 +22,6 @@ _Big props to [Benjamin Fleming](https://github.com/benjamminf) for some good id * Move the `/reasons` folder to your `/craft/plugins` folder * Install from the Control Panel (`/admin/settings/plugins`) -**Important: Reasons requires Craft 2.5+.** Need Craft 2.4 or 2.3 support? Check out the [legacy branch](https://github.com/mmikkel/Reasons-Craft/tree/legacy). - ## Usage and common questions ![Setting up conditionals using the built-in field layout designer](http://g.recordit.co/R7Ti1xpL9Q.gif) @@ -36,9 +32,9 @@ Note that for element sources that support tabbed field layouts, Reasons is desi ### What's a toggle field? -A _toggle field_ in Reasons is a field that can be used to "toggle" another field (the _target field_) on or off (show or hide, as it were). This works by comparing the value of the toggle field declared inside the Field Layout Designer, to the current value of that same field when the content is edited. +A _toggle field_ in Reasons is a field that can be used to "toggle" another field (the _target field_) on or off (show or hide, as it were). -The following FieldTypes can be used as toggle fields: +The following stock FieldTypes can be used as toggle fields: * Lightswitch * Dropdown @@ -48,12 +44,31 @@ The following FieldTypes can be used as toggle fields: * Number * Position Select * Plain Text +* Entries +* Categories +* Tags +* Assets +* Users + +The following custom FieldTypes are also supported: + +* Solspace Events + +### Where does it work? -All FieldTypes (even custom ones) can be target fields. +Reasons currently works for -### Does Reasons work inside Matrix fields? +* Entries +* Categories +* Tags +* Assets +* Users +* Tag Manager +* Solspace Calendar Events -Nope, not yet. Work is underway for Matrix block type support, but it's pretty difficult and there's no ETA, unfortunately. +Reasons also works in Live Preview and Element Editor modals. + +Unfortunately, Reasons does _not_ work inside Matrix blocks. Matrix support is hopefully coming in the future. ### A note on required fields @@ -94,6 +109,14 @@ Please report any bugs, feature requests or other issues [here](https://github.c ### Changelog +#### 1.0.5 (07.06.2016) + +* [Added] Support for Entries, Assets, Tags, Users and Categories fieldtypes as toggle fields +* [Added] Support for Solspace Calendar Events fieldtype as toggle fields +* [Added] Full support for Solspace Calendar +* [Improved] Reasons will no longer cache conditionals if devMode is enabled +* [Improved] Hidden fields are no longer tabbable/focusable + #### 1.0.4 (05.25.2016) * [Fixed] Fixes a rare issue where Reasons would hide fields inside Matrix blocks diff --git a/reasons/ReasonsPlugin.php b/reasons/ReasonsPlugin.php index 82109b6..e8361bf 100644 --- a/reasons/ReasonsPlugin.php +++ b/reasons/ReasonsPlugin.php @@ -18,16 +18,16 @@ class ReasonsPlugin extends BasePlugin { - protected $_version = '1.0.4', - $_schemaVersion = '1.1', - $_developer = 'Mats Mikkel Rummelhoff', - $_developerUrl = 'http://mmikkel.no', - $_pluginName = 'Reasons', - $_pluginUrl = 'https://github.com/mmikkel/Reasons-Craft', - $_releaseFeedUrl = 'https://raw.githubusercontent.com/mmikkel/Reasons-Craft/master/releases.json', - $_documentationUrl = 'https://github.com/mmikkel/Reasons-Craft/blob/master/README.md', - $_description = 'Adds conditionals to field layouts.', - $_minVersion = '2.5'; + protected $_version = '1.0.5'; + protected $_schemaVersion = '1.1'; + protected $_developer = 'Mats Mikkel Rummelhoff'; + protected $_developerUrl = 'http://mmikkel.no'; + protected $_pluginName = 'Reasons'; + protected $_pluginUrl = 'https://github.com/mmikkel/Reasons-Craft'; + protected $_releaseFeedUrl = 'https://raw.githubusercontent.com/mmikkel/Reasons-Craft/master/releases.json'; + protected $_documentationUrl = 'https://github.com/mmikkel/Reasons-Craft/blob/master/README.md'; + protected $_description = 'Adds conditionals to field layouts.'; + protected $_minVersion = '2.5'; /** * @return string @@ -162,7 +162,7 @@ public function init() $this->includeResources(); craft()->templates->includeJs('if (window.Craft && window.Craft.ReasonsPlugin) { - Craft.ReasonsPlugin.init(' . $this->getData() . '); + Craft.ReasonsPlugin.init('.$this->getData().'); }'); craft()->on('fields.saveFieldLayout', array($this, 'onSaveFieldLayout')); @@ -268,14 +268,19 @@ protected function includeResources() */ protected function getData() { - if (!$data = craft()->fileCache->get($this->getCacheKey())) { + $doCacheData = !craft()->config->get('devMode'); + $cacheKey = $this->getCacheKey(); + $data = $doCacheData ? craft()->fileCache->get($cacheKey) : null; + if (!$data) { $data = array( 'conditionals' => $this->getConditionals(), 'toggleFieldTypes' => $this->getToggleFieldTypes(), 'toggleFields' => $this->getToggleFields(), 'fieldIds' => $this->getFieldIds(), ); - craft()->fileCache->set($this->getCacheKey(), $data, 1800); // Cache for 30 minutes + if ($doCacheData) { + craft()->fileCache->set($this->getCacheKey(), $data, 1800); // Cache for 30 minutes + } } return json_encode($data); } @@ -323,14 +328,14 @@ protected function getConditionals() $sources['globalSet:' . $globalSet->id] = $globalSet->fieldLayoutId; } - // Matrix blocks - $matrixBlockTypeRecords = MatrixBlockTypeRecord::model()->findAll(); - if ($matrixBlockTypeRecords) { - foreach ($matrixBlockTypeRecords as $matrixBlockTypeRecord) { - $matrixBlockType = MatrixBlockTypeModel::populateModel($matrixBlockTypeRecord); - $sources['matrixBlockType:' . $matrixBlockType->id] = $matrixBlockType->fieldLayoutId; - } - } + // Matrix blocks – TODO + // $matrixBlockTypeRecords = MatrixBlockTypeRecord::model()->findAll(); + // if ($matrixBlockTypeRecords) { + // foreach ($matrixBlockTypeRecords as $matrixBlockTypeRecord) { + // $matrixBlockType = MatrixBlockTypeModel::populateModel($matrixBlockTypeRecord); + // $sources['matrixBlockType:' . $matrixBlockType->id] = $matrixBlockType->fieldLayoutId; + // } + // } // Users $usersFieldLayout = craft()->fields->getLayoutByType(ElementType::User); @@ -338,6 +343,15 @@ protected function getConditionals() $sources['users'] = $usersFieldLayout->id; } + // Solspace Calendar + $solspaceCalendarPlugin = craft()->plugins->getPlugin('calendar'); + if ($solspaceCalendarPlugin && $solspaceCalendarPlugin->getDeveloper() === 'Solspace') { + $solspaceCalendarFieldLayout = craft()->fields->getLayoutByType('Calendar_Event'); + if ($solspaceCalendarFieldLayout) { + $sources['solspaceCalendar'] = $solspaceCalendarFieldLayout->id; + } + } + // Get all conditionals $conditionals = array(); $conditionalsRecords = Reasons_ConditionalsRecord::model()->findAll(); @@ -367,6 +381,7 @@ protected function getConditionals() protected function getToggleFieldTypes() { return array( + // Stock FieldTypes 'Lightswitch', 'Dropdown', 'Checkboxes', @@ -375,6 +390,13 @@ protected function getToggleFieldTypes() 'Number', 'PositionSelect', 'PlainText', + 'Entries', + 'Categories', + 'Tags', + 'Assets', + 'Users', + // Custom FieldTypes + 'Calendar_Event', ); } diff --git a/reasons/resources/javascripts/reasons-6124c2e40a6cb71616a5.js b/reasons/resources/javascripts/reasons-6124c2e40a6cb71616a5.js deleted file mode 100644 index a6ba65e..0000000 --- a/reasons/resources/javascripts/reasons-6124c2e40a6cb71616a5.js +++ /dev/null @@ -1 +0,0 @@ -!function(e){function t(s){if(i[s])return i[s].exports;var n=i[s]={exports:{},id:s,loaded:!1};return e[s].call(n.exports,n,n.exports,t),n.loaded=!0,n.exports}var i={};return t.m=e,t.c=i,t.p="javascripts/",t(0)}([function(e,t,i){e.exports=i(1)},function(e,t,i){"use strict";!function(e){return e.Craft?void(Craft.ReasonsPlugin={FieldLayoutDesigner:i(2),ConditionalsRenderer:i(4),ASSET_SOURCE_HANDLE:"assetSource",CATEGORY_GROUP_HANDLE:"categoryGroup",TAG_GROUP_HANDLE:"tagGroup",GLOBAL_SET_HANDLE:"globalSet",ENTRY_TYPE_HANDLE:"entryType",SECTION_HANDLE:"section",USERS_HANDLE:"users",FIELDS_HANDLE:"field",ASSET_SOURCE_ACTION:"assetSources/saveSource",CATEGORY_ACTION:"categories/saveCategory",CATEGORY_GROUP_ACTION:"categories/saveGroup",TAG_ACTION:"tagManager/saveTag",TAG_GROUP_ACTION:"tags/saveTagGroup",GLOBAL_SET_CONTENT_ACTION:"globals/saveContent",GLOBAL_SET_ACTION:"globals/saveSet",ENTRY_ACTION:"entries/saveEntry",ENTRY_TYPE_ACTION:"sections/saveEntryType",USERS_ACTION:"users/saveUser",USERS_FIELDS_ACTION:"users/saveFieldLayout",FIELDS_ACTION:"fields/saveField",RENDER_CONTEXT:"render",LAYOUT_DESIGNER_CONTEXT:"fld",FIELD_DESIGNER_CONTEXT:"field",init:function(e){this.data=e,this.initPrimaryForm()},initPrimaryForm:function(){this.destroyPrimaryForm(),Craft.cp.$primaryForm&&(this.primaryForm=this.initForm(Craft.cp.$primaryForm))},destroyPrimaryForm:function(){this.primaryForm&&(this.primaryForm.destroy(),delete this.primaryForm)},initElementEditor:function(e){var t=this.getConditionals(e);if(!t)return!1;var i=(new Date).getTime(),s=$.proxy(function(){var e=(new Date).getTime(),n=$(".elementeditor:last"),a=n.length>0?n.closest(".hud"):!1,r=a&&a.length>0?a.data("elementEditor"):!1,o=r?r.$form:!1;o?(r._reasonsForm=new this.ConditionalsRenderer(o,t),r.hud.on("hide",$.proxy(this.destroyElementEditorForm,this,r))):2e3>e-i&&Garnish.requestAnimationFrame(s)},this);s()},destroyElementEditorForm:function(e){var t=e._reasonsForm||null;t&&(t.destroy(),delete e._reasonsForm)},initForm:function(e){var t=this.getElementSourceFromForm(e),i=t?this.getFormContext(e):!1;if(!t||!i)return!1;var s=t.type+(t.id?":"+t.id:""),n=this.getConditionals(s);switch(i){case this.LAYOUT_DESIGNER_CONTEXT:return new this.FieldLayoutDesigner(e,n);case this.FIELD_DESIGNER_CONTEXT:return null;case this.RENDER_CONTEXT:return n?new this.ConditionalsRenderer(e,n):null}return null},getConditionals:function(e){return e?this.data.conditionals&&this.data.conditionals.hasOwnProperty(e)?this.data.conditionals[e]:null:this.data.conditionals||{}},getToggleFields:function(){return this.data.toggleFields?this.data.toggleFields:[]},getToggleFieldById:function(e){e=parseInt(e);for(var t=this.getToggleFields(),i=t.length,s=0;i>s;++s)if(parseInt(t[s].id)===e)return t[s];return!1},getFieldIds:function(){return this.data.fieldIds?this.data.fieldIds:{}},getFieldIdByHandle:function(e){var t=this.getFieldIds();return t&&t.hasOwnProperty(e)?t[e]:!1},getToggleFieldTypes:function(){return this.data.toggleFieldTypes?this.data.toggleFieldTypes:[]},getElementSourceFromForm:function(e){if(e.data("elementEditor"))return!1;var t=e.find('input[type="hidden"][name="namespace"]').val();t&&(t+="-");var i,s,n=e.find('input[type="hidden"][name="action"]').val();switch(n){case this.ASSET_SOURCE_ACTION:i=this.ASSET_SOURCE_HANDLE,s='input[type="hidden"][name="sourceId"]';break;case this.CATEGORY_ACTION:case this.CATEGORY_GROUP_ACTION:i=this.CATEGORY_GROUP_HANDLE,s='input[type="hidden"][name="groupId"]';break;case this.GLOBAL_SET_CONTENT_ACTION:case this.GLOBAL_SET_ACTION:i=this.GLOBAL_SET_HANDLE,s='input[type="hidden"][name="setId"]';break;case this.ENTRY_ACTION:var a=e.find('select#entryType, input[type="hidden"][name="entryTypeId"], input[type="hidden"][name="typeId"], #'+t+"entryType");i=a.length?this.ENTRY_TYPE_HANDLE:this.SECTION_HANDLE,s=a.length?'select#entryType, input[type="hidden"][name="entryTypeId"], input[type="hidden"][name="typeId"], #'+t+"entryType":'input[type="hidden"][name="sectionId"], #'+t+"section";break;case this.ENTRY_TYPE_ACTION:i=this.ENTRY_TYPE_HANDLE,s='input[type="hidden"][name="entryTypeId"]';break;case this.TAG_ACTION:case this.TAG_GROUP_ACTION:i=this.TAG_GROUP_HANDLE,s='input[type="hidden"][name="tagGroupId"], input[type="hidden"][name="groupId"]';break;case this.USERS_ACTION:case this.USERS_FIELDS_ACTION:i=this.USERS_HANDLE;break;case this.FIELDS_ACTION:i=this.FIELDS_HANDLE,s='input[type="hidden"][name="fieldId"]'}return i?{type:i,id:s?0|e.find(s).val():!1}:!1},getFormContext:function(e){if(e.data("elementEditor"))return!1;var t=e.find('input[type="hidden"][name="action"]').val();switch(t){case this.GLOBAL_SET_CONTENT_ACTION:case this.ENTRY_ACTION:case this.TAG_ACTION:case this.CATEGORY_ACTION:case this.USERS_ACTION:return this.RENDER_CONTEXT;case this.ASSET_SOURCE_ACTION:case this.CATEGORY_GROUP_ACTION:case this.GLOBAL_SET_ACTION:case this.ENTRY_TYPE_ACTION:case this.TAG_GROUP_ACTION:case this.USERS_FIELDS_ACTION:return this.LAYOUT_DESIGNER_CONTEXT;case this.FIELDS_ACTION:return this.FIELD_DESIGNER_CONTEXT}return!1}}):!1}(window)},function(e,t,i){"use strict";function s(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var n=function(){function e(e,t){for(var i=0;i'},modal:function(){return'