blob: 294353d94b3802b7c36a72a2ebcb64abb74d4b99 [file] [log] [blame]
avm9996304def3e2016-11-27 22:53:05 +01001// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
2// source code is governed by a BSD-style license that can be found in the
3// LICENSE file.
4
5function isHighVersion() {
6 var version = navigator.userAgent.match(/Chrome\/(\d+)/)[1];
7 return version > 9;
8}
9
10function $(id) {
11 return document.getElementById(id);
12}
13function i18nReplace(id, messageKey) {
14 return $(id).innerHTML = chrome.i18n.getMessage(messageKey);
15}
16UploadUI.init();
17
18var bg = chrome.extension.getBackgroundPage();
19var canvas = new Canvas();
20var photoshop = {
21 canvas: document.createElement("canvas"),
22 tabTitle: '',
23 startX: 0,
24 startY: 0,
25 endX: 0,
26 endY: 0,
27 dragFlag: false,
28 flag: 'rectangle',
29 layerId: 'layer0',
30 canvasId: '',
31 color: '#ff0000',
32 highlightColor: '',
33 lastValidAction: 0,
34 markedArea: [],
35 isDraw: true,
36 offsetX: 0,
37 offsetY: 36,
38 nowHeight: 0,
39 nowWidth: 0,
40 highlightType: 'border',
41 highlightMode: 'rectangle',
42 text: '',
43
44 i18nReplace: i18nReplace,
45
46 initCanvas: function() {
47 $('canvas').width = $('mask-canvas').width = $('photo').style.width =
48 photoshop.canvas.width = bg.screenshot.canvas.width;
49 $('canvas').height = $('mask-canvas').height = $('photo').style.height =
50 photoshop.canvas.height = bg.screenshot.canvas.height;
51 var context = photoshop.canvas.getContext('2d');
52 context.drawImage(bg.screenshot.canvas, 0, 0);
53 context = $('canvas').getContext('2d');
54 context.drawImage(photoshop.canvas, 0, 0);
55 $('canvas').style.display = 'block';
56 },
57
58 init: function() {
59 photoshop.initTools();
60 photoshop.initCanvas();
61 photoshop.tabTitle = bg.screenshot.tab.title;
62 var showBoxHeight = function() {
63 $('showBox').style.height = window.innerHeight - photoshop.offsetY - 1;
64 }
65 setTimeout(showBoxHeight, 50);
66 },
67
68 markCurrentElement: function(element) {
69 if (element && element.parentNode) {
70 var children = element.parentNode.children;
71 for (var i = 0; i < children.length; i++) {
72 var node = children[i];
73 if (node == element) {
74 element.className = 'mark';
75 } else {
76 node.className = '';
77 }
78 }
79 }
80 },
81
82 setHighLightMode: function() {
83 photoshop.highlightType = localStorage.highlightType || 'border';
84 photoshop.color = localStorage.highlightColor || '#FF0000';
85 $(photoshop.layerId).style.border = '2px solid ' + photoshop.color;
86 if (photoshop.highlightType == 'rect') {
87 $(photoshop.layerId).style.backgroundColor = photoshop.color;
88 $(photoshop.layerId).style.opacity = 0.5;
89 }
90 if (photoshop.flag == 'rectangle') {
91 $(photoshop.layerId).style.borderRadius = '0 0';
92 } else if (photoshop.flag == 'radiusRectangle') {
93 $(photoshop.layerId).style.borderRadius = '6px 6px';
94 } else if (photoshop.flag == 'ellipse') {
95 $(photoshop.layerId).style.border = '0';
96 $(photoshop.layerId).style.backgroundColor = '';
97 $(photoshop.layerId).style.opacity = 1;
98 }
99
100 },
101
102 setBlackoutMode: function() {
103 photoshop.color = '#000000';
104 $(photoshop.layerId).style.opacity = 1;
105 $(photoshop.layerId).style.backgroundColor = '#000000';
106 $(photoshop.layerId).style.border = '2px solid #000000';
107 },
108
109 setTextMode: function() {
110 localStorage.fontSize = localStorage.fontSize || '16';
111 photoshop.color = localStorage.fontColor =
112 localStorage.fontColor || '#FF0000';
113 $(photoshop.layerId).setAttribute('contentEditable', true);
114 $(photoshop.layerId).style.border = '1px dotted #333333';
115 $(photoshop.layerId).style.cursor = 'text';
116 $(photoshop.layerId).style.lineHeight = localStorage.fontSize + 'px';
117 $(photoshop.layerId).style.fontSize = localStorage.fontSize + 'px';
118 $(photoshop.layerId).style.color = photoshop.color;
119 $(photoshop.layerId).innerHTML = '<br/>';
120 var layer = $(photoshop.layerId);
121 var id = photoshop.layerId;
122 layer.addEventListener('blur', function() {
123 photoshop.setTextToArray(id);
124 }, true);
125 layer.addEventListener('click', function() {
126 this.style.border = '1px dotted #333333';
127 }, true);
128 layer.addEventListener('mouseout', function() {
129 if (!photoshop.dragFlag) {
130 this.style.borderWidth = 0;
131 }
132 }, false);
133 layer.addEventListener('mousemove', function() {
134 this.style.border = '1px dotted #333333';
135 }, false);
136 },
137
138 setTextToArray: function(id) {
139 var str = $(id).innerText.split("\n");
140 if (photoshop.markedArea.length > 0) {
141 for (var i = photoshop.markedArea.length - 1; i >= 0; i--) {
142 if (photoshop.markedArea[i].id == id) {
143 photoshop.markedArea[i].context = str;
144 break;
145 }
146 }
147 $(id).style.borderWidth = 0;
148 }
149 },
150
151 openOptionPage: function() {
152 chrome.tabs.create({url: chrome.extension.getURL("options.html")});
153 },
154
155 closeCurrentTab: function() {
156 chrome.tabs.getSelected(null, function(tab) {
157 chrome.tabs.remove(tab.id);
158 });
159 },
160
161 finish: function() {
162 var context = $('canvas').getContext('2d');
163 context.drawImage(photoshop.canvas, 0, 0);
164 },
165
166 colorRgba: function(color, opacity) {
167 var sColor = color.toLowerCase();
168 var sColorChange = [];
169 for (var i = 1; i < sColor.length; i += 2) {
170 sColorChange.push(parseInt("0x" + sColor.slice(i,i + 2)));
171 }
172 return "rgba(" + sColorChange.join(",") + "," + opacity + ")";
173 },
174
175 /**
176 * Undo the current operation
177 */
178 toDo: function(element, what) {
179 photoshop.flag = what;
180 photoshop.isDraw = true;
181 photoshop.markCurrentElement(element);
182 },
183
184 setDivStyle: function(x, y) {
185 $(photoshop.layerId).setAttribute("style", "");
186 $(photoshop.layerId).setAttribute("contentEditable", false);
187 switch(photoshop.flag) {
188 case 'rectangle':
189 case 'radiusRectangle':
190 case 'ellipse':
191 photoshop.setHighLightMode();
192 break;
193 case 'redact':
194 photoshop.setBlackoutMode();
195 break;
196 case 'text':
197 photoshop.setTextMode();
198 break;
199 case 'line':
200 case 'arrow':
201 photoshop.drawLineOnMaskCanvas(x, y, x, y, 'lineStart',
202 photoshop.layerId);
203 break;
204 case 'blur':
205 photoshop.createCanvas(photoshop.layerId);
206 break;
207 }
208 },
209
210 /**
211 * Create a layer and set style
212 */
213 createDiv: function() {
214 photoshop.lastValidAction++;
215 photoshop.layerId = 'layer' + photoshop.lastValidAction;
216 if ($(photoshop.layerId)) {
217 photoshop.removeElement(photoshop.layerId);
218 }
219 var divElement = document.createElement('div');
220 divElement.id = photoshop.layerId;
221 divElement.className = 'layer';
222 $('photo').appendChild(divElement);
223 if (photoshop.flag == 'blur') {
224 photoshop.createCanvas(photoshop.layerId);
225 }
226 return divElement;
227 },
228
229 createCanvas: function(parentId) {
230 photoshop.canvasId = 'cav-' + parentId;
231 if (!$(photoshop.canvasId)) {
232 var cav = document.createElement('canvas');
233 cav.id = photoshop.canvasId;
234 cav.width = 10;
235 cav.height = 10;
236 $(photoshop.layerId).appendChild(cav);
237 return cav;
238 }
239 return $(photoshop.canvasId);
240
241 },
242
243 createCloseButton: function(parent, id, left, top, flag) {
244 var imgElement = document.createElement('img');
245 imgElement.id = id;
246 imgElement.src = 'images/cross.png';
247 imgElement.className = 'closeButton';
248 imgElement.style.left = left - 15 + 'px';
249 if (photoshop.flag == 'line' || photoshop.flag == 'arrow') {
250 imgElement.style.left = left / 2 - 5 + 'px';
251 imgElement.style.top = top / 2 - 5 + 'px';
252 }
253 imgElement.onclick = function() {
254 $(parent).style.display = 'none';
255 photoshop.removeLayer(parent);
256 };
257 $(parent).onmousemove = function() {
258 if (!photoshop.dragFlag) {
259 photoshop.showCloseButton(id);
260 $(parent).style.zIndex = 110;
261 photoshop.isDraw = (flag == 'text' ? false : photoshop.isDraw);
262 }
263 };
264 $(parent).onmouseout = function() {
265 photoshop.hideCloseButton(id);
266 $(parent).style.zIndex = 100;
267 photoshop.isDraw = true;
268 };
269 $(parent).appendChild(imgElement);
270 return imgElement;
271 },
272
273 showCloseButton: function(id) {
274 $(id).style.display = 'block';
275 },
276
277 hideCloseButton: function(id) {
278 $(id).style.display = 'none';
279 photoshop.isDraw = true;
280 },
281
282 removeLayer: function(id) {
283 for (var i = 0; i < photoshop.markedArea.length; i++) {
284 if (photoshop.markedArea[i].id == id) {
285 photoshop.markedArea.splice(i, 1);
286 break;
287 }
288 }
289 photoshop.removeElement(id);
290 },
291
292 /**
293 * Set the starting point(x,y) when mouse pressed
294 */
295 onMouseDown: function(event) {
296 if (photoshop.isDraw && event.button != 2) {
297 photoshop.startX = event.pageX + $('showBox').scrollLeft -
298 photoshop.offsetX;
299 photoshop.startY = event.pageY + $('showBox').scrollTop -
300 photoshop.offsetY;
301 photoshop.setDivStyle(photoshop.startX, photoshop.startY);
302 photoshop.dragFlag = true;
303
304 $(photoshop.layerId).style.left = photoshop.startX + 'px';
305 $(photoshop.layerId).style.top = photoshop.startY + 'px';
306 $(photoshop.layerId).style.height = 0;
307 $(photoshop.layerId).style.width = 0;
308 $(photoshop.layerId).style.display = 'block';
309 }
310 },
311
312 onMouseUp: function(event) {
313 $('mask-canvas').style.zIndex = 10;
314 photoshop.endX = event.pageX + $('showBox').scrollLeft -
315 photoshop.offsetX;
316 if (photoshop.endX > photoshop.canvas.width) {
317 photoshop.endX = photoshop.canvas.width ;
318 }
319
320 if (photoshop.endX < 0) {
321 photoshop.endX = 0;
322 }
323
324 photoshop.endY = event.pageY + $('showBox').scrollTop -
325 photoshop.offsetY;
326 if (photoshop.endY > photoshop.canvas.height) {
327 photoshop.endY = photoshop.canvas.height ;
328 }
329 if (photoshop.endY < 0) {
330 photoshop.endY = 0;
331 }
332 if (photoshop.isDraw && photoshop.dragFlag && (photoshop.endX !=
333 photoshop.startX || photoshop.endY != photoshop.startY)) {
334 if (photoshop.flag == 'line' || photoshop.flag == 'arrow') {
335 photoshop.drawLineOnMaskCanvas(photoshop.startX, photoshop.startY,
336 photoshop.endX, photoshop.endY, 'drawEnd', photoshop.layerId);
337 } else if (photoshop.flag == 'blur') {
338 canvas.blurImage(photoshop.canvas, $(photoshop.canvasId),
339 photoshop.layerId, photoshop.startX, photoshop.startY,
340 photoshop.endX, photoshop.endY);
341 } else if (photoshop.flag == 'ellipse') {
342 photoshop.drawEllipseOnMaskCanvas(photoshop.endX,
343 photoshop.endY, 'end', photoshop.layerId);
344 }
345 photoshop.markedArea.push({
346 'id': photoshop.layerId,
347 'startX': photoshop.startX,
348 'startY': photoshop.startY,
349 'endX': photoshop.endX,
350 'endY': photoshop.endY,
351 'width': photoshop.nowWidth,
352 'height': photoshop.nowHeight,
353 'flag': photoshop.flag,
354 'highlightType': photoshop.highlightType,
355 'fontSize': localStorage.fontSize,
356 'color': photoshop.color,
357 'context': ''
358 });
359 $(photoshop.layerId).focus();
360 var imageBtnId = 'close_' + photoshop.layerId;
361 photoshop.createCloseButton(photoshop.layerId, imageBtnId,
362 photoshop.nowWidth, photoshop.nowHeight, photoshop.flag);
363 photoshop.createDiv();
364 } else if (photoshop.endX ==
365 photoshop.startX && photoshop.endY == photoshop.startY) {
366 photoshop.removeElement(photoshop.layerId);
367 photoshop.createDiv();
368 }
369 photoshop.dragFlag = false;
370
371 },
372
373 /**
374 * Refresh div‘s height and width when the mouse move
375 */
376 onMouseMove: function(event) {
377 if(photoshop.dragFlag) {
378 $('mask-canvas').style.zIndex = 200;
379 photoshop.endX = event.pageX + $('showBox').scrollLeft -
380 photoshop.offsetX;
381 if (photoshop.endX > photoshop.canvas.width) {
382 photoshop.endX = photoshop.canvas.width ;
383 }
384
385 if (photoshop.endX < 0) {
386 photoshop.endX = 0;
387 }
388
389 photoshop.endY = event.pageY + $('showBox').scrollTop -
390 photoshop.offsetY;
391 if (photoshop.endY > photoshop.canvas.height) {
392 photoshop.endY = photoshop.canvas.height ;
393 }
394 if (photoshop.endY < 0) {
395 photoshop.endY = 0;
396 }
397 photoshop.nowHeight = photoshop.endY - photoshop.startY - 1 ;
398 photoshop.nowWidth = photoshop.endX - photoshop.startX - 1 ;
399
400 if(photoshop.nowHeight < 0) {
401 $(photoshop.layerId).style.top = photoshop.endY + 'px';
402 photoshop.nowHeight = -1 * photoshop.nowHeight;
403 }
404 if(photoshop.nowWidth < 0) {
405 $(photoshop.layerId).style.left = photoshop.endX + 'px';
406 photoshop.nowWidth = -1 * photoshop.nowWidth;
407 }
408
409 $(photoshop.layerId).style.height = photoshop.nowHeight - 3;
410 $(photoshop.layerId).style.width = photoshop.nowWidth - 3;
411 if (photoshop.flag == 'line' || photoshop.flag == 'arrow') {
412 photoshop.drawLineOnMaskCanvas(photoshop.startX, photoshop.startY,
413 photoshop.endX, photoshop.endY, 'lineDrawing', photoshop.layerId);
414 } else if (photoshop.flag == 'blur') {
415 $(photoshop.layerId).style.height = photoshop.nowHeight ;
416 $(photoshop.layerId).style.width = photoshop.nowWidth ;
417 canvas.blurImage(photoshop.canvas, $(photoshop.canvasId),
418 photoshop.layerId, photoshop.startX, photoshop.startY,
419 photoshop.endX, photoshop.endY);
420 } else if (photoshop.flag == 'ellipse') {
421 photoshop.drawEllipseOnMaskCanvas(photoshop.endX,
422 photoshop.endY, 'drawing', photoshop.layerId);
423 }
424 }
425
426 },
427
428 /**
429 * Remove a div
430 */
431 removeElement: function(id) {
432 if($(id)) {
433 $(id).parentNode.removeChild($(id));
434 }
435 },
436
437 /**
438 * Use fillStyle, fillText and fillRect functions to draw rectangles,
439 * and render to canvas
440 */
441 draw: function() {
442 var context = $('canvas').getContext('2d');
443 for (var j = 0; j < photoshop.markedArea.length; j++) {
444 var mark = photoshop.markedArea[j];
445 var x = (mark.startX < mark.endX) ? mark.startX : mark.endX;
446 var y = (mark.startY < mark.endY) ? mark.startY : mark.endY;
447 var width = mark.width;
448 var height = mark.height;
449 var color = mark.color;
450 switch(mark.flag) {
451 case 'rectangle':
452 if (mark.highlightType == 'border') {
453 canvas.drawStrokeRect(context, color, x, y, width, height, 2);
454 } else {
455 var color = changeColorToRgba(color, 0.5);
456 canvas.drawFillRect(context, color, x, y, width, height);
457 }
458 break;
459 case 'radiusRectangle':
460 canvas.drawRoundedRect(
461 context, color, x, y, width, height, 6, mark.highlightType);
462 break;
463 case 'ellipse':
464 x = (mark.startX + mark.endX) / 2;
465 y = (mark.startY + mark.endY) / 2;
466 var xAxis = Math.abs(mark.endX - mark.startX) / 2;
467 var yAxis = Math.abs(mark.endY - mark.startY) / 2;
468 canvas.drawEllipse(
469 context, color, x, y, xAxis, yAxis, 3, mark.highlightType);
470 break;
471 case 'redact':
472 canvas.drawFillRect(context, color, x, y, width, height);
473 break;
474 case 'text':
475 for (var i = 0; i < mark.context.length; i++) {
476 canvas.setText(
477 context, mark.context[i], color, mark.fontSize, 'arial',
478 mark.fontSize, x, y + mark.fontSize * i, width);
479 }
480 break;
481 case 'blur':
482 var imageData = context.getImageData(
483 x, y, photoshop.markedArea[j].width,
484 photoshop.markedArea[j].height);
485 imageData = canvas.boxBlur(
486 imageData, photoshop.markedArea[j].width,
487 photoshop.markedArea[j].height, 10);
488 context.putImageData(
489 imageData, x, y, 0, 0, photoshop.markedArea[j].width,
490 photoshop.markedArea[j].height);
491 break;
492 case 'line':
493 canvas.drawLine(
494 context, color, 'round', 2,
495 mark.startX, mark.startY, mark.endX, mark.endY);
496 break;
497 case 'arrow':
498 canvas.drawArrow(
499 context, color, 2, 4, 10, 'round',
500 mark.startX, mark.startY, mark.endX, mark.endY);
501 break;
502 }
503 }
504 },
505
506 drawLineOnMaskCanvas: function(startX, startY, endX, endY, type, layerId) {
507 var ctx = $('mask-canvas').getContext('2d');
508 ctx.clearRect(0, 0, $('mask-canvas').width, $('mask-canvas').height);
509 if (type == 'drawEnd') {
510 var offset = 20;
511 var width = Math.abs(endX - photoshop.startX) > 0 ?
512 Math.abs(endX - photoshop.startX): 0;
513 var height = Math.abs(endY - photoshop.startY) > 0 ?
514 Math.abs(endY - photoshop.startY): 0;
515 var offsetLeft = parseInt($(layerId).style.left);
516 var offsetTop = parseInt($(layerId).style.top);
517 startX = startX - offsetLeft + offset / 2;
518 startY = startY - offsetTop + offset / 2;
519 endX = endX - offsetLeft + offset / 2;
520 endY = endY - offsetTop + offset / 2;
521 $(layerId).style.left = offsetLeft - offset / 2;
522 $(layerId).style.top = offsetTop - offset / 2;
523 var cavCopy = photoshop.createCanvas(layerId);
524 cavCopy.width = width + offset;
525 cavCopy.height = height + offset;
526 ctx = cavCopy.getContext('2d');
527 }
528 if (localStorage.lineType == 'line') {
529 canvas.drawLine(ctx, localStorage.lineColor, 'round', 2,
530 startX, startY, endX, endY);
531 } else {
532 canvas.drawArrow(ctx, localStorage.lineColor, 2, 4, 10, 'round',
533 startX, startY, endX, endY)
534 }
535
536 },
537
538 createColorPadStr: function(element, type) {
539 var colorList = ['#000000', '#0036ff', '#008000', '#dacb23', '#d56400',
540 '#c70000', '#be00b3', '#1e2188', '#0090ff', '#22cc01', '#ffff00',
541 '#ff9600', '#ff0000', '#ff008e', '#7072c3', '#49d2ff', '#9dff3d',
542 '#ffffff', '#ffbb59', '#ff6b6b', '#ff6bbd'];
543
544 var div = document.createElement("div");
545 div.id = "colorpad";
546 element.appendChild(div);
547
548 for(var i = 0; i < colorList.length; i++) {
549 var a = document.createElement("a");
550 var color = colorList[i];
551 a.id = color;
552 a.title = color;
553 a.style.backgroundColor = color;
554 if (color == '#ffffff') {
555 a.style.border = "1px solid #444";
556 a.style.width = "12px"
557 a.style.height = "12px";
558 }
559 a.addEventListener('click', function(e) {
560 photoshop.colorPadPick(e.target.id, type);
561 return false;
562 });
563 div.appendChild(a);
564 }
565 },
566
567 colorPadPick: function(color, type) {
568 photoshop.color = color;
569 if(type == 'highlight') {
570 localStorage.highlightColor = color;
571 photoshop.setHighlightColorBoxStyle(color);
572 } else if(type == 'text') {
573 localStorage.fontColor = color;
574 $('fontColorBox').style.backgroundColor = color;
575 } else if (type == 'line') {
576 localStorage.lineColor = color;
577 photoshop.setLineColorBoxStyle();
578 } else if (type == 'ellipse') {
579 $('ellipseBox').style.borderColor = color;
580 }
581 },
582
583 setHighlightColorBoxStyle: function(color) {
584 var highlightColorBox = $('highlightColorBox');
585 highlightColorBox.style.borderColor = color;
586 localStorage.highlightType = localStorage.highlightType || 'border';
587 if (localStorage.highlightType == 'border') {
588 highlightColorBox.style.background = '#ffffff';
589 highlightColorBox.style.opacity = 1;
590 $('borderMode').className = 'mark';
591 $('rectMode').className = '';
592 } else if (localStorage.highlightType == 'rect') {
593 highlightColorBox.style.background = color;
594 highlightColorBox.style.opacity = 0.5;
595 $('borderMode').className = '';
596 $('rectMode').className = 'mark';
597 }
598 if (photoshop.flag == 'rectangle') {
599 highlightColorBox.style.borderRadius = '0 0';
600 } else if (photoshop.flag == 'radiusRectangle') {
601 highlightColorBox.style.borderRadius = '3px 3px';
602 } else if (photoshop.flag == 'ellipse') {
603 highlightColorBox.style.borderRadius = '12px 12px';
604 }
605 photoshop.markCurrentElement($(photoshop.flag));
606 },
607
608 setBlackoutColorBoxStyle: function() {
609 localStorage.blackoutType = localStorage.blackoutType || 'redact';
610 if (localStorage.blackoutType == 'redact') {
611 $('blackoutBox').className = 'rectBox';
612 $('redact').className = 'mark';
613 $('blur').className = '';
614 } else if (localStorage.blackoutType == 'blur') {
615 $('blackoutBox').className = 'blurBox';
616 $('redact').className = '';
617 $('blur').className = 'mark';
618 }
619 },
620
621 setFontSize: function(size) {
622 var id = 'size_' + size;
623 localStorage.fontSize = size;
624 $('size_10').className = '';
625 $('size_16').className = '';
626 $('size_18').className = '';
627 $('size_32').className = '';
628 $(id).className = 'mark';
629 },
630
631 setLineColorBoxStyle: function() {
632 localStorage.lineType = localStorage.lineType || 'line';
633 photoshop.color = localStorage.lineColor =
634 localStorage.lineColor || '#FF0000';
635 var ctx = $('lineIconCav').getContext('2d');
636 ctx.clearRect(0, 0, 14, 14);
637 if (localStorage.lineType == 'line') {
638 $('straightLine').className = 'mark';
639 $('arrow').className = '';
640 canvas.drawLine(ctx, photoshop.color, 'round', 2, 1, 13, 13, 1);
641 } else if (localStorage.lineType == 'arrow') {
642 $('straightLine').className = '';
643 $('arrow').className = 'mark';
644 canvas.drawArrow(ctx, photoshop.color, 2, 4, 7, 'round',1, 13, 13, 1);
645 }
646
647 },
648
649 initTools: function() {
650 photoshop.i18nReplace('tHighlight', 'highlight');
651 photoshop.i18nReplace('tRedact', 'redact');
652 photoshop.i18nReplace('redactText', 'solid_black');
653 photoshop.i18nReplace('tText', 'text');
654 photoshop.i18nReplace('tSave', 'save');
655 photoshop.i18nReplace('tClose', 'close');
656 photoshop.i18nReplace('border', 'border');
657 photoshop.i18nReplace('rect', 'rect');
658 photoshop.i18nReplace('blurText', 'blur');
659 photoshop.i18nReplace('lineText', 'line');
660 photoshop.i18nReplace('size_10', 'size_small');
661 photoshop.i18nReplace('size_16', 'size_normal');
662 photoshop.i18nReplace('size_18', 'size_large');
663 photoshop.i18nReplace('size_32', 'size_huge');
664 var fontSize = localStorage.fontSize = localStorage.fontSize || 16;
665 if (fontSize != 10 && fontSize != 16 && fontSize != 18 && fontSize != 32) {
666 localStorage.fontSize = 16;
667 }
668 localStorage.highlightMode = photoshop.flag =
669 localStorage.highlightMode || 'rectangle';
670 localStorage.highlightColor = localStorage.highlightColor || '#FF0000';
671 localStorage.fontColor = localStorage.fontColor || '#FF0000';
672 localStorage.highlightType = photoshop.highlightType =
673 localStorage.highlightType || 'border';
674 localStorage.blackoutType = localStorage.blackoutType || 'redact';
675 localStorage.lineType = localStorage.lineType || 'line';
676 localStorage.lineColor = localStorage.lineColor || '#FF0000';
677 photoshop.setHighlightColorBoxStyle(localStorage.highlightColor);
678 $('fontColorBox').style.backgroundColor =
679 localStorage.fontColor || '#FF0000';
680 $('btnHighlight').addEventListener('click', function() {
681 photoshop.toDo(this, localStorage.highlightMode);
682 photoshop.setHighlightColorBoxStyle(localStorage.highlightColor);
683 }, false);
684
685 $('btnBlackout').addEventListener('click', function() {
686 photoshop.toDo(this, localStorage.blackoutType);
687 photoshop.setBlackoutColorBoxStyle();
688 }, false);
689
690 $('btnText').addEventListener('click', function() {
691 photoshop.toDo(this, 'text');
692 }, false);
693
694 $('btnLine').addEventListener('click', function() {
695 photoshop.toDo(this, localStorage.lineType);
696 photoshop.setLineColorBoxStyle();
697 }, false);
698
699
700
701 photoshop.setHighlightColorBoxStyle(localStorage.highlightColor);
702 $('borderMode').addEventListener('click', function() {
703 localStorage.highlightType = 'border';
704 }, false);
705 $('rectMode').addEventListener('click', function() {
706 localStorage.highlightType = 'rect';
707 }, false);
708 $('rectangle').addEventListener('click', function() {
709 localStorage.highlightMode = photoshop.flag = 'rectangle';
710 photoshop.markCurrentElement(this);
711 }, false);
712 $('radiusRectangle').addEventListener('click', function() {
713 localStorage.highlightMode = photoshop.flag = 'radiusRectangle';
714 photoshop.markCurrentElement(this);
715 }, false);
716 $('ellipse').addEventListener('click', function() {
717 localStorage.highlightMode = photoshop.flag = 'ellipse';
718 photoshop.markCurrentElement(this);
719 }, false);
720 photoshop.setBlackoutColorBoxStyle();
721 $('redact').addEventListener('click', function() {
722 localStorage.blackoutType = 'redact';
723 }, false);
724 $('blur').addEventListener('click', function() {
725 localStorage.blackoutType = 'blur';
726 }, false);
727
728 photoshop.setLineColorBoxStyle();
729
730 photoshop.createColorPadStr($('highlightColorPad'), 'highlight');
731 photoshop.createColorPadStr($('fontColorPad'), 'text');
732 photoshop.createColorPadStr($('lineColorPad'), 'line');
733
734 $('straightLine').addEventListener('click', function() {
735 localStorage.lineType = 'line';
736 photoshop.setLineColorBoxStyle();
737 }, false);
738 $('arrow').addEventListener('click', function() {
739 localStorage.lineType = 'arrow';
740 photoshop.setLineColorBoxStyle();
741 }, false);
742
743 photoshop.setFontSize(localStorage.fontSize);
744 $('size_10').addEventListener('click', function() {
745 photoshop.setFontSize(10);
746 }, false);
747 $('size_16').addEventListener('click', function() {
748 photoshop.setFontSize(16);
749 }, false);
750 $('size_18').addEventListener('click', function() {
751 photoshop.setFontSize(18);
752 }, false);
753 $('size_32').addEventListener('click', function() {
754 photoshop.setFontSize(32);
755 }, false);
756 },
757
758 drawEllipseOnMaskCanvas: function(endX, endY, type, layerId) {
759 var ctx = $('mask-canvas').getContext('2d');
760 ctx.clearRect(0, 0, $('mask-canvas').width, $('mask-canvas').height);
761 var x = (photoshop.startX + endX) / 2;
762 var y = (photoshop.startY + endY) / 2;
763 var xAxis = Math.abs(endX - photoshop.startX) / 2;
764 var yAxis = Math.abs(endY - photoshop.startY) / 2;
765 canvas.drawEllipse(ctx, photoshop.color, x, y, xAxis, yAxis, 3,
766 photoshop.highlightType);
767 if (type == 'end') {
768 var offsetLeft = parseInt($(layerId).style.left);
769 var offsetTop = parseInt($(layerId).style.top);
770 var startX = photoshop.startX - offsetLeft ;
771 var startY = photoshop.startY - offsetTop ;
772 var newEndX = photoshop.endX - offsetLeft ;
773 var newEndY = photoshop.endY - offsetTop ;
774 x = (startX + newEndX) / 2;
775 y = (startY + newEndY) / 2;
776 xAxis = Math.abs(newEndX - startX) / 2;
777 yAxis = Math.abs(newEndY - startY) / 2;
778 var cavCopy = photoshop.createCanvas(layerId);
779 cavCopy.width = Math.abs(endX - photoshop.startX);
780 cavCopy.height = Math.abs(endY - photoshop.startY);
781 var ctxCopy = cavCopy.getContext('2d');
782 canvas.drawEllipse(ctxCopy, photoshop.color, x, y,
783 xAxis, yAxis, 3, photoshop.highlightType);
784 ctx.clearRect(0, 0, $('mask-canvas').width, $('mask-canvas').height);
785 }
786 },
787
788 showTip: function(className, message, delay) {
789 delay = delay || 2000;
790 var div = document.createElement('div');
791 div.className = className;
792 div.innerHTML = message;
793 document.body.appendChild(div);
794 div.style.left = (document.body.clientWidth - div.clientWidth) / 2 + 'px';
795 window.setTimeout(function() {
796 document.body.removeChild(div);
797 }, delay);
798 }
799};
800
801photoshop.init();
802$('photo').addEventListener('mousemove', photoshop.onMouseMove, true);
803$('photo').addEventListener('mousedown', photoshop.onMouseDown, true);
804$('photo').addEventListener('mouseup', photoshop.onMouseUp, true);
805document.addEventListener('mouseup', photoshop.onMouseUp, true);
806document.addEventListener('mousemove', photoshop.onMouseMove, true);
807
808$('canvas').addEventListener(
809 'selectstart', function f(e) { return false });
810$('mask-canvas').addEventListener(
811 'selectstart', function f(e) { return false });
812$('btnClose').addEventListener('click', photoshop.closeCurrentTab);
813$('uploadAccountList').addEventListener('click', function(e) {
814 var target = e.target;
815 var classList = Array.prototype.slice.call(target.classList)
816 if (classList.indexOf('accountName') >= 0) {
817 var site = target.dataset.site;
818 var userId = target.dataset.userId;
819 UploadUI.upload(site, userId);
820 } else if (classList.indexOf('deleteBtn') >= 0) {
821 var accountId = target.dataset.accountId;
822 UploadUI.deleteAccountItem(accountId);
823 }
824});