| author | ymh <ymh.work@gmail.com> |
| Sat, 25 Apr 2015 04:13:53 +0200 | |
| changeset 433 | e457ec945e50 |
| parent 420 | 22393cbf4467 |
| child 434 | 0d5998b32a7c |
| permissions | -rw-r--r-- |
| 284 | 1 |
|
2 |
define(['jquery', 'underscore', 'requtils', 'renderer/baseeditor'], function ($, _, requtils, BaseEditor) { |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
3 |
'use strict'; |
|
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
4 |
|
| 284 | 5 |
var Utils = requtils.getUtils(); |
6 |
||
7 |
/* NodeEditor Begin */ |
|
8 |
//var NodeEditor = Renderer.NodeEditor = Utils.inherit(Renderer._BaseEditor); |
|
9 |
var NodeEditor = Utils.inherit(BaseEditor); |
|
10 |
||
11 |
_(NodeEditor.prototype).extend({ |
|
| 420 | 12 |
_init: function() { |
13 |
BaseEditor.prototype._init.apply(this); |
|
14 |
this.template = this.options.templates['templates/nodeeditor.html']; |
|
15 |
this.readOnlyTemplate = this.options.templates['templates/nodeeditor_readonly.html']; |
|
16 |
}, |
|
| 284 | 17 |
draw: function() { |
18 |
var _model = this.source_representation.model, |
|
19 |
_created_by = _model.get("created_by") || Utils._USER_PLACEHOLDER(this.renkan), |
|
20 |
_template = (this.renderer.isEditable() ? this.template : this.readOnlyTemplate ), |
|
21 |
_image_placeholder = this.options.static_url + "img/image-placeholder.png", |
|
22 |
_size = (_model.get("size") || 0); |
|
23 |
this.editor_$ |
|
24 |
.html(_template({ |
|
25 |
node: { |
|
26 |
has_creator: !!_model.get("created_by"), |
|
27 |
title: _model.get("title"), |
|
28 |
uri: _model.get("uri"), |
|
29 |
short_uri: Utils.shortenText((_model.get("uri") || "").replace(/^(https?:\/\/)?(www\.)?/,'').replace(/\/$/,''),40), |
|
30 |
description: _model.get("description"), |
|
31 |
image: _model.get("image") || "", |
|
32 |
image_placeholder: _image_placeholder, |
|
33 |
color: _model.get("color") || _created_by.get("color"), |
|
34 |
clip_path: _model.get("clip_path") || false, |
|
35 |
created_by_color: _created_by.get("color"), |
|
36 |
created_by_title: _created_by.get("title"), |
|
| 330 | 37 |
size: (_size > 0 ? "+" : "") + _size, |
38 |
shape: _model.get("shape") || "circle" |
|
| 284 | 39 |
}, |
40 |
renkan: this.renkan, |
|
41 |
options: this.options, |
|
42 |
shortenText: Utils.shortenText |
|
43 |
})); |
|
44 |
this.redraw(); |
|
45 |
var _this = this, |
|
46 |
closeEditor = function() { |
|
47 |
_this.renderer.removeRepresentation(_this); |
|
48 |
paper.view.draw(); |
|
49 |
}; |
|
50 |
||
51 |
this.editor_$.find(".Rk-CloseX").click(closeEditor); |
|
52 |
||
53 |
this.editor_$.find(".Rk-Edit-Goto").click(function() { |
|
54 |
if (!_model.get("uri")) { |
|
55 |
return false; |
|
56 |
} |
|
57 |
}); |
|
58 |
||
59 |
if (this.renderer.isEditable()) { |
|
60 |
||
| 433 | 61 |
var onFieldChange = _.throttle(function() { |
62 |
_.defer(function() { |
|
63 |
if (_this.renderer.isEditable()) { |
|
64 |
var _data = { |
|
65 |
title: _this.editor_$.find(".Rk-Edit-Title").val() |
|
66 |
}; |
|
67 |
if (_this.options.show_node_editor_uri) { |
|
68 |
_data.uri = _this.editor_$.find(".Rk-Edit-URI").val(); |
|
69 |
_this.editor_$.find(".Rk-Edit-Goto").attr("href",_data.uri || "#"); |
|
70 |
} |
|
71 |
if (_this.options.show_node_editor_image) { |
|
72 |
_data.image = _this.editor_$.find(".Rk-Edit-Image").val(); |
|
73 |
_this.editor_$.find(".Rk-Edit-ImgPreview").attr("src", _data.image || _image_placeholder); |
|
74 |
} |
|
75 |
if (_this.options.show_node_editor_description) { |
|
76 |
_data.description = _this.editor_$.find(".Rk-Edit-Description").val(); |
|
77 |
} |
|
78 |
if (_this.options.change_shapes) { |
|
79 |
if(_model.get("shape")!==_this.editor_$.find(".Rk-Edit-Shape").val()){ |
|
80 |
_data.shape = _this.editor_$.find(".Rk-Edit-Shape").val(); |
|
81 |
_data.shape_changed = true; |
|
| 284 | 82 |
} |
| 433 | 83 |
} |
84 |
_model.set(_data); |
|
85 |
_this.redraw(); |
|
86 |
// For an unknown reason, we have to set data twice when we change shape, otherwise the image disappears. |
|
87 |
if(_data.shape_changed===true){ |
|
| 284 | 88 |
_model.set(_data); |
89 |
} |
|
| 433 | 90 |
} else { |
91 |
closeEditor(); |
|
92 |
} |
|
93 |
}); |
|
94 |
}, 500); |
|
95 |
||
| 284 | 96 |
this.editor_$.on("keyup", function(_e) { |
97 |
if (_e.keyCode === 27) { |
|
98 |
closeEditor(); |
|
99 |
} |
|
100 |
}); |
|
101 |
||
| 330 | 102 |
this.editor_$.find("input, textarea, select").on("change keyup paste", onFieldChange); |
| 284 | 103 |
|
| 385 | 104 |
if(_this.options.allow_image_upload) { |
105 |
this.editor_$.find(".Rk-Edit-Image-File").change(function() { |
|
106 |
if (this.files.length) { |
|
107 |
var f = this.files[0], |
|
108 |
fr = new FileReader(); |
|
109 |
if (f.type.substr(0,5) !== "image") { |
|
110 |
alert(_this.renkan.translate("This file is not an image")); |
|
111 |
return; |
|
112 |
} |
|
113 |
if (f.size > (_this.options.uploaded_image_max_kb * 1024)) { |
|
114 |
alert(_this.renkan.translate("Image size must be under ") + _this.options.uploaded_image_max_kb + _this.renkan.translate("KB")); |
|
115 |
return; |
|
116 |
} |
|
117 |
fr.onload = function(e) { |
|
118 |
_this.editor_$.find(".Rk-Edit-Image").val(e.target.result); |
|
119 |
onFieldChange(); |
|
120 |
}; |
|
121 |
fr.readAsDataURL(f); |
|
| 284 | 122 |
} |
| 385 | 123 |
}); |
124 |
} |
|
| 284 | 125 |
this.editor_$.find(".Rk-Edit-Title")[0].focus(); |
126 |
||
127 |
var _picker = _this.editor_$.find(".Rk-Edit-ColorPicker"); |
|
128 |
||
129 |
this.editor_$.find(".Rk-Edit-ColorPicker-Wrapper").hover( |
|
130 |
function(_e) { |
|
131 |
_e.preventDefault(); |
|
132 |
_picker.show(); |
|
133 |
}, |
|
134 |
function(_e) { |
|
135 |
_e.preventDefault(); |
|
136 |
_picker.hide(); |
|
137 |
} |
|
138 |
); |
|
139 |
||
140 |
_picker.find("li").hover( |
|
141 |
function(_e) { |
|
142 |
_e.preventDefault(); |
|
143 |
_this.editor_$.find(".Rk-Edit-Color").css("background", $(this).attr("data-color")); |
|
144 |
}, |
|
145 |
function(_e) { |
|
146 |
_e.preventDefault(); |
|
147 |
_this.editor_$.find(".Rk-Edit-Color").css("background", _model.get("color") || (_model.get("created_by") || Utils._USER_PLACEHOLDER(_this.renkan)).get("color")); |
|
148 |
} |
|
149 |
).click(function(_e) { |
|
150 |
_e.preventDefault(); |
|
151 |
if (_this.renderer.isEditable()) { |
|
152 |
_model.set("color", $(this).attr("data-color")); |
|
153 |
_picker.hide(); |
|
154 |
paper.view.draw(); |
|
155 |
} else { |
|
156 |
closeEditor(); |
|
157 |
} |
|
158 |
}); |
|
159 |
||
160 |
var shiftSize = function(n) { |
|
161 |
if (_this.renderer.isEditable()) { |
|
162 |
var _newsize = n+(_model.get("size") || 0); |
|
163 |
_this.editor_$.find(".Rk-Edit-Size-Value").text((_newsize > 0 ? "+" : "") + _newsize); |
|
164 |
_model.set("size", _newsize); |
|
165 |
paper.view.draw(); |
|
166 |
} else { |
|
167 |
closeEditor(); |
|
168 |
} |
|
169 |
}; |
|
170 |
||
171 |
this.editor_$.find(".Rk-Edit-Size-Down").click(function() { |
|
172 |
shiftSize(-1); |
|
173 |
return false; |
|
174 |
}); |
|
175 |
this.editor_$.find(".Rk-Edit-Size-Up").click(function() { |
|
176 |
shiftSize(1); |
|
177 |
return false; |
|
178 |
}); |
|
| 433 | 179 |
|
|
384
6a7930a0d4d1
Close #60 - Add a trash icon to delete the image URL
rougeronj
parents:
331
diff
changeset
|
180 |
this.editor_$.find(".Rk-Edit-Image-Del").click(function() { |
|
6a7930a0d4d1
Close #60 - Add a trash icon to delete the image URL
rougeronj
parents:
331
diff
changeset
|
181 |
_this.editor_$.find(".Rk-Edit-Image").val(''); |
|
6a7930a0d4d1
Close #60 - Add a trash icon to delete the image URL
rougeronj
parents:
331
diff
changeset
|
182 |
onFieldChange(); |
|
6a7930a0d4d1
Close #60 - Add a trash icon to delete the image URL
rougeronj
parents:
331
diff
changeset
|
183 |
return false; |
|
6a7930a0d4d1
Close #60 - Add a trash icon to delete the image URL
rougeronj
parents:
331
diff
changeset
|
184 |
}); |
| 284 | 185 |
} else { |
186 |
if (typeof this.source_representation.highlighted === "object") { |
|
187 |
var titlehtml = this.source_representation.highlighted.replace(_(_model.get("title")).escape(),'<span class="Rk-Highlighted">$1</span>'); |
|
188 |
this.editor_$.find(".Rk-Display-Title" + (_model.get("uri") ? " a" : "")).html(titlehtml); |
|
189 |
if (this.options.show_node_tooltip_description) { |
|
190 |
this.editor_$.find(".Rk-Display-Description").html(this.source_representation.highlighted.replace(_(_model.get("description")).escape(),'<span class="Rk-Highlighted">$1</span>')); |
|
191 |
} |
|
192 |
} |
|
193 |
} |
|
194 |
this.editor_$.find("img").load(function() { |
|
195 |
_this.redraw(); |
|
196 |
}); |
|
197 |
}, |
|
198 |
redraw: function() { |
|
199 |
var _coords = this.source_representation.paper_coords; |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
200 |
Utils.drawEditBox(this.options, _coords, this.editor_block, this.source_representation.circle_radius * 0.75, this.editor_$); |
| 284 | 201 |
this.editor_$.show(); |
202 |
paper.view.draw(); |
|
203 |
} |
|
| 433 | 204 |
}).value(); |
205 |
// }); |
|
| 284 | 206 |
/* NodeEditor End */ |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
207 |
|
| 284 | 208 |
return NodeEditor; |
209 |
||
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
210 |
}); |