src_js/iconolab-bundle/src/components/cutout/svgboard.js
author durandn
Wed, 21 Sep 2016 14:46:17 +0200
changeset 209 b4b24c5eeb3e
parent 205 24e4aba394bf
child 213 43aa7c589048
permissions -rw-r--r--
Quickfix for rectangle editor #35
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
146
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
     1
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
     2
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
     3
import Snap from 'snapsvg'
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
     4
import ShapeResizer from './shape-resizer'
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
     5
import { eventEmitter } from '../utils'
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
     6
import SnapsvgZoom from './snapsvg-zoom'
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
     7
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
     8
/* custom plugins */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
     9
Snap.plugin(function (Snap, Element, Paper, glob) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    10
	var elproto = Element.prototype;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    11
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    12
	elproto.toBack = function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    13
		this.prependTo(this.paper);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    14
	};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    15
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    16
	elproto.toFront = function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    17
		this.appendTo(this.paper);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    18
	};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    19
});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    20
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    21
Element.prototype.getTransformedXY = function( x, y ) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    22
            var m = this.transform().globalMatrix;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    23
            return { x: m.x(x,y), y: m.y(x,y) };             
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    24
    };
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    25
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    26
var paper = null;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    27
var mainImage = null;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    28
var pointData = [];
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    29
var viewBoxBounds = {X: 100, Y:100};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    30
var zoomFactor = {x:1, y:1};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    31
var viewPort= {width:850, height:850};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    32
var currentViewBox = []; 
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    33
var config = null;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    34
var readOnly = false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    35
var startPoint = null;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    36
var drawing_path = null;
158
f9d0d6a840cf heatmap and folder design
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 156
diff changeset
    37
var drawing_paths = [];
146
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    38
var canDraw = false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    39
var rectZone = null;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    40
var PATH_COLOR = "#ff00ff";
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    41
var STROKE_COLOR = "red";
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    42
var FILL_COLOR = "orange";
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    43
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    44
var SELECTED_COLOR = "#ffff00";
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    45
var FIRST_NODE_COLOR = "#FF0000";
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    46
var HANDLE_SIZE = 8;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    47
var isDragged = false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    48
var enablePoint = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    49
var pathIsClosed = false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    50
var ENABLE_NEW_NODE = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    51
var RECT_MODE ='RECT';
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    52
var drawingMode = RECT_MODE; //free
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    53
var FREE_MODE = 'FREE';
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    54
var availableModes = [RECT_MODE, FREE_MODE];
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    55
var onChangeCallback = null;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    56
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    57
var getId = (function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    58
		var cpt = 0;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    59
		var defautPrefix = "item_"; 
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    60
		return function (prefix) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    61
			prefix = (typeof prefix === 'string') ? prefix : defautPrefix;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    62
			cpt = cpt + 1;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    63
			return prefix + cpt; 
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    64
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    65
	}());
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    66
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    67
var handleRectPath = function (path) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    68
	if (readOnly) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    69
		paper.path(path).attr({ stroke:'red', opacity: 0.6});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    70
		return;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    71
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    72
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    73
	var bbBox = Snap.path.getBBox(path);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    74
	rectZone = paper.rect(bbBox.x, bbBox.y, bbBox.width, bbBox.height);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    75
	rectZone.attr({fill: FILL_COLOR, stroke: STROKE_COLOR, opacity: 0.6});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    76
	drawing_path = rectZone;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    77
	canDraw = false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    78
	pathIsClosed = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    79
	ShapeResizer.enable_resizer(paper, drawing_path, viewPort, currentViewBox);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    80
};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    81
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    82
var handleFreePath = function (path) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    83
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    84
	if (readOnly) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    85
		
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    86
		paper.path(path).attr({
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    87
			stroke: 'orange',
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    88
			fill: 'orange',
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    89
			opacity: 0.5,
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    90
		});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    91
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    92
		return; 
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    93
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    94
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    95
	var pathInfos = Snap.parsePathString(path);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    96
	pathInfos.map(function (pathData) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    97
		if(pathData[0] !== 'Z') {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    98
			createPoint(paper, pathData[1], pathData[2], pointData);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
    99
		} else {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   100
			pathIsClosed = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   101
			updatePath(paper, onClosePath);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   102
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   103
	});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   104
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   105
	/* replay the path here */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   106
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   107
}; 
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   108
//transform point to path
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   109
var updatePath = function (paper, updateCallback) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   110
	var path = "M";
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   111
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   112
	if (pointData.length <= 1) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   113
		return;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   114
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   115
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   116
	path += pointData[0].x + ',' + pointData[0].y;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   117
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   118
	for (var i=0; i < pointData.length; i++) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   119
		if (i == 0) continue;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   120
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   121
		var pointInfos = pointData[i];
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   122
		var lPath = "L" + pointInfos.x + "," + pointInfos.y;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   123
		path += " " + lPath;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   124
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   125
	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   126
	path += (pathIsClosed) ? " Z": "";
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   127
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   128
	/* remove prev path */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   129
	if (drawing_path) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   130
		drawing_path.remove();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   131
	}	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   132
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   133
	drawing_path = paper.path(path);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   134
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   135
	drawing_path.attr({
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   136
		stroke: STROKE_COLOR,
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   137
		"vector-effect": "non-scaling-stroke",//prevent line to be zoom in
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   138
		"stroke-width": 3,
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   139
		fill: "white",
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   140
		opacity: 0.1
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   141
	});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   142
	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   143
	/* bring all handler to front */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   144
	pointData.map(function (point) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   145
		
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   146
		/*deal with handler size */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   147
		var handleSize = computeHandleSize();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   148
		if (point.handler) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   149
			point.handler.toFront();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   150
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   151
	});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   152
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   153
	if (typeof updateCallback === 'function' && pathIsClosed) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   154
		updateCallback();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   155
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   156
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   157
	if (!updateCallback && pathIsClosed) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   158
		applyClosedStyle();	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   159
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   160
};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   161
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   162
var applyClosedStyle = function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   163
	drawing_path.attr({ fill: FILL_COLOR, strokeWidth: 1, opacity:.6 });
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   164
} 
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   165
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   166
var onClosePath = function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   167
	ENABLE_NEW_NODE = false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   168
	applyClosedStyle();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   169
};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   170
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   171
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   172
var onClickOnHandler = function (point, p, e) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   173
	//close path
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   174
	if (point.isFirst && pointData.length > 2) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   175
		pathIsClosed = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   176
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   177
};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   178
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   179
var updatePointPosition = function (newPoint, x, y) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   180
	var index = pointData.indexOf(newPoint);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   181
	if (index !== -1) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   182
		pointData[index].x = x;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   183
		pointData[index].y = y; 
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   184
		return true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   185
	} else {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   186
		return false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   187
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   188
};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   189
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   190
var clearPreviousPath = function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   191
	drawing_path.remove();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   192
};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   193
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   194
var computeHandleSize = function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   195
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   196
	if(!currentViewBox.length) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   197
		currentViewBox = [0, 0, parseInt(mainImage.width()), parseInt(mainImage.height())]
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   198
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   199
	var currentHandleSize = HANDLE_SIZE * Math.min(currentViewBox[2], currentViewBox[3]) / 850; 
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   200
	return currentHandleSize;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   201
}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   202
 
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   203
var onMoveHandler = function (dx, dy, posX, posY, e) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   204
	isDragged = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   205
	var tdx, tdy;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   206
    var snapInvMatrix = this.transform().diffMatrix.invert();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   207
    snapInvMatrix.e = snapInvMatrix.f = 0;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   208
    tdx = snapInvMatrix.x( dx,dy ); 
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   209
    tdy = snapInvMatrix.y( dx,dy );
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   210
	var transformValue = this.data('origTransform') + (this.data('origTransform') ? "T" : "t") + [tdx, tdy];
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   211
	this.attr({ transform: transformValue});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   212
	var boxSize = this.getBBox();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   213
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   214
	var wasUpdated = updatePointPosition(this.data('point'), boxSize.x + (computeHandleSize() / 2) , boxSize.y + (computeHandleSize() / 2));
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   215
	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   216
	if (wasUpdated) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   217
		updatePath(this.paper);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   218
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   219
}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   220
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   221
var bindHandlerEvent = function (point, p) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   222
	point.handler.click(onClickOnHandler.bind(this, point, p));
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   223
	/* -- handler -- */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   224
	point.handler.hover(function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   225
		point.handler.attr({fill: 'yellow'});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   226
	}, function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   227
		var fillColor = point.isFirst ? FIRST_NODE_COLOR : "";
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   228
		point.handler.attr({fill: fillColor});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   229
	});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   230
	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   231
	point.handler.drag(onMoveHandler, function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   232
        this.data('origTransform', this.transform().local );
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   233
	}, function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   234
		if (!isDragged) { return true; }
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   235
		isDragged = false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   236
		enablePoint = false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   237
	});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   238
}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   239
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   240
var createPointHandler = function (p, point) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   241
	var handler;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   242
	var handleSize = computeHandleSize();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   243
	var handleX = point.x - handleSize / 2;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   244
	var handleY = point.y - handleSize / 2;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   245
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   246
	/* preserve initial size of 5px a quoi correspond 5 deal with current vp */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   247
	handler = p.rect(handleX, handleY, handleSize, handleSize);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   248
	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   249
	handler.addClass("drawingHandler");
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   250
	point.handler = handler;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   251
	point.handler.data('point', point);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   252
	if (pointData.length === 0) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   253
		point.isFirst = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   254
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   255
	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   256
	bindHandlerEvent(point, p);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   257
	point.handler.attr({
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   258
		fill: (pointData.length === 0) ? FIRST_NODE_COLOR : "",
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   259
		opacity: 0.9,
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   260
		stroke: PATH_COLOR
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   261
	});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   262
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   263
	return point;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   264
}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   265
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   266
//create paper
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   267
var createPoint = function (paper, x, y, pointData) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   268
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   269
	var point = {x:x, y:y, id: getId()};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   270
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   271
	if (pathIsClosed) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   272
		updatePath(paper, onClosePath);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   273
		return;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   274
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   275
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   276
	if (!enablePoint) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   277
		enablePoint = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   278
		return false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   279
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   280
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   281
	point = createPointHandler(paper, point);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   282
	pointData.push(point);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   283
	updatePath(paper);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   284
};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   285
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   286
var attachRectEvents = function (paper) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   287
	if (readOnly) { return false; }
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   288
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   289
	var startPosition = {};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   290
	var currentPosition = {};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   291
	/* add resizer */
209
b4b24c5eeb3e Quickfix for rectangle editor #35
durandn
parents: 205
diff changeset
   292
	
b4b24c5eeb3e Quickfix for rectangle editor #35
durandn
parents: 205
diff changeset
   293
    var computeOffset = function (e) {
b4b24c5eeb3e Quickfix for rectangle editor #35
durandn
parents: 205
diff changeset
   294
        var target = e.target || e.srcElement;
b4b24c5eeb3e Quickfix for rectangle editor #35
durandn
parents: 205
diff changeset
   295
        var rect = target.getBoundingClientRect();
b4b24c5eeb3e Quickfix for rectangle editor #35
durandn
parents: 205
diff changeset
   296
        var offsetX = (e.clientX - rect.left) / Math.min(zoomFactor.x, zoomFactor.y);
b4b24c5eeb3e Quickfix for rectangle editor #35
durandn
parents: 205
diff changeset
   297
        var offsetY  = (e.clientY - rect.top) / Math.min(zoomFactor.x, zoomFactor.y);
b4b24c5eeb3e Quickfix for rectangle editor #35
durandn
parents: 205
diff changeset
   298
        return {x: offsetX, y: offsetY};
b4b24c5eeb3e Quickfix for rectangle editor #35
durandn
parents: 205
diff changeset
   299
    }
b4b24c5eeb3e Quickfix for rectangle editor #35
durandn
parents: 205
diff changeset
   300
	
146
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   301
	paper.mousedown(function (e) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   302
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   303
		if (drawingMode === FREE_MODE || pathIsClosed) { return; }
205
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   304
		var offset = computeOffset(e)
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   305
		startPosition.x = offset.x;
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   306
		startPosition.y = offset.y;
146
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   307
		canDraw = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   308
	});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   309
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   310
	paper.mousemove(function (e) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   311
		if (drawingMode === FREE_MODE) { return; }
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   312
		if (!canDraw) { return; }
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   313
		var x, y;
205
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   314
		var offset = computeOffset(e)
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   315
		currentPosition.x = offset.x;
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   316
		currentPosition.y = offset.y;
146
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   317
		
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   318
		if (rectZone) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   319
			rectZone.remove();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   320
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   321
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   322
		/* bas -> droite */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   323
		var width = Math.abs(currentPosition.x - startPosition.x);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   324
		var height = Math.abs(startPosition.y - currentPosition.y);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   325
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   326
		if (currentPosition.y > startPosition.y && currentPosition.x > startPosition.x) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   327
			x = startPosition.x;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   328
			y = startPosition.y;	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   329
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   330
		
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   331
		/* haut -> droite */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   332
		if (currentPosition.y < startPosition.y && currentPosition.x > startPosition.x) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   333
			x = currentPosition.x - width;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   334
			y = currentPosition.y; 
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   335
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   336
		
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   337
		/* haut -> gauche */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   338
		if (currentPosition.y < startPosition.y && currentPosition.x < startPosition.x) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   339
			x = currentPosition.x;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   340
			y = currentPosition.y;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   341
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   342
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   343
		/* bas -> gauche */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   344
		if (currentPosition.y > startPosition.y && currentPosition.x < startPosition.x) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   345
			x = currentPosition.x
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   346
			y = currentPosition.y - height;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   347
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   348
		if(!x || !y) { return; }	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   349
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   350
		rectZone = paper.rect(x, y, width, height);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   351
		rectZone.attr({fill: FILL_COLOR, stroke: STROKE_COLOR, opacity: 0.6});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   352
	});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   353
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   354
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   355
	paper.mouseup(function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   356
		if ((drawingMode === FREE_MODE) || pathIsClosed || !rectZone) { return false; }
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   357
		drawing_path = rectZone;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   358
		ShapeResizer.enable_resizer(paper, rectZone, viewPort, currentViewBox);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   359
		canDraw = false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   360
		pathIsClosed = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   361
	});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   362
};
156
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   363
146
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   364
var attachPointEvents = function (paper) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   365
	if (readOnly) { return; }
156
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   366
	var clickTimeout = null;
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   367
	var preventClick = false;
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   368
	paper.dblclick( function (e) {
146
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   369
		if (drawingMode === RECT_MODE) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   370
			return true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   371
		}
156
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   372
		preventClick = true;
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   373
		if (clickTimeout) {
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   374
			clickTimeout = clearTimeout(clickTimeout);
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   375
			preventClick = false;
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   376
			pathIsClosed = true;
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   377
			if (pointData.length > 2) {
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   378
				updatePath(paper, onClosePath);
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   379
			}
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   380
		}
146
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   381
156
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   382
		return false;
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   383
	});
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   384
164
44ced96727f7 fixing event offset on firefox
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 158
diff changeset
   385
	var computeOffset = function (e) {
44ced96727f7 fixing event offset on firefox
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 158
diff changeset
   386
		var target = e.target || e.srcElement;
44ced96727f7 fixing event offset on firefox
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 158
diff changeset
   387
		var rect = target.getBoundingClientRect();
205
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   388
        var offsetX = (e.clientX - rect.left) / Math.min(zoomFactor.x, zoomFactor.y);
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   389
        var offsetY  = (e.clientY - rect.top) / Math.min(zoomFactor.x, zoomFactor.y);
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   390
        return {x: offsetX, y: offsetY};
164
44ced96727f7 fixing event offset on firefox
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 158
diff changeset
   391
	}
44ced96727f7 fixing event offset on firefox
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 158
diff changeset
   392
44ced96727f7 fixing event offset on firefox
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 158
diff changeset
   393
	var clickHandler = function (offsetX, offsetY) {
44ced96727f7 fixing event offset on firefox
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 158
diff changeset
   394
	
156
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   395
		if (preventClick) { return; }
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   396
		
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   397
		if (drawingMode === RECT_MODE) {
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   398
			return true;
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   399
		}
146
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   400
		if (!ENABLE_NEW_NODE) { return true; }
164
44ced96727f7 fixing event offset on firefox
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 158
diff changeset
   401
		createPoint(paper, offsetX, offsetY, pointData);
156
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   402
		clickTimeout = null;
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   403
		preventClick = false;
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   404
	}
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   405
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   406
	paper.click(function (e) {
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   407
		if (clickTimeout) { return; }
205
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   408
		if (!jQuery(e.target).is(mainImage)) { return; }
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   409
		var offset = computeOffset(e)
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   410
		var offsetX = offset.x;
24e4aba394bf cleared incompatibility with older versions of Firefox and bug on Chrome for the fragment editor #35
durandn
parents: 164
diff changeset
   411
		var offsetY = offset.y;
164
44ced96727f7 fixing event offset on firefox
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 158
diff changeset
   412
		clickTimeout = setTimeout(clickHandler.bind(this, offsetX, offsetY), 190);	
146
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   413
	});
156
e1e14766f608 adding diffviewer to merging view
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents: 146
diff changeset
   414
146
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   415
};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   416
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   417
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   418
var attachZoomEvents = function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   419
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   420
	eventEmitter.on("zoomChanged", function (zoomInfos) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   421
		zoomFactor = zoomInfos.zoomFactor;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   422
		currentViewBox = zoomInfos.currentViewBox;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   423
		var previousPath = API.getPath();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   424
		API.clear();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   425
		API.setPath(previousPath);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   426
	});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   427
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   428
}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   429
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   430
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   431
var API = {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   432
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   433
	getPaper: function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   434
		return paper;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   435
	},
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   436
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   437
	setPath: function (pathString) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   438
		/* redraw the path */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   439
		var pathInfos = pathString.split(';');
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   440
		if ( availableModes.indexOf(pathInfos[1]) === -1) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   441
			/* We assume then it is a free path */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   442
			pathInfos[1] = "FREE"; 
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   443
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   444
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   445
		this.setDrawingMode(pathInfos[1]);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   446
		var pathData = pathInfos[0];
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   447
		
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   448
		if (!pathData.length) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   449
			return;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   450
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   451
		/* deal with path nomalization x = ImageWith/MaxXBound */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   452
		var xRatio = mainImage.attr("width") / viewBoxBounds.X;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   453
		var yRatio = mainImage.attr("height") / viewBoxBounds.Y;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   454
		
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   455
		if (isNaN(xRatio) || isNaN(yRatio)) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   456
			new Error('Ratio should be a number.');
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   457
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   458
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   459
		var transformMatrix = Snap.matrix(xRatio, 0, 0, yRatio, 0, 0);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   460
		var path = Snap.path.map(pathData, transformMatrix).toString();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   461
		
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   462
		/* always close path */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   463
		if (path.search(/[z|Z]/gi) === -1 ) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   464
			path += "Z";
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   465
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   466
		if (pathInfos.length >= 2) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   467
			if (pathInfos[1] === RECT_MODE) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   468
				handleRectPath(path);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   469
			}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   470
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   471
			if (pathInfos[1] === FREE_MODE) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   472
				handleFreePath(path);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   473
			}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   474
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   475
	},
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   476
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   477
	setDrawingMode: function (mode) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   478
		
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   479
		if (availableModes.indexOf(mode) !== -1) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   480
			drawingMode = mode;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   481
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   482
		if (typeof onChangeCallback === "function") {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   483
			onChangeCallback(drawingMode);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   484
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   485
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   486
		this.clear();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   487
	},
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   488
	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   489
	clear: function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   490
		/* clear previous path, point, handler */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   491
		pointData.map(function (point) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   492
			if (point.handler) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   493
				point.handler.remove();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   494
			}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   495
		});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   496
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   497
		/*clear path is exists*/
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   498
		 if (drawing_path) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   499
		 	drawing_path.remove();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   500
		 }		
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   501
		eventEmitter.emit("cutout:clear");
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   502
		pointData = [];
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   503
		startPoint = null;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   504
		drawing_path = null;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   505
		isDragged = false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   506
		enablePoint = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   507
		pathIsClosed = false;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   508
		ENABLE_NEW_NODE = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   509
	},
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   510
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   511
	getShapeBBox: function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   512
		var currentPath = this.getPath();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   513
		return Snap.path.getBBox(currentPath);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   514
	},
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   515
	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   516
	getShape: function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   517
		return this.getPath();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   518
	},
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   519
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   520
	getPath: function () {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   521
		/* retourne le chemin */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   522
		/* send path and BBox | implement edit and load path */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   523
		var path = "";
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   524
		if (drawing_path) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   525
			if (drawingMode === RECT_MODE) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   526
				var bBox = drawing_path.getBBox();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   527
				var transform = drawing_path.transform();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   528
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   529
				if (!transform.global.length) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   530
					var shapePath = drawing_path.getBBox().path;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   531
				}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   532
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   533
				else {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   534
					
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   535
					var shapeX = drawing_path.node.getAttribute('x');
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   536
					var shapeY = drawing_path.node.getAttribute('y');
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   537
					var transformMatrix = transform.totalMatrix;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   538
					var fakeShape = paper.rect(transformMatrix.x(shapeX, shapeY),transformMatrix.y(shapeX, shapeY), bBox.width, bBox.height);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   539
					shapePath = fakeShape.getBBox().path;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   540
					fakeShape.remove();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   541
				}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   542
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   543
				path = Snap.path.toAbsolute(shapePath).toString();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   544
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   545
			}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   546
			else {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   547
				path = drawing_path.attr('d');
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   548
			}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   549
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   550
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   551
		var xRatio = viewBoxBounds.X / mainImage.attr("width");
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   552
		var yRatio = viewBoxBounds.Y / mainImage.attr("height");
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   553
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   554
		if(isNaN(xRatio) || isNaN(yRatio)) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   555
			new Error('ratio should be a number.');
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   556
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   557
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   558
		if (!path.length) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   559
			path = (drawingMode === RECT_MODE) ? ";RECT" : ";FREE";
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   560
			return path;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   561
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   562
		var normalizeMatrix = Snap.matrix(xRatio, 0, 0, yRatio, 0, 0);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   563
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   564
		path = Snap.path.map(path, normalizeMatrix).toString();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   565
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   566
				/* save the type */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   567
		var type = (drawingMode === RECT_MODE) ? ";RECT" : ";FREE";
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   568
		if (path.search(/[z|Z]/gi) === -1) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   569
			path += " Z";	
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   570
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   571
		
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   572
		path += type;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   573
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   574
		return path;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   575
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   576
};
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   577
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   578
/* change to a component */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   579
export default {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   580
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   581
	init: function(config) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   582
		mainImage = jQuery(config.wrapperId).find('.main-image').eq(0);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   583
		var cutCanvas = jQuery(config.wrapperId).find('.cut-canvas').eq(0);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   584
		var path = jQuery(config.wrapperId).find('.image-path').eq(0);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   585
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   586
		if (typeof config.onDrawingModeChange === 'function') {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   587
			onChangeCallback = config.onDrawingModeChange;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   588
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   589
		
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   590
		if (!mainImage.length) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   591
			throw new Error("The main image Can't be found ...");
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   592
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   593
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   594
		if (!cutCanvas.length) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   595
			var cutCanvas = jQuery('<svg version="1.1"></svg>').addClass('cut-canvas');
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   596
			jQuery(config.wrapperId).append(cutCanvas);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   597
			cutCanvas.append(mainImage);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   598
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   599
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   600
		cutCanvas.css({
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   601
			marginLeft: 'auto',
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   602
			marginRight: 'auto',
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   603
			width: viewPort.width,
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   604
			height: viewPort.height,
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   605
		});
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   606
		if (typeof config.readOnly === 'boolean' && config.readOnly === true) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   607
			readOnly = true;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   608
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   609
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   610
		paper = new Snap(cutCanvas.get(0));
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   611
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   612
		if (path.length) {
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   613
			jQuery(cutCanvas).append(path);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   614
			var pathData = path.attr("d");
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   615
			API.setPath(pathData);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   616
			path.remove();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   617
		}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   618
		/* enable zoom */
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   619
		attachZoomEvents();
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   620
		attachPointEvents(paper);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   621
		attachRectEvents(paper);
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   622
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   623
		return API;
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   624
	}
f912b591e1c1 new src_js folder
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff changeset
   625
};