Ensure the palette gets refreshed when saving a new block, and use ng-change instead of $watch
delete entity.data[fieldName];
};
- function buildPaletteLists() {
+ this.buildPaletteLists = function() {
var search = $scope.controls.fieldSearch ? $scope.controls.fieldSearch.toLowerCase() : null;
buildFieldList(search);
buildBlockList(search);
buildElementList(search);
- }
+ };
function buildFieldList(search) {
$scope.fieldList.length = 0;
});
}
- $scope.clearSearch = function() {
- $scope.controls.fieldSearch = '';
- };
-
// This gets called from jquery-ui so we have to manually apply changes to scope
$scope.buildPaletteLists = function() {
$timeout(function() {
$scope.$apply(function() {
- buildPaletteLists();
+ ctrl.buildPaletteLists();
});
});
};
return check(ctrl.editor.layout['#children'], {'#tag': 'af-field', name: fieldName});
};
+ // Checks if fields in a block are already in use on the form.
+ // Note that if a block contains no fields it can be used repeatedly, so this will always return false for those.
$scope.blockInUse = function(block) {
if (block['af-join']) {
return check(ctrl.editor.layout['#children'], {'af-join': block['af-join']});
return found.match;
}
- $scope.$watch('controls.addValue', function(fieldName) {
- if (fieldName) {
- if (!ctrl.entity.data) {
- ctrl.entity.data = {};
- }
- ctrl.entity.data[fieldName] = '';
- $scope.controls.addValue = '';
- }
- });
+ this.$onInit = function() {
+ // When a new block is saved, update the list
+ this.meta = afGui.meta;
+ $scope.$watchCollection('$ctrl.meta.blocks', function() {
+ $scope.controls.fieldSearch = '';
+ ctrl.buildPaletteLists();
+ });
- $scope.$watch('controls.fieldSearch', buildPaletteLists);
+ $scope.$watch('controls.addValue', function(fieldName) {
+ if (fieldName) {
+ if (!ctrl.entity.data) {
+ ctrl.entity.data = {};
+ }
+ ctrl.entity.data[fieldName] = '';
+ $scope.controls.addValue = '';
+ }
+ });
+ };
}
});
<fieldset class="af-gui-entity-palette">
<legend class="form-inline">
{{:: ts('Add:') }}
- <input ng-model="controls.fieldSearch" class="form-control" type="search" placeholder="" title="{{:: ts('Search fields') }}" />
+ <input ng-model="controls.fieldSearch" ng-change="$ctrl.buildPaletteLists()" class="form-control" type="search" placeholder="" title="{{:: ts('Search fields') }}" />
</legend>
<div class="af-gui-entity-palette-select-list">
<div ng-if="elementList.length">
<label>{{:: ts('Elements') }}</label>
<div ui-sortable="{update: buildPaletteLists, items: '> div:not(.disabled)', connectWith: '[ui-sortable]', placeholder: 'af-gui-dropzone'}" ui-sortable-update="$ctrl.editor.onDrop" ng-model="elementList">
<div ng-repeat="element in elementList" >
- {{ elementTitles[$index] }}
+ {{:: elementTitles[$index] }}
</div>
</div>
</div>
<label>{{:: ts('Blocks') }}</label>
<div ui-sortable="{update: buildPaletteLists, items: '> div:not(.disabled)', connectWith: '[data-entity=' + $ctrl.entity.name + '] > [ui-sortable]', placeholder: 'af-gui-dropzone'}" ui-sortable-update="$ctrl.editor.onDrop" ng-model="blockList">
<div ng-repeat="block in blockList" ng-class="{disabled: blockInUse(block)}">
- {{ blockTitles[$index] }}
+ {{:: blockTitles[$index] }}
</div>
</div>
</div>
<label>{{ fieldGroup.label }}</label>
<div ui-sortable="{update: buildPaletteLists, items: '> div:not(.disabled)', connectWith: '[data-entity=' + fieldGroup.entityName + '] > [ui-sortable]', placeholder: 'af-gui-dropzone'}" ui-sortable-update="$ctrl.editor.onDrop" ng-model="fieldGroup.fields">
<div ng-repeat="field in fieldGroup.fields" ng-class="{disabled: fieldInUse(field.name)}">
- {{ getField(fieldGroup.entityType, field.name).label }}
+ {{:: getField(fieldGroup.entityType, field.name).label }}
</div>
</div>
</div>
<fieldset ng-if="!$ctrl.entity.loading && $ctrl.editor.allowEntityConfig">
<legend>{{:: ts('Options') }}</legend>
- <div ng-include="'~/afGuiEditor/entityConfig/' + $ctrl.entity.type + '.html'"></div>
+ <div ng-include="::'~/afGuiEditor/entityConfig/' + $ctrl.entity.type + '.html'"></div>
</fieldset>
$scope.getField = afGui.getField;
- function buildPaletteLists() {
+ this.buildPaletteLists = function() {
var search = $scope.controls.fieldSearch ? $scope.controls.fieldSearch.toLowerCase() : null;
buildFieldList(search);
buildBlockList(search);
buildElementList(search);
- }
+ };
function buildBlockList(search) {
$scope.blockList.length = 0;
});
}
- $scope.clearSearch = function() {
- $scope.controls.fieldSearch = '';
- };
-
// This gets called from jquery-ui so we have to manually apply changes to scope
$scope.buildPaletteLists = function() {
$timeout(function() {
$scope.$apply(function() {
- buildPaletteLists();
+ ctrl.buildPaletteLists();
});
});
};
return check(ctrl.editor.layout['#children'], {'#tag': 'af-field', name: fieldName});
};
+ // Checks if fields in a block are already in use on the form.
+ // Note that if a block contains no fields it can be used repeatedly, so this will always return false for those.
+ $scope.blockInUse = function(block) {
+ if (block['af-join']) {
+ return check(ctrl.editor.layout['#children'], {'af-join': block['af-join']});
+ }
+ var fieldsInBlock = _.pluck(afGui.findRecursive(afGui.meta.blocks[block['#tag']].layout, {'#tag': 'af-field'}), 'name');
+ return check(ctrl.editor.layout['#children'], function(item) {
+ return item['#tag'] === 'af-field' && _.includes(fieldsInBlock, item.name);
+ });
+ };
+
// Check for a matching item for this entity
// Recursively checks the form layout, including block directives
function check(group, criteria, found) {
return found.match;
}
- $scope.$watch('controls.fieldSearch', buildPaletteLists);
+ this.$onInit = function() {
+ // When a new block is saved, update the list
+ this.meta = afGui.meta;
+ $scope.$watchCollection('$ctrl.meta.blocks', function() {
+ $scope.controls.fieldSearch = '';
+ ctrl.buildPaletteLists();
+ });
+ };
}
});
<fieldset class="af-gui-entity-palette">
<legend class="form-inline">
{{:: ts('Add:') }}
- <input ng-model="controls.fieldSearch" class="form-control" type="search" placeholder="" title="{{:: ts('Search fields') }}" />
+ <input ng-model="controls.fieldSearch" ng-change="$ctrl.buildPaletteLists()" class="form-control" type="search" placeholder="" title="{{:: ts('Search fields') }}" />
</legend>
<div class="af-gui-entity-palette-select-list">
<div ng-if="elementList.length">
<label>{{:: ts('Blocks') }}</label>
<div ui-sortable="{update: buildPaletteLists, items: '> div:not(.disabled)', connectWith: '[ui-sortable]', placeholder: 'af-gui-dropzone'}" ui-sortable-update="$ctrl.editor.onDrop" ng-model="blockList">
<div ng-repeat="block in blockList" ng-class="{disabled: blockInUse(block)}">
- {{ blockTitles[$index] }}
+ {{:: blockTitles[$index] }}
</div>
</div>
</div>