Commit d3fd9bd1 authored by Michael Seaholm's avatar Michael Seaholm

Merge branch 'feature/cos-46' of gitlab.entropy.cc:blacklight/bl-edit into release

parents 9938c6ed 9ebcd5c0
...@@ -101,6 +101,12 @@ module.exports = function(utils){ ...@@ -101,6 +101,12 @@ module.exports = function(utils){
}else if(node.toDelete){ }else if(node.toDelete){
//delete //delete
_.set(blMaps, node.path, undefined); _.set(blMaps, node.path, undefined);
//check if there are any other nodes on this bl:ref
var otherNodes = _.filter(_.get(blMaps, node.path.slice(0, -1)), 'source');
//Delete the entire bl:ref if no other nodes need it
if(_.isEmpty(otherNodes)) {
_.unset(blMaps, node.path.slice(0, 2));
}
}else if(node.modified){ }else if(node.modified){
//copy just the relevant data //copy just the relevant data
_.set(blMaps, node.path, cloneNode()); _.set(blMaps, node.path, cloneNode());
......
...@@ -10,7 +10,19 @@ module.exports = function(utils, key, $lockButton){ ...@@ -10,7 +10,19 @@ module.exports = function(utils, key, $lockButton){
var dialog = utils.dialog; var dialog = utils.dialog;
var widget = utils.widget; var widget = utils.widget;
var mappingContext = {}; var mappingContext = {};
var getClosestToComponentPath = function(paths) {
return _.minBy(paths, function(path) {
var target = dialog.config.saveTarget;
if(!path.includes('jcr:content')) {
target = target.split('/jcr:content')[0];
}
var pageParts = target.split('/');
var pathParts = path.split('/');
return (pageParts.length > pathParts.length ? _.difference(pageParts, pathParts).length : _.difference(pathParts, pageParts).length);
});
}
var buildBlRefItem = function(ref, options, parentTriggers){ var buildBlRefItem = function(ref, options, parentTriggers){
ref = ref || ''; ref = ref || '';
...@@ -44,8 +56,9 @@ module.exports = function(utils, key, $lockButton){ ...@@ -44,8 +56,9 @@ module.exports = function(utils, key, $lockButton){
$li.find('.tooltipped').tooltip(); $li.find('.tooltipped').tooltip();
var $ref = $li.find('.reference'); var $ref = $li.find('.reference');
var $source = $li.find('.source'); var $source = $li.find('.source');
var $hiddenRef = $li.find('.hidden-ref'); var $hiddenRef = $li.find('.hidden-ref');
var initialPath = ref; var initialPath = ref;
var referenceInitialPath = initialPath || options.referenceInitialPath;
if(ref && options.source){ if(ref && options.source){
if(initialPath.indexOf('jcr:content') > -1){ if(initialPath.indexOf('jcr:content') > -1){
...@@ -55,6 +68,16 @@ module.exports = function(utils, key, $lockButton){ ...@@ -55,6 +68,16 @@ module.exports = function(utils, key, $lockButton){
} }
} }
//default initial path
if(!referenceInitialPath) {
if(_.includes(utils.page.baseUrl, '/pages/')) {
referenceInitialPath = utils.page.baseUrl.replace('/pages/', '/collections/').split('/').slice(0, 7).join('/');
} else if(_.includes(utils.page.baseUrl, '/feeds/')) {
var pathParts = utils.page.baseUrl.split('/');
referenceInitialPath = _.concat(pathParts.slice(0, pathParts.indexOf('feeds')), 'collections', pathParts.slice(pathParts.indexOf('feeds')+2, pathParts.length)).join('/');
}
}
$hiddenRef.val(initialPath); $hiddenRef.val(initialPath);
var clonedUtils = _.clone(utils); var clonedUtils = _.clone(utils);
clonedUtils.widget = {onChange: function(updatedVal){ clonedUtils.widget = {onChange: function(updatedVal){
...@@ -76,7 +99,7 @@ module.exports = function(utils, key, $lockButton){ ...@@ -76,7 +99,7 @@ module.exports = function(utils, key, $lockButton){
$source.html(''); $source.html('');
$li.addClass('warning'); $li.addClass('warning');
} }
}}; }, initialPath: referenceInitialPath};
referenceWidget.init($hiddenRef.parent(), clonedUtils); referenceWidget.init($hiddenRef.parent(), clonedUtils);
...@@ -150,8 +173,6 @@ module.exports = function(utils, key, $lockButton){ ...@@ -150,8 +173,6 @@ module.exports = function(utils, key, $lockButton){
//get the blRefs //get the blRefs
var blRefs = dialogMapping.getRefsBySource(key); var blRefs = dialogMapping.getRefsBySource(key);
var titleBar = $dialog.titleBar("Data Reference"); var titleBar = $dialog.titleBar("Data Reference");
var body=` var body=`
...@@ -194,11 +215,42 @@ module.exports = function(utils, key, $lockButton){ ...@@ -194,11 +215,42 @@ module.exports = function(utils, key, $lockButton){
}); });
}); });
$dialogBody.find('.add-mapping').click(function(){ var addNewItem = function(referenceInitialPath) {
var newItem = buildBlRefItem('', '', {updateWarningState: updateWarningState}); var newItem = buildBlRefItem('', {referenceInitialPath}, {updateWarningState: updateWarningState});
mappings.append(newItem); mappings.append(newItem);
newItem.find('.edit').click(); newItem.find('.edit').click();
updateWarningState(); updateWarningState();
}
$dialogBody.find('.add-mapping').click(function(){
var referenceInitialPath;
//Check the Current Component for other blrefs
var currentComponentBlRefs = _.uniq(_.flatMap(dialogMapping.getRefsBySource(), function(value) {return _.keys(value)}));
if(currentComponentBlRefs.length) {
//if only 1 then just set it
if(currentComponentBlRefs.length === 1) {
referenceInitialPath = currentComponentBlRefs[0];
} else { //otherwise find least number of differences in path
referenceInitialPath = getClosestToComponentPath(currentComponentBlRefs);
}
}
//Query the current page to find other blrefs (only want parents of current component??)
if(!referenceInitialPath) {
var blrefQuery = `/content.custom-query.json?statement=/jcr:root${utils.page.baseUrl}/jcr:content//*[@bl:ref or @bl:autoStubbed]&property=bl:ref,bl:autoStubbed&queryType=xpath`
utils.sling.request(blrefQuery, { leaveMangledNames: true }, function(err, data) {
//only get from components that are parents of the current component
var filteredData = _.filter(data, function(item) {
let path = item['jcr:path'].split('/').slice(0, -2).join('/');
return _.includes(dialog.config.saveTarget, path);
});
var queriedblRefs = _.uniq(_.map(filteredData, function(item) {return item['bl:ref'] || item['bl:autoStubbed']}));
referenceInitialPath = getClosestToComponentPath(queriedblRefs);
addNewItem(referenceInitialPath);
});
} else {
addNewItem(referenceInitialPath);
}
}); });
var $applyButton = $dialog.find('.bl-save-button'); var $applyButton = $dialog.find('.bl-save-button');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment