Content deleted Content added
Convert left/right to dbl/single click |
refactor some ifs |
||
(23 intermediate revisions by the same user not shown) | |||
Line 43:
//Translate texts
translate() {
for (const trans of [
this.translations, window.ime_translations $('.ime_t_' + key).text(trans[key]);
},
std_handler(fct) {
return e => {
e.
fct(e);
};
},
Line 74 ⟶ 77:
makeDiv(id) {
const div = document.createElement('div');
return div;
},
Line 113 ⟶ 116:
};
const dblClick = event.detail > 1;
ime.store(dblClick);
}
Line 120 ⟶ 122:
//Highlighted areas
const areas = Array();
areas.current = function () { return this[
areas.make = function (shape) {
mouse.listen();
this.push({ shape: shape, coords: [], link: '' });
this.update();
return areas.current();
Line 130 ⟶ 132:
areas.update = function () {
this.updateSelection();
this.edit(
updateResult();
};
areas.updateSelection = function () {
this.selection.children().remove();
for (
const
' [' + area.shape + ']'; $('<option>', { value: i })
.text(title)
.prop({ selected: i ==
.appendTo(this.selection);
}
this.selection.prop('selectedIndex',
};
areas.edit = function (index) {
Line 149 ⟶ 152:
const area = this[index];
mouse.currentClicks = (area || {}).clicks || {};
if (area) {
$('#imeProps').toggle(true);
Line 165 ⟶ 168:
mouse.relax();
// Remove element from this array
this.splice(
this.update();
if (
this.edit(
};
areas.draw = function () { // this is where the magic is done.
function markPoint(point, color) {
if (point) {
context.beginPath();▼
const arm = 8 / ime.scale;
ime.context.
ime.context.
ime.context.
ime.context.
ime.context.
ime.context.
ime.context.
}
}
Line 189 ⟶ 191:
function drawPoly(coords) {
coords = coords.slice();
ime.context.moveTo(coords.shift(), coords.shift());
while (coords.length)
ime.context.lineTo(coords.shift(), coords.shift());
}
ime.context.clearRect(0, 0,
ime.context.canvas.width / ime.scale,
const current = ind == state.currentlyEditing;▼
ime.context.
ime.context.fillStyle =
'rgba(255,' + (current ? '255' : '0') + ',0,0.4)';
const coords = area.coords;
ime.context.beginPath();
switch (area.shape) {
case 'rect':
//Ignore spurious clicks outside
if (
drawPoly([
coords[0], coords[1], coords[0], coords[3], coords[2], coords[3], coords[2], coords[1] break;
case 'circle':
if (coords.length
//x,y,r,startAngle,endAngle
ime.context.arc(coords[0], coords[1], coords[2],
0, Math.PI * 2); break;
case 'poly':
Line 216 ⟶ 227:
break;
}
ime.context.closePath();
ime.context.fill();
if (current) {
ime.context.strokeStyle = 'red';
ime.context.stroke();
}
}
markPoint(mouse.currentClicks.left, 'red');
if (this.current() && this.current().shape != 'poly')
markPoint(mouse.currentClicks.right, 'yellow');
};
Line 242 ⟶ 253:
case 'rect':
if (d.left) {
area.coords[0] = d.left.x;
area.coords[1] = d.left.y; }
if (d.right) {
area.coords[2] = d.right.x;
area.coords[3] = d.right.y; }
break;
case 'circle':
if (leftClick) {
area.coords[0] = d.left.x;
area.coords[1] = d.left.y; }
if (full) {
dx = d.left.x - d.right.x, dy = d.left.y - d.right.y;
area.coords[2] = parseInt(Math.
}
break;
Line 268 ⟶ 283:
imports: {
make() {
for (const line of
document.ime.importText.value.split("\n")) { const detect = {
rect: /^rect +(\d+) +(\d+) +(\d+) +(\d+) +\[\[([^|]*)(|(.*))?\]\]/i,
Line 280 ⟶ 296:
area.coords = results.slice(1, 5);
area.link = results[5];
if (results[6])
area.title = results[6].substring(1); }
else if (results = detect.circ.exec(line)) {
Line 286 ⟶ 303:
area.coords = results.slice(1, 4);
area.link = results[4];
if (results[5])
area.title = results[5].substring(1); }
else if (results = detect.poly.exec(line)) {
Line 292 ⟶ 310:
area.coords = results[1].split(/\s+/);
area.link = results[2];
if (results[3])
area.title = results[3].substring(1); }
}
Line 310 ⟶ 329:
}
};
if (document.getElementById('file'))▼
/*
Initialization, part 1:
-Find image and
-Download info async via XMLHttpRequest. When complete...
*/
const a = util.findHyperlink(divFile);
if (!a) throw 'a';
Line 344 ⟶ 359:
$('<a>', { id: 'imeLink' })
.css({ display: 'block' })
.text('
.click(util.std_handler(
.appendTo('#file');
}
}
});
} catch (e) {
util.displayErr(
e + '" was missing'); }
}
/*
Initialization, part 2
-Hides the link and finally
-Adds the ImageMapEdit HTML.
*/
function
ime.trimUI();
Line 383 ⟶ 400:
img.id = 'imeImg';
img.style.border = 'none';
const handler = util.std_handler(e => e.stopPropagation());
img.oncontextmenu =
};▼
// Internet Explorer needs this differently
Line 406 ⟶ 421:
.attr({ width: $img.width(), height: $img.height() })
.appendTo(divImeContainer);
ime.context = ime.jcanvas[0].getContext("2d");
$.extend(ime.context, {
fillStyle: 'rgba(255,255,0,0.4)',
strokeStyle: 'red',
Line 414 ⟶ 429:
}
);
ime.context.scale(ime.scale, ime.scale);
ime.jcanvas.click(mouse.fired.bind(mouse));
▲ }
ime.jcanvas.click(handler);
ime.jcanvas[0].oncontextmenu = img.oncontextmenu = handler;
Line 437 ⟶ 448:
function updateResult() {
const
arr = Array.from(document.ime.imageDescriptionPos ▲ break;
▲ }
}▼
const results = Array().concat(
['<imagemap>',
mw.config.get('wgPageName') + '|' +
document.ime.imageDescription.value, ''],
areas.map(area =>
area.shape + ' ' + area.coords.join(' ') +
' [[' + area.link +
(area.title ? '|' + area.title : '') + ']]'), ['',
'desc ' + imageDescriptionPos,
Line 459 ⟶ 468:
const preResult = document.getElementById('imeResult');
while
preResult.removeChild(preResult.lastChild);
Line 466 ⟶ 475:
preResult.appendChild(document.createElement('br'));
}
}
function updateInputs(fromAreas) {
function updateShape(selectors, coords) {
for (
$(
}
Line 482 ⟶ 491:
switch (area.shape) {
case 'rect':
updateShape([
'#ime_areaRectLeft', '#ime_areaRectTop', '#ime_areaRectRight', '#ime_areaRectBottom' ], coords); break;
case 'circle':
updateShape([
'#ime_areaCircleX', '#ime_areaCircleY', '#ime_areaCircleRadius' ], coords); break;
case 'poly':
Line 496 ⟶ 514:
function attachTemplate() {
{const
clicks = { '.ime_t_rect': () => areas.make('rect'),
'.ime_t_circle': () => areas.make('circle'),
'.ime_t_poly': () => areas.make('poly'),
'.ime_t_deletearea': areas.remove.bind(areas),
'#imeImportShow': ime.imports.show.bind(ime.imports),
'#imeImportHide': ime.imports.hide.bind(ime.imports),
'.ime_t_import': ime.imports.make.bind(ime.imports),
'.ime_t_deletecoordinates': () => {
areas.current().coords = [];
mouse.currentClicks = {};
ime.store();
}
}
for (const id in clicks) $(id).click(utils.std_handler(clicks[id]));▼
areas.edit($(this).prop('selectedIndex'));
},
▲ '#ime_areaselector': function (e) { areas.edit($(this).prop('selectedIndex')); },
'.ime_saveonchange': ime.store.bind(ime),
'.ime-updateresultsonchange': updateResult,
'#ime_areaLink': function () {
areas.current().link = $(this).val(); updateResult(); },
'#ime_areaTitle': function () { areas.current().title = $(this).val(); updateResult(); }▼
'#ime_areaTitle': function () {
▲ };
for (const id in changes) $(id).change(changes[id]);▼
}
▲ };
for (const [id, click] of Object.entries(clicks))
$(id).change(change);
▲ }
areas.selection = $('#ime_areaselector');
$('.ime-saveonchange').focusout(function () {
const
input = $(this) if (coords &&
typeof (ind) == 'number' && typeof (val) == 'number' && !isNaN(val)) { coords[ind] = val;
mouse.currentClicks = {};
Line 532 ⟶ 563:
}
});
mw.loader.using(['mediawiki.api', 'jquery.ui']).done(
$('#ime_areaLink').autocomplete({
source(request, response) {
Line 550 ⟶ 581:
// this is the one-(very long)-line version of the template
//mw //in the comment... ime.templateHtml = '<form name="ime" action=""><fieldset style="margin:0.5ex;padding:0 1ex 1ex"><legend>ImageMapEdit</legend><fieldset style="float:left;margin:0 0.5ex 0.5ex;padding:0 1ex 1ex"><legend style="padding:0 0.5ex" class="ime_t_newarea">Create new area</legend><a style="padding:1px;background:white;color:darkblue" class="ime_t_rect">rect(angle)</a><br /><a style="padding:1px;background:white;color:darkblue" class="ime_t_circle">circle</a><br /><a style="padding:1px;background:white;color:darkblue" class="ime_t_poly">poly(gon)</a></fieldset><fieldset style="float:left;margin:0 0.5ex 0.5ex;padding:0 1ex 1ex"><legend style="padding:0 0.5ex" class="ime_t_editarea">Edit area</legend><select id="ime_areaselector" size="10" style="float:left;height:10em;margin:0.5ex;width:auto;min-width:18em;" ></select><div id="imeProps" style="float:left;margin:0.5ex;padding:0 1ex 1ex;display:none"><div style="float:left;margin:0.5ex;padding:0 1ex 1ex"><label for="ime_areaLink" class="ime_t_linktarget">Link target</label><br /><input id="ime_areaLink" name="areaLink" style="width:10em" class="ime-saveonchange" /><br /><label for="ime_areaTitle"><span class="ime_t_linktitle">Link title</span> (<span class="ime_t_optional">optional</span>)</label><br /><input id="ime_areaTitle" name="areaTitle" style="width:10em" class="ime-saveonchange" /></div><div id="imePropsRect" class="ime-prop ime-prop-rect" style="float:left;margin:0.5ex;padding:0 1ex 1ex;display:none"><label for="ime_areaRectLeft" class="ime_t_rectleft">First corner</label><br /><input id="ime_areaRectLeft" name="areaRectLeft" data-coord=0 class="ime-saveonchange" style="width:4em" /><input id="ime_areaRectTop" name="areaRectTop" data-coord=1 style="width:4em" class="ime-saveonchange" /><span class="ime_t_rectchoose1">Select with
});
/*
Line 588 ⟶ 621:
<input id="ime_areaRectTop" name="areaRectTop" data-coord=1 style="width:4em" class="ime-saveonchange" />
<span class="ime_t_rectchoose1">Select with
<br/>
<label for="ime_areaRectRight" class="ime_t_rectright">Second corner</label>
Line 594 ⟶ 627:
<input id="ime_areaRectRight" name="areaRectRight" data-coord=2 style="width:4em" class="ime-saveonchange" />
<input id="ime_areaRectBottom" name="areaRectBottom" data-coord=3 style="width:4em" class="ime-saveonchange" />
<span class="ime_t_rectchoose2">Select with
</div>
Line 602 ⟶ 635:
<input name="areaCircleX" id="ime_areaCircleX" class="ime-saveonchange" data-coord=0 style="width:4em" />
<input name="areaCircleY" id="ime_areaCircleY" style="width:4em" data-coord=1 class="ime-saveonchange" />
<span class="ime_t_circlechoose1">Select with
<br />
Line 608 ⟶ 641:
<br />
<input id="ime_areaCircleRadius" name="areaCircleRadius" data-coord=2 style="width:4em" class="ime-saveonchange" />
<span class="ime_t_circlechoose2">Select with
</div>
<div id="imePropsPoly" class="ime-prop ime-prop-poly" style="float:left;margin:0.5ex;padding:0 1ex 1ex;display:none">
Line 617 ⟶ 650:
<a style="padding:1px;background:white;color:darkblue" class="ime_t_deletecoordinates">Delete all coordinates</a>
<br />
<span class="ime_t_polychoose">Add new corner with
</div>
<div style="clear:both"></div>
|