Commit c26c0668 authored by Tim Rogers's avatar Tim Rogers

COS-46 updated to set initialPath on reference widget instead of hidden val,...

COS-46 updated to set initialPath on reference widget instead of hidden val, moved repeated code into function
parent 7545154d
......@@ -54,6 +54,7 @@ module.exports = function(utils, key, $lockButton){
var $source = $li.find('.source');
var $hiddenRef = $li.find('.hidden-ref');
var initialPath = ref;
var referenceInitialPath = options.referenceInitialPath;
if(ref && options.source){
if(initialPath.indexOf('jcr:content') > -1){
......@@ -61,16 +62,15 @@ module.exports = function(utils, key, $lockButton){
}else{
initialPath += "/jcr:content/" + options.source
}
} else if(options.initialPath) {
initialPath = options.initialPath;
}
//default initial path
if(!initialPath) {
if(!referenceInitialPath) {
if(_.includes(utils.page.baseUrl, '/pages/')) {
initialPath = utils.page.baseUrl.replace('/pages/', '/collections/').split('/').slice(0, 7).join('/');
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('/');
initialPath = _.concat(pathParts.slice(0, pathParts.indexOf('feeds')), 'collections', pathParts.slice(pathParts.indexOf('feeds')+2, pathParts.length)).join('/');
referenceInitialPath = _.concat(pathParts.slice(0, pathParts.indexOf('feeds')), 'collections', pathParts.slice(pathParts.indexOf('feeds')+2, pathParts.length)).join('/');
}
}
......@@ -95,7 +95,7 @@ module.exports = function(utils, key, $lockButton){
$source.html('');
$li.addClass('warning');
}
}};
}, initialPath: referenceInitialPath};
referenceWidget.init($hiddenRef.parent(), clonedUtils);
......@@ -211,21 +211,28 @@ module.exports = function(utils, key, $lockButton){
});
});
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 initialPath;
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) {
initialPath = currentComponentBlRefs[0];
referenceInitialPath = currentComponentBlRefs[0];
} else { //otherwise find least number of differences in path
initialPath = getClosestToComponentPath(currentComponentBlRefs);
referenceInitialPath = getClosestToComponentPath(currentComponentBlRefs);
}
}
//Query the current page to find other blrefs (only want parents of current component??)
if(!initialPath) {
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
......@@ -234,17 +241,11 @@ module.exports = function(utils, key, $lockButton){
return _.includes(dialog.config.saveTarget, path);
});
var queriedblRefs = _.uniq(_.map(filteredData, function(item) {return item['bl:ref'] || item['bl:autoStubbed']}));
initialPath = getClosestToComponentPath(queriedblRefs);
var newItem = buildBlRefItem('', {initialPath}, {updateWarningState: updateWarningState});
mappings.append(newItem);
newItem.find('.edit').click();
updateWarningState();
referenceInitialPath = getClosestToComponentPath(queriedblRefs);
addNewItem(referenceInitialPath);
});
} else {
var newItem = buildBlRefItem('', {initialPath}, {updateWarningState: updateWarningState});
mappings.append(newItem);
newItem.find('.edit').click();
updateWarningState();
addNewItem(referenceInitialPath);
}
});
......
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