Skip to content

Commit

Permalink
rmove underscore from content overlays, some refactoring as appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwoulfe authored and nul800sebastiaan committed Jul 31, 2020
1 parent 12087e7 commit ad1eda4
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 213 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
(function () {
"use strict";

function PublishController($scope, localizationService, contentEditingHelper) {
function PublishController($scope, localizationService) {

var vm = this;
vm.loading = true;
vm.isNew = true;

vm.changeSelection = changeSelection;

/** Returns true if publish meets the requirements of mandatory languages */
/**
* Returns true if publish meets the requirements of mandatory languages
* */
function canPublish() {

var hasSomethingToPublish = false;

for (var i = 0; i < vm.variants.length; i++) {
var variant = vm.variants[i];

// if varaint is mandatory and not already published:
vm.variants.forEach(variant => {
// if variant is mandatory and not already published:
if (variant.publish === false && notPublishedMandatoryFilter(variant)) {
return false;
}
if (variant.publish === true) {
hasSomethingToPublish = true;
}
});

}
return hasSomethingToPublish;
}

Expand All @@ -36,7 +36,6 @@
variant.save = variant.publish;
}


function hasAnyDataFilter(variant) {

if (variant.name == null || variant.name.length === 0) {
Expand All @@ -47,140 +46,142 @@
return true;
}

for (var t=0; t < variant.tabs.length; t++){
for (var p=0; p < variant.tabs[t].properties.length; p++){
var property = variant.tabs[t].properties[p];
variant.tabs.forEach(tab => {
tab.properties.forEach(property => {
if (property.value != null && property.value.length > 0) {
return true;
}
}
}
});
});

return false;
}

/**
* determine a variant is 'dirty' (meaning it will show up as publish-able) if it's
* * it's editor is in a $dirty state
* * it has pending saves
* * it is unpublished
* @param {*} variant
*/
function dirtyVariantFilter(variant) {
//determine a variant is 'dirty' (meaning it will show up as publish-able) if it's
// * it's editor is in a $dirty state
// * it has pending saves
// * it is unpublished
return (variant.isDirty || variant.state === "Draft" || variant.state === "PublishedPendingChanges");
}

/**
* determine a variant is 'dirty' (meaning it will show up as publish-able) if it's
* * variant is active
* * it's editor is in a $dirty state
* * it has pending saves
* * it is unpublished
* @param {*} variant
*/
function publishableVariantFilter(variant) {
//determine a variant is 'dirty' (meaning it will show up as publish-able) if it's
// * variant is active
// * it's editor is in a $dirty state
// * it has pending saves
// * it is unpublished

return (variant.active || variant.isDirty || variant.state === "Draft" || variant.state === "PublishedPendingChanges");
}

function notPublishedMandatoryFilter(variant) {
return variant.state !== "Published" && isMandatoryFilter(variant);
}
function isMandatoryFilter(variant) {
//determine a variant is 'dirty' (meaning it will show up as publish-able) if it's
// * has a mandatory language
// * without having a segment, segments cant be mandatory at current state of code.

/**
* determine a variant is 'dirty' (meaning it will show up as publish-able) if it's
* * has a mandatory language
* * without having a segment, segments cant be mandatory at current state of code.
* @param {*} variant
*/
function isMandatoryFilter(variant) {
return (variant.language && variant.language.isMandatory === true && variant.segment == null);
}

/**
* determine a variant is needed, but not already a choice.
* * publishable — aka. displayed as a publish option.
* * published — its already published and everything is then fine.
* * mandatory — this is needed, and thats why we highlight it.
* @param {*} variant
*/
function notPublishableButMandatoryFilter(variant) {
//determine a variant is needed, but not already a choice.
// * publishable — aka. displayed as a publish option.
// * published — its already published and everything is then fine.
// * mandatory — this is needed, and thats why we highlight it.

return !publishableVariantFilter(variant) && variant.state !== "Published" && variant.isMandatory === true;
}

function onInit() {

vm.variants = $scope.model.variants;

_.each(vm.variants, (variant) => {
// If we have a variant that's not in the state of NotCreated,
// then we know we have data and it's not a new content node.
vm.isNew = vm.variants.some(variant => variant.state === 'NotCreated');

vm.variants.forEach(variant => {

// reset to not be published
variant.publish = false;
variant.save = false;

variant.isMandatory = isMandatoryFilter(variant);
variant.publish = variant.save = false;

// If we have a variant thats not in the state of NotCreated, then we know we have adata and its not a new content node.
if(variant.state !== "NotCreated") {
vm.isNew = false;
}
});
variant.isMandatory = isMandatoryFilter(variant);

_.each(vm.variants, (variant) => {

// if this is a new node and we have data on this variant.
if(vm.isNew === true && hasAnyDataFilter(variant)) {
if (vm.isNew === true && hasAnyDataFilter(variant)) {
variant.save = true;
}

});

vm.availableVariants = vm.variants.filter(publishableVariantFilter);
vm.missingMandatoryVariants = vm.variants.filter(notPublishableButMandatoryFilter);

// if any active varaiant that is available for publish, we set it to be published:
_.each(vm.availableVariants, (v) => {
vm.availableVariants.forEach(v => {
if(v.active) {
v.save = v.publish = true;
}
});

if (vm.availableVariants.length !== 0) {
vm.availableVariants.sort(function (a, b) {
vm.availableVariants.sort((a, b) => {
if (a.language && b.language) {
if (a.language.name > b.language.name) {
if (a.language.name < b.language.name) {
return -1;
}
if (a.language.name < b.language.name) {
if (a.language.name > b.language.name) {
return 1;
}
}
if (a.segment && b.segment) {
if (a.segment > b.segment) {
if (a.segment < b.segment) {
return -1;
}
if (a.segment < b.segment) {
if (a.segment > b.segment) {
return 1;
}
}
return 0;
});
}


$scope.model.disableSubmitButton = !canPublish();

if (vm.missingMandatoryVariants.length > 0) {
localizationService.localize("content_notReadyToPublish").then(function (value) {
const localizeKey = vm.missingMandatoryVariants.length > 0 ? 'content_notReadyToPublish' :
!$scope.model.title ? 'content_readyToPublish' : '';

if (localizeKey) {
localizationService.localize(localizeKey).then(value => {
$scope.model.title = value;
vm.loading = false;
});
} else {
if (!$scope.model.title) {
localizationService.localize("content_readyToPublish").then(function (value) {
$scope.model.title = value;
vm.loading = false;
});
} else {
vm.loading = false;
}
vm.loading = false;
}

}

onInit();

//when this dialog is closed, reset all 'publish' flags
$scope.$on('$destroy', function () {
for (var i = 0; i < vm.variants.length; i++) {
vm.variants[i].publish = false;
vm.variants[i].save = false;
}
$scope.$on('$destroy', () => {
vm.variants.forEach(variant => {
variant.publish = variant.save = false;
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,54 @@
function PublishDescendantsController($scope, localizationService) {

var vm = this;

vm.includeUnpublished = false;

vm.changeSelection = changeSelection;
vm.toggleIncludeUnpublished = toggleIncludeUnpublished;


function onInit() {

vm.variants = $scope.model.variants;
vm.displayVariants = vm.variants.slice(0);// shallow copy, we dont want to share the array-object(because we will be performing a sort method) but each entry should be shared (because we need validation and notifications).
vm.labels = {};

if (!$scope.model.title) {
localizationService.localize("buttons_publishDescendants").then(function (value) {
localizationService.localize("buttons_publishDescendants").then(value => {
$scope.model.title = value;
});
}

_.each(vm.variants, function (variant) {
vm.variants.forEach(variant => {
variant.isMandatory = isMandatoryFilter(variant);
});

if (vm.variants.length > 1) {

vm.displayVariants.sort(function (a, b) {
vm.displayVariants.sort((a, b) => {
if (a.language && b.language) {
if (a.language.name > b.language.name) {
if (a.language.name < b.language.name) {
return -1;
}
if (a.language.name < b.language.name) {
if (a.language.name > b.language.name) {
return 1;
}
}
if (a.segment && b.segment) {
if (a.segment > b.segment) {
if (a.segment < b.segment) {
return -1;
}
if (a.segment < b.segment) {
if (a.segment > b.segment) {
return 1;
}
}
return 0;
});

var active = _.find(vm.variants, function (v) {
return v.active;
});
var active = vm.variants.find(v => v.active);

if (active) {
//ensure that the current one is selected
active.publish = true;
active.save = true;
active.publish = active.save = true;
}

$scope.model.disableSubmitButton = !canPublish();
Expand All @@ -67,24 +62,21 @@
"key": "content_publishDescendantsHelp",
"tokens": [vm.variants[0].name]
};
}

}
}

function toggleIncludeUnpublished() {
console.log("toggleIncludeUnpublished")
vm.includeUnpublished = !vm.includeUnpublished;
}

/** Returns true if publishing is possible based on if there are un-published mandatory languages */
function canPublish() {
var selected = [];
for (var i = 0; i < vm.variants.length; i++) {
var variant = vm.variants[i];
vm.variants.forEach(variant => {

var published = !(variant.state === "NotCreated" || variant.state === "Draft");

if (variant.segment == null && variant.language && variant.language.isMandatory && !published && !variant.publish) {
if (variant.segment == null && variant.language && variant.language.isMandatory && !published && !variant.publish) {
//if a mandatory variant isn't published
//and not flagged for saving
//then we cannot continue
Expand All @@ -96,7 +88,8 @@
if (variant.publish) {
selected.push(variant.publish);
}
}
});

return selected.length > 0;
}

Expand All @@ -115,11 +108,10 @@
}

//when this dialog is closed, reset all 'publish' flags
$scope.$on('$destroy', function () {
for (var i = 0; i < vm.variants.length; i++) {
vm.variants[i].publish = false;
vm.variants[i].save = false;
}
$scope.$on('$destroy', () => {
vm.variants.forEach(variant => {
variant.publish = variant.save = false;
});
});

onInit();
Expand Down
Loading

0 comments on commit ad1eda4

Please sign in to comment.