Skip to content

Commit

Permalink
Inbox merging #2
Browse files Browse the repository at this point in the history
  • Loading branch information
it-spiderman committed May 14, 2024
1 parent 8719596 commit d1ae96d
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 30 deletions.
3 changes: 2 additions & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,11 @@
},
"ext.DataAccounting.inbox.compare": {
"scripts": [
"ui/Inbox/ConflictResolution.js",
"ui/Inbox/ComparePanel.js",
"ui/Inbox/TreePanel.js",
"ui/Inbox/TreeNode.js",
"ui/Inbox/CombinedTreeNode.js",
"ui/Inbox/ResolutionNode.js",
"ext.DataAccounting.inbox.compare.js"
],
"styles": [
Expand Down
24 changes: 22 additions & 2 deletions modules/ui/Inbox/ComparePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ da.ui.ComparePanel.prototype.initialize = function () {
this.$element.append( this.treePanel.$element );
this.treePanel.initialize();

var $diff = $( '#da-specialinbox-compare-diff' );
if ( $diff.length ) {
this.resolution = new da.ui.ConflictResolution( {}, $diff );
this.resolution.connect( this, {
allResolved: function () {
var importSubmitButton = this.$form.find( 'button[name=import]' );
importSubmitButton.prop( 'disabled', false );
console.log( this.resolution.getFinalText() );
}
} );
this.$element.append( this.resolution.$element );
}

this.setFormData();
};

Expand Down Expand Up @@ -52,7 +65,7 @@ da.ui.ComparePanel.prototype.setFormData = function () {
} ),
option2 = new OO.ui.ButtonOptionWidget( {
data: 'remote',
label: 'Use remote version (discard local)',
label: 'Use remote version (merge remote into local)',
flags: [ 'progressive' ]
} ),
option3 = new OO.ui.ButtonOptionWidget( {
Expand All @@ -67,14 +80,21 @@ da.ui.ComparePanel.prototype.setFormData = function () {

buttonSelect.connect( this, {
select: function( item ) {
if ( this.resolution ) {
this.resolution.setVisibility( false );
}
this.treePanel.showCombinedNode( false );
this.treePanel.deselectBranches();
if ( item.getData() === 'remote' ) {
this.treePanel.selectBranch( 'remote' );
this.treePanel.showCombinedNode( true, 'remote' );
} else if ( item.getData() === 'local' ) {
this.treePanel.selectBranch( 'local' );
} else if ( item.getData() === 'merge' ) {
this.treePanel.showCombinedNode( true );
this.treePanel.showCombinedNode( true, 'combined' );
if ( this.resolution ) {
this.resolution.setVisibility( true );
}
}
}
} );
Expand Down
183 changes: 183 additions & 0 deletions modules/ui/Inbox/ConflictResolution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
da = window.da || {};
da.ui = window.da.ui || {};

da.ui.ConflictResolution = function ( config, $diff ) {
config = config || {};

da.ui.ConflictResolution.super.call( this, $.extend( { expanded: false }, config ) );

this.$diff = $diff;
this.diffData = this.$diff.data( 'diff' );
this.neededResolutions = this.getNumberOfResolutionsNeeded();
this.allResolved = false;
this.resolved = {};
this.$element.html( this.$diff );
this.setVisibility( false );
this.finalText = '';
this.$element.addClass( 'da-conflict-resolution' );
this.initialize();
};

OO.inheritClass( da.ui.ConflictResolution, OO.ui.PanelLayout );

da.ui.ConflictResolution.prototype.initialize = function () {
this.$element.prepend( new OO.ui.LabelWidget( {
label: 'Merge diverging versions of the page',
classes: [ 'da-conflict-resolution-title' ]
} ).$element );

this.$diff.children( '#da-diff' ).children( '.da-diff-add, .da-diff-delete, .da-diff-change' )
.each( function ( index, element ) {
var $element = $( element ),
id = $element.data( 'diff-id' ),
type = $element.data( 'diff' );
if ( this.diffData.hasOwnProperty( id ) ) {
this.addButtons( $element, id, type );
}
}.bind( this ) );
};

da.ui.ConflictResolution.prototype.setVisibility = function ( visibile ) {
if ( visibile ) {
this.$element.show();
} else {
this.$element.hide();
}
};

da.ui.ConflictResolution.prototype.addButtons = function ( $element, id, type ) {
var changeSelector;
if ( type === 'change' ) {
changeSelector = new OO.ui.ButtonSelectWidget( {
classes: [ 'da-conflict-resolution-buttons' ],
items: [
new OO.ui.ButtonOptionWidget( {
label: 'use ours',
framed: false,
data: { change: id, type: 'local' },
} ),
new OO.ui.ButtonOptionWidget( {
label: 'use theirs',
framed: false,
data: { change: id, type: 'remote' },
} ),
new OO.ui.ButtonOptionWidget( {
label: 'use both',
framed: false,
data: { change: id, type: 'both' },
} ),
new OO.ui.ButtonOptionWidget( {
label: 'use neither',
framed: false,
data: { change: id, type: 'neither' },
} )
]
} );
} else {
changeSelector = new OO.ui.ButtonSelectWidget( {
classes: [ 'da-conflict-resolution-buttons' ],
items: [
new OO.ui.ButtonOptionWidget( {
label: 'accept',
framed: false,
data: { change: id, type: 'accept' },
} ),
new OO.ui.ButtonOptionWidget( {
label: 'reject',
framed: false,
data: { change: id, type: 'reject' },
} )
]
} );
}

changeSelector.connect( this, {
select: 'onSelect'
} );
$element.append( changeSelector.$element );
};

da.ui.ConflictResolution.prototype.onSelect = function ( item ) {
if ( !item ) {
return;
}
var $block = this.$diff.find( '[data-diff-id=' + item.data.change + ']' );
if ( !$block.length ) {
return;
}
$block.addClass( 'da-diff-resolved' );
$block.removeClass( function( index, className ) {
return (className.match(/(^|\s)da-diff-resolution-\S+/g) || []).join(' ');
} );

$block.addClass( 'da-diff-resolution-' + item.data.type );

this.resolved[item.data.change] = item.data.type;
if ( Object.keys( this.resolved ).length === this.neededResolutions ) {
this.allResolved = true;
this.emit( 'allResolved' );
}
};

da.ui.ConflictResolution.prototype.getNumberOfResolutionsNeeded = function () {
var needed = 0;
for ( var id in this.diffData ) {
if ( !this.diffData.hasOwnProperty( id ) ) {
continue;
}
if ( this.diffData[id].type !== 'copy' ) {
needed++;
}
}
return needed;
};

da.ui.ConflictResolution.prototype.getFinalText = function () {
if ( !this.allResolved ) {
throw new Error( 'Not all resolutions have been made' );
}
var finalText = [];
console.log( this.diffData, this.resolved );
for ( var id in this.diffData ) {
if ( !this.diffData.hasOwnProperty( id ) ) {
continue;
}
if ( this.diffData[id].type === 'copy' ) {
finalText.push( this.diffData[id].old );
}
if ( this.diffData[id].type === 'change' ) {
if ( this.resolved.hasOwnProperty( id ) ) {
if ( this.resolved[id] === 'local' ) {
finalText.push( this.diffData[id].old );
}
if ( this.resolved[id] === 'remote' ) {
finalText.push( this.diffData[id].new );
}
if ( this.resolved[id] === 'both' ) {
finalText.push( this.diffData[id].old )
finalText.push( this.diffData[id].new );
}
}
}
if ( this.diffData[id].type === 'add' ) {
if ( this.resolved.hasOwnProperty( id ) ) {
if ( this.resolved[id] === 'accept' ) {
finalText.push( this.diffData[id].new );
}
}
}
if ( this.diffData[id].type === 'delete' ) {
if ( this.resolved.hasOwnProperty( id ) ) {
if ( this.resolved[id] === 'reject' ) {
finalText.push( this.diffData[id].old );
}
}
}
}

// Filter out empty strings
finalText = finalText.filter( function( text ) {
return text !== '';
} );
return finalText.join( '\n' );
};
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
da = window.da || {};
da.ui = window.da.ui || {};

da.ui.CombinedTreeNode = function ( data ) {
da.ui.CombinedTreeNode.super.call( this, '-1', { source: 'combined', parents: { local: data.local, remote: data.remote } } );
da.ui.ResolutionNode = function ( data ) {
da.ui.ResolutionNode.super.call( this, '-1', { source: 'combined', parents: { local: data.local, remote: data.remote } } );
};

OO.inheritClass( da.ui.CombinedTreeNode, da.ui.TreeNode );
OO.inheritClass( da.ui.ResolutionNode, da.ui.TreeNode );

da.ui.CombinedTreeNode.prototype.makeGraphPart = function () {
da.ui.ResolutionNode.prototype.makeGraphPart = function () {
this.makeRelevantNode();
this.$element.append( this.$relevantNode );
this.$element.append( $( '<span>' ).addClass( 'da-compare-node-graph da-compare-node-graph-placeholder' ) );
};

da.ui.CombinedTreeNode.prototype.makeRelevantNode = function () {
da.ui.ResolutionNode.prototype.makeRelevantNode = function () {
var parents = '';
console.log( this.nodeData );
if ( this.nodeData.parents.local ) {
Expand All @@ -27,10 +27,10 @@ da.ui.CombinedTreeNode.prototype.makeRelevantNode = function () {
.attr( 'parent', parents );
};

da.ui.CombinedTreeNode.prototype.makeLabel = function () {
da.ui.ResolutionNode.prototype.makeLabel = function () {
// NOOP
};

da.ui.CombinedTreeNode.prototype.getType = function () {
return 'combined';
da.ui.ResolutionNode.prototype.getType = function () {
return 'resolution';
};
6 changes: 4 additions & 2 deletions modules/ui/Inbox/TreePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ da.ui.TreePanel.prototype.selectBranch = function ( branch ) {
this.connect();
};

da.ui.TreePanel.prototype.showCombinedNode = function ( show ) {
da.ui.TreePanel.prototype.showCombinedNode = function ( show, type ) {
this.deselectBranches( false );
if ( show ) {
this.addNode( new da.ui.CombinedTreeNode( this.lastParents ) );
var parents = type === 'combined' ?
this.lastParents : { local: null, remote: this.lastParents.remote };
this.addNode( new da.ui.ResolutionNode( parents ) );
} else {
if ( this.nodes.hasOwnProperty( '-1' ) ) {
this.nodes[ '-1' ].$element.remove();
Expand Down
Loading

0 comments on commit d1ae96d

Please sign in to comment.