Skip to content

Commit

Permalink
#16 Blocks now only save if they've been modified
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamminf committed Jun 13, 2016
1 parent df62978 commit a50fe85
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions neo/fieldtypes/NeoFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public function prepValueFromPost($data)
else
{
$block = $oldBlocksById[$blockId];
$block->modified = (isset($blockData['modified']) ? (bool) $blockData['modified'] : true);
}

$block->setOwner($this->element);
Expand Down
10 changes: 10 additions & 0 deletions neo/models/Neo_BlockModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
*/
class Neo_BlockModel extends BaseElementModel
{
// Public properties

/**
* Used to indicate whether this block needs to be saved to the database.
*
* @var bool
*/
public $modified = true;


// Protected properties

protected $elementType = Neo_ElementType::NeoBlock;
Expand Down
2 changes: 1 addition & 1 deletion neo/services/NeoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public function validateBlock(Neo_BlockModel $block)
*/
public function saveBlock(Neo_BlockModel $block, $validate = true)
{
if(!$validate || $this->validateBlock($block))
if($block->modified && (!$validate || $this->validateBlock($block)))
{
$blockRecord = $this->_getBlockRecord($block);
$isNewBlock = $blockRecord->isNewRecord();
Expand Down
28 changes: 26 additions & 2 deletions src/input/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const _defaults = {
level: 0,
buttons: null,
enabled: true,
collapsed: false
collapsed: false,
modified: true
}

export default Garnish.Base.extend({
Expand All @@ -30,6 +31,8 @@ export default Garnish.Base.extend({
_initialised: false,
_expanded: true,
_enabled: true,
_modified: true,
_initialPostData: null,

init(settings = {})
{
Expand All @@ -39,6 +42,7 @@ export default Garnish.Base.extend({
this._blockType = settings.blockType
this._id = settings.id
this._buttons = settings.buttons
this._modified = settings.modified

NS.enter(this._templateNs)

Expand All @@ -47,7 +51,8 @@ export default Garnish.Base.extend({
id: this._id,
enabled: !!settings.enabled,
collapsed: !!settings.collapsed,
level: settings.level
level: settings.level,
modified: settings.modified
}))

NS.leave()
Expand All @@ -68,6 +73,7 @@ export default Garnish.Base.extend({
this.$enabledInput = $neo.filter('[data-neo-b="input.enabled"]')
this.$collapsedInput = $neo.filter('[data-neo-b="input.collapsed"]')
this.$levelInput = $neo.filter('[data-neo-b="input.level"]')
this.$modifiedInput = $neo.filter('[data-neo-b="input.modified"]')
this.$status = $neo.filter('[data-neo-b="status"]')

if(this._buttons)
Expand Down Expand Up @@ -95,6 +101,12 @@ export default Garnish.Base.extend({

this.addListener(this.$togglerButton, 'dblclick', '@doubleClickTitle')
this.addListener(this.$tabButton, 'click', '@setTab')

if(!this.isNew())
{
this._initialPostData = Garnish.getPostData(this.$contentContainer)
this._detectChangeInterval = setInterval(() => this._detectChange(), 500)
}
},

initUi()
Expand Down Expand Up @@ -382,6 +394,18 @@ export default Garnish.Base.extend({
}
},

_detectChange()
{
const postData = Garnish.getPostData(this.$contentContainer)
const modified = !Craft.compare(postData, this._initialPostData)

if(modified !== this._modified)
{
this.$modifiedInput.val(modified ? 1 : 0)
this._modified = modified
}
},

'@settingSelect'(e)
{
const $option = $(e.option)
Expand Down
1 change: 1 addition & 0 deletions src/input/templates/block.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% set hasTabs = (type.getTabs() is not empty) %}
{% set isParent = type.isParent() %}
<div class="ni_block">
<input type="hidden" name="{{ 'modified'|ns }}" value="{{ modified ? 1 : 0 }}" data-neo-b="input.modified">
<input type="hidden" name="{{ 'type'|ns }}" value="{{ type.getHandle() }}">
<input type="hidden" name="{{ 'enabled'|ns }}" value="{{ enabled ? 1 : 0 }}" data-neo-b="input.enabled">
<input type="hidden" name="{{ 'collapsed'|ns }}" value="{{ collapsed ? 1 : 0 }}" data-neo-b="input.collapsed">
Expand Down

0 comments on commit a50fe85

Please sign in to comment.