Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
return (function(q, _, context) {
debugger;
var defer = q.defer();
sap.ui.require(["aicomp/domain/element/api", "aicomp/service/noty"], function(
api,
noty
) {
var current = api.current();
var flatcodes = flattenAndGroup(current);
api.model().setProperty("/flat_codes_start", shallowclone(flatcodes));
api.model().setProperty("/flat_codes", flatcodes);
var customController = _.merge({
save: saveChanges,
onActionPress: onActionPress,
priceForPlantsFormatter: function(price, units, plants){
return "" + (price * plants);
}
},
context
);
var actions = parseActions(current) || [];
actions.push( {
key: "Update",
icon: 'sap-icon://save',
action: saveChanges,
action_params: []
});
current.actions = actions;
defer.resolve(customController);
return customController;
function onActionPress(evt){
var src = evt.getSource();
var context = src.getBindingContext().getObject();
if( context && context.action){
context.action.apply(null, context.action_params );
}
}
function saveChanges(evt) {
var start = api.model().getProperty("/flat_codes_start");
var actual = api.model().getProperty("/flat_codes");
var diffed = diffs(start, actual);
if (diffed && diffed.length > 0) {
var promises = diffed.map(function(code) {
return api
.update(actual[code].id, actual[code], cleanForSaving)
.then(function(item){
api.model().setProperty('/flat_codes/' + item.metadata.code, item);
api.model().setProperty("/flat_codes_start", shallowclone(flatcodes));
return item;
});
});
q.allSettled(promises).then(function(results) {
var hasRejected = _.chain(results)
.filter(function(res) {
return res.status === "rejected";
})
.value();
if (_.get(hasRejected, "length", 0) > 0) {
noty
.error(
'Could not update offer: "' +
actual.VCPP.code +
'" because of: ' +
hasRejected[0].reason.message
)
.show();
} else {
noty.success("Saved offer " + actual.VCPP.code ).show();
}
return api.validate(api.model().getProperty("/flat_codes/VCPP" ));
});
}else{
noty.info("Nothing to save").show();
api.validate(api.model().getProperty("/flat_codes/VCPP" ));
}
function cleanForSaving(item) {
item = _.cloneDeep(item);
item = fixProperties(item);
item = fixParents(item);
delete item.selected;
delete item.image;
delete item.type;
//delete item.parts;
delete item.parent;
//delete item.metadata;
delete item.x0;
delete item.y0;
delete item.metadata_evalation;
delete item.template;
return item;
function fixParents(item) {
if (item.hasOwnProperty("parent")) {
delete item.parent;
}
if (item.hasOwnProperty("parts") && _.isArray(item.parts)) {
item.parts.forEach(function(sub) {
fixParents(sub);
});
}
return item;
}
function fixProperties(item) {
if (_.get(item, "extra.properties_kv", undefined)) {
delete item.extra.properties_kv;
}
_.chain(_.keys(item.extra.properties))
.forEach(function(key) {
var prop = item.extra[key];
if (prop === true) {
prop = "true";
}
if (prop === false) {
prop = "false";
}
})
.value();
return item;
}
}
function diffs(start, actual) {
var startkeys = _.keys(start);
var actualkeys = _.keys(actual);
var added = _.chain(actualkeys)
.filter(function(key) {
return startkeys.indexOf(key) === -1;
})
.value();
var removed = _.chain(startkeys).filter(function(key) {
return actualkeys.indexOf(key) === -1;
});
var changed = _.chain(actualkeys)
.filter(function(key) {
return startkeys.indexOf(key) !== -1;
})
.filter(function(key) {
return keyhaschanged(key, start, actual);
})
.value();
return changed;
function keyhaschanged(key, start, actual) {
var fieldstocompare = [
"code",
"extra.properties",
"parameters_grouped"
];
return (
_.chain(fieldstocompare)
.map(function(field) {
return haschanged(
JSON.stringify(_.get(start[key], field, "")),
JSON.stringify(_.get(actual[key], field, ""))
);
})
.filter(function(result) {
return result === true;
})
.first()
.value() || false
);
function haschanged(start, actual) {
return start !== actual;
}
}
}
}
function shallowclone(flatcodes) {
var cloned = _.clone(flatcodes);
_.chain(_.keys(cloned))
.forEach(function(key) {
cloned[key] = _.clone(cloned[key]);
cloned[key].extra = _.cloneDeep(cloned[key].extra);
cloned[key].parameters_grouped = _.cloneDeep(
cloned[key].parameters_grouped
);
})
.value();
return cloned;
}
function parseActions(current) {
var actions = _.chain(current.parts || [])
.map(function(item) {
return parseActions(item);
})
.flatten()
.value() || [];
var props = _.cloneDeep(current.extra.properties);
props = _.merge(props, _.cloneDeep(current.metadata.extra.properties));
var propKeys = _.keys(props);
var currentActions = _.chain(propKeys)
.filter(function(key) {
return key.indexOf('app-action') === 0;
})
.map(function(key) {
var value = props[key];
return {
key: key.replace('app-action-', ''),
value: value
};
})
.map(function(property) {
return evaluateProperty(property);
})
.filter(function(property) {
return property.attachmentType === "js" ||
property.attachmentType === "javascript";
})
.map(function(property) {
try {
var fAction = new Function(property.decodedValue);
property.action = fAction();
property.action_params = [current, _];
} catch (err) {
noty.error(err.message).show();
}
return property;
})
.value();
return actions.concat(currentActions);
function evaluateProperty(property) {
if (!property.type) {
property.type = "VALUE";
}
if (_.includes(property.value, "||")) {
var fields = property.value.split("||");
if (fields[1] !== undefined && fields[1] !== "undefined") {
var toSet = atob(fields[1]);
property.decodedValue = toSet;
}
property.type = "ATTACHMENT";
property.attachmentType = fields[0];
}
return property;
}
}
function flattenAndGroup(current) {
var items = flatten(current);
var grouped = _.groupBy(items, "metadata.code");
_.chain(_.keys(grouped))
.forEach(function(key) {
var arr = grouped[key];
grouped[key] = arr[0];
})
.value();
return grouped;
}
function flatten(current) {
var parts = _.chain(_.get(current, "parts", []))
.map(function(item) {
return flatten(item);
})
.flatten()
.value();
parts.push(current);
fixPropertiesBooleans(current);
var parameters_grouped = groupparameters(current);
if (parameters_grouped) {
current.parameters_grouped = parameters_grouped;
}
return parts;
}
function groupparameters(current) {
var grouped = _.groupBy(current.parameters, "name");
_.chain(_.keys(grouped))
.forEach(function(key) {
var arr = grouped[key];
grouped[key] = arr[0];
})
.value();
return grouped;
}
function fixPropertiesBooleans(current) {
var props = _.keys(current.extra.properties);
_.chain(props)
.forEach(function(prop) {
if (current.extra.properties[prop] === "true") {
current.extra.properties[prop] = true;
}
if (current.extra.properties[prop] === "false") {
current.extra.properties[prop] = false;
}
})
.value();
}
return customcontroller;
});
return defer.promise;
})(arguments[0], arguments[1], arguments[2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.