Skip to content

Instantly share code, notes, and snippets.

@sniffdk
Created August 17, 2020 08:33
Show Gist options
  • Save sniffdk/9ba2087f61530653135864599d3cf9f8 to your computer and use it in GitHub Desktop.
Save sniffdk/9ba2087f61530653135864599d3cf9f8 to your computer and use it in GitHub Desktop.
public class ContentCopierAppComponent : IUserComposer
{
public void Compose(Composition composition)
{
composition.ContentApps().Append<ContentCopierApp>();
}
}
public class ContentCopierApp : IContentAppFactory
{
private readonly IContentTypeService _contentTypeService;
public ContentCopierApp(IContentTypeService contentTypeService)
{
_contentTypeService = contentTypeService;
}
public ContentApp GetContentAppFor(object source, IEnumerable<IReadOnlyUserGroup> userGroups)
{
if (!(source is IContent content))
{
return null;
}
var contentType = _contentTypeService.Get(content.ContentTypeId);
if (contentType.CompositionAliases().Contains(CompositionMainContent.ModelTypeAlias))
{
return new ContentApp
{
Alias = "ContentCopier",
Name = "Copy content",
Icon = "icon-documents",
View = "/App_Plugins/ContentCopier/view.html",
Weight = 0
};
}
return null;
}
}
angular.module("umbraco")
.controller("ContentCopierApp", function ($scope, $timeout, editorState, userService, contentResource) {
var vm = this;
vm.CurrentNodeId = editorState.current.id;
vm.CurrentNodeAlias = editorState.current.contentTypeAlias;
vm.Editor = editorState.getCurrent();
vm.copyContent = function (variantIndex) {
var fromProperties = vm.Editor.variants[variantIndex].tabs[0].properties;
var fromGridValue;
for (var i = 0; i < fromProperties.length; i++) {
if (fromProperties[i].alias === "grid") {
fromGridValue = JSON.parse(JSON.stringify(fromProperties[i].value));
break;
}
}
//console.log("original guid before", vm.Editor.variants[variantIndex].tabs[0].properties[0].value.sections[0].rows[0].areas[0].controls[0].$uniqueId);
//console.log("copy guid before", fromGridValue.sections[0].rows[0].areas[0].controls[0].$uniqueId);
// Change uniquie id properties before copying
for (var k = 0; k < fromGridValue.sections.length; k++) {
for (var l = 0; l < fromGridValue.sections[k].rows.length; l++) {
fromGridValue.sections[k].rows[l].id = generateGuid();
fromGridValue.sections[k].rows[l].$uniqueId = generateGuid();
for (var n = 0; n < fromGridValue.sections[k].rows[l].areas.length; n++) {
fromGridValue.sections[k].rows[l].areas[n].$uniqueId = generateGuid();
for (var o = 0; o < fromGridValue.sections[k].rows[l].areas[n].controls.length; o++) {
fromGridValue.sections[k].rows[l].areas[n].controls[o].$uniqueId = generateGuid();
}
}
}
}
//console.log("");
//console.log("original guid after", vm.Editor.variants[variantIndex].tabs[0].properties[0].value.sections[0].rows[0].areas[0].controls[0].$uniqueId);
//console.log("copy guid after", fromGridValue.sections[0].rows[0].areas[0].controls[0].$uniqueId);
var variantDestinationIndex = variantIndex === 0 ? 1 : 0;
var toProperties = vm.Editor.variants[variantDestinationIndex].tabs[0].properties;
for (var j = 0; j < toProperties.length; j++) {
if (toProperties[j].alias === "grid") {
toProperties[j].value = fromGridValue;
break;
}
}
vm.contentCopied = true;
$timeout(function () {
vm.contentCopied = false;
}, 4000);
};
// Create guids
// Taken from http://www.broofa.com/Tools/Math.uuid.js
var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
function generateGuid() {
var chars = CHARS, uuid = new Array(36), rnd = 0, r;
for (var i = 0; i < 36; i++) {
if (i === 8 || i === 13 || i === 18 || i === 23) {
uuid[i] = '-';
} else if (i === 14) {
uuid[i] = '4';
} else {
if (rnd <= 0x02) rnd = 0x2000000 + (Math.random() * 0x1000000) | 0;
r = rnd & 0xf;
rnd = rnd >> 4;
uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r];
}
}
return uuid.join('').toLowerCase();
}
});
{
// array of files we want to inject into the application on app_start
"javascript": [
"~/App_Plugins/ContentCopier/controller.js"
]
}
<div ng-controller="ContentCopierApp as vm">
<umb-box>
<umb-box-header title="Copy grid content from one language to another" />
<umb-box-content>
<div ng-repeat="(key, value) in vm.Editor.variants" style="margin-bottom: 20px;">
<button type="button" ng-click="vm.copyContent(key)">Copy from {{value.language.name}}</button>
</div>
<div style="margin-top: 40px;" ng-show="vm.contentCopied">
<span class="btn-success" style="display: inline-block; padding: 8px;">Done copying content</span>
</div>
</umb-box-content>
</umb-box>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment