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){
}else if(node.toDelete){
//delete
_.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){
//copy just the relevant data
_.set(blMaps, node.path, cloneNode());
......
......@@ -12,6 +12,18 @@ module.exports = function(utils, key, $lockButton){
var widget = utils.widget;
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){
ref = ref || '';
options = options || {};
......@@ -46,6 +58,7 @@ module.exports = function(utils, key, $lockButton){
var $source = $li.find('.source');
var $hiddenRef = $li.find('.hidden-ref');
var initialPath = ref;
var referenceInitialPath = initialPath || options.referenceInitialPath;
if(ref && options.source){
if(initialPath.indexOf('jcr:content') > -1){
......@@ -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);
var clonedUtils = _.clone(utils);
clonedUtils.widget = {onChange: function(updatedVal){
......@@ -76,7 +99,7 @@ module.exports = function(utils, key, $lockButton){
$source.html('');
$li.addClass('warning');
}
}};
}, initialPath: referenceInitialPath};
referenceWidget.init($hiddenRef.parent(), clonedUtils);
......@@ -150,8 +173,6 @@ module.exports = function(utils, key, $lockButton){
//get the blRefs
var blRefs = dialogMapping.getRefsBySource(key);
var titleBar = $dialog.titleBar("Data Reference");
var body=`
......@@ -194,11 +215,42 @@ module.exports = function(utils, key, $lockButton){
});
});
$dialogBody.find('.add-mapping').click(function(){
var newItem = buildBlRefItem('', '', {updateWarningState: updateWarningState});
var addNewItem = function(referenceInitialPath) {
var newItem = buildBlRefItem('', {referenceInitialPath}, {updateWarningState: updateWarningState});
mappings.append(newItem);
newItem.find('.edit').click();
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');
......
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