1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 14:00:56 +01:00
phorge-phorge/webroot/rsrc/js/application/differential/behavior-edit-inline-comments.js

266 lines
6.5 KiB
JavaScript
Raw Normal View History

/**
* @provides javelin-behavior-differential-edit-inline-comments
* @requires javelin-lib-dev
*/
JX.behavior('differential-edit-inline-comments', function(config) {
var selecting = false;
var reticle = JX.$N('div', {className: 'differential-reticle'});
JX.DOM.hide(reticle);
document.body.appendChild(reticle);
var origin = null;
var target = null;
var root = null;
var changeset = null;
var workflow = false;
var is_new = false;
function updateReticle() {
var top = origin;
var bot = target;
if (JX.$V(top).y > JX.$V(bot).y) {
var tmp = top;
top = bot;
bot = tmp;
}
var code = target.nextSibling;
var pos = JX.$V(top).add(1 + JX.$V.getDim(target).x, 0);
var dim = JX.$V.getDim(code).add(-4, 0);
dim.y = (JX.$V(bot).y - pos.y) + JX.$V.getDim(bot).y;
pos.setPos(reticle);
dim.setDim(reticle);
JX.DOM.show(reticle);
}
function hideReticle() {
JX.DOM.hide(reticle);
}
function finishSelect() {
selecting = false;
workflow = false;
hideReticle();
}
function drawInlineComment(table, anchor, r) {
copyRows(table, JX.$N('div', JX.HTML(r.markup)), anchor);
finishSelect();
}
2011-02-02 01:42:36 +01:00
function isOnRight(node) {
return node.parentNode.firstChild != node;
}
2011-02-02 22:48:52 +01:00
2011-02-02 01:42:36 +01:00
function isNewFile(node) {
var data = JX.Stratcom.getData(root);
return isOnRight(node) || (data.left != data.right);
}
function getRowNumber(th_node) {
try {
return parseInt(th_node.id.match(/^C\d+[ON]L(\d+)$/)[1], 10);
} catch (x) {
return undefined;
}
}
function isInlineCommentNode(target) {
return target &&
(!JX.DOM.isType(target, 'tr')
|| target.className.indexOf('inline') !== -1);
}
function findInlineCommentTarget(target) {
while (isInlineCommentNode(target)) {
target = target.nextSibling;
}
return target;
}
JX.Stratcom.listen(
'mousedown',
['differential-changeset', 'tag:th'],
function(e) {
if (workflow ||
selecting ||
getRowNumber(e.getTarget()) === undefined) {
return;
}
selecting = true;
root = e.getNode('differential-changeset');
origin = target = e.getTarget();
var data = e.getNodeData('differential-changeset');
2011-02-02 01:42:36 +01:00
if (isOnRight(target)) {
changeset = data.left;
} else {
2011-02-02 01:42:36 +01:00
changeset = data.right;
}
updateReticle();
e.kill();
});
JX.Stratcom.listen(
'mouseover',
['differential-changeset', 'tag:th'],
function(e) {
if (!selecting ||
workflow ||
(getRowNumber(e.getTarget()) === undefined) ||
2011-02-02 01:42:36 +01:00
(isOnRight(e.getTarget()) != isOnRight(origin)) ||
(e.getNode('differential-changeset') !== root)) {
return;
}
target = e.getTarget();
updateReticle();
});
JX.Stratcom.listen(
'mouseup',
null,
function(e) {
if (workflow || !selecting) {
return;
}
var o = getRowNumber(origin);
var t = getRowNumber(target);
var insert;
var len;
if (t < o) {
len = (o - t);
o = t;
insert = origin.parentNode;
} else {
len = (t - o);
insert = target.parentNode;
}
var data = {
op: 'new',
changeset: changeset,
number: o,
length: len,
2011-02-02 01:42:36 +01:00
is_new: isNewFile(target) ? 1 : 0,
2011-02-02 22:48:52 +01:00
on_right: isOnRight(target) ? 1 : 0
};
workflow = true;
var w = new JX.Workflow(config.uri, data)
.setHandler(function(r) {
// Skip over any rows which contain inline feedback. Don't mimic this!
// We're shipping around raw HTML here for performance reasons, but
// normally you should use sigils to encode this kind of data on
// the document.
var target = findInlineCommentTarget(insert.nextSibling);
drawInlineComment(insert.parentNode, target, r);
finishSelect();
2011-02-02 01:42:36 +01:00
JX.Stratcom.invoke('differential-inline-comment-update');
})
.setCloseHandler(finishSelect);
w.listen('error', function(e) {
// TODO: uh, tell the user I guess
finishSelect();
JX.Stratcom.context().stop();
});
w.start();
e.kill();
});
JX.Stratcom.listen(
['mouseover', 'mouseout'],
2011-02-02 01:42:36 +01:00
'differential-inline-comment',
function(e) {
if (selecting || workflow) {
return;
}
if (e.getType() == 'mouseout') {
hideReticle();
} else {
root = e.getNode('differential-changeset');
2011-02-02 01:42:36 +01:00
var data = e.getNodeData('differential-inline-comment');
var change = e.getNodeData('differential-changeset');
2011-02-02 01:42:36 +01:00
var prefix;
if (data.on_right) {
prefix = 'C' + (change.left) + 'NL';
} else {
2011-02-02 01:42:36 +01:00
prefix = 'C' + (change.right) + 'OL';
}
origin = JX.$(prefix + data.number);
2011-02-02 01:42:36 +01:00
target = JX.$(prefix + (parseInt(data.number, 10) +
parseInt(data.length, 10)));
updateReticle();
}
});
JX.Stratcom.listen(
'click',
[['differential-inline-comment', 'differential-inline-reply']],
function(e) {
new JX.Workflow(config.uri, e.getNodeData('differential-inline-reply'))
.setHandler(function(r) {
var base_row =
findInlineCommentTarget(
e.getNode('differential-inline-comment')
.parentNode
.parentNode
);
drawInlineComment(base_row.parentNode, base_row, r);
JX.Stratcom.invoke('differential-inline-comment-update');
})
.start();
e.kill();
}
);
JX.Stratcom.listen(
'click',
2011-02-02 19:10:25 +01:00
[['differential-inline-comment', 'differential-inline-delete'],
['differential-inline-comment', 'differential-inline-edit']],
function(e) {
var data = {
2011-02-02 19:10:25 +01:00
op: e.getNode('differential-inline-edit') ? 'edit' : 'delete',
id: e.getNodeData('differential-inline-comment').id,
on_right: e.getNodeData('differential-inline-comment').on_right,
};
new JX.Workflow(config.uri, data)
.setHandler(function(r) {
2011-02-02 01:42:36 +01:00
var base_row = e.getNode('differential-inline-comment')
.parentNode
.parentNode;
if (data.op == 'edit' && r.markup) {
drawInlineComment(base_row.parentNode, base_row, r);
}
JX.DOM.remove(base_row);
JX.Stratcom.invoke('differential-inline-comment-update');
})
.start();
e.kill();
});
});