-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
116 lines (100 loc) · 3.08 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function TextContent(){
return {
getEditHandler: function(){
return function editBox(){
//Getting text element
var viewableText = $(this);
var element = $(this).parent()[0];
//creating and preparing editable field
var editableText = $("<textarea />");
editableText.html(viewableText.text());
editableText.css('height', element.clientHeight*0.9);
editableText.css('width', element.clientWidth*0.9);
editableText.css('resize', 'none');
//Replacing text element with editable text area
viewableText.replaceWith(editableText);
editableText.focus();
//Setting reverse replace after editing
$(editableText).blur(function() {
var html = $(this).val();
var viewableText = $("<div class='content text'>");
viewableText.html(html);
$(this).replaceWith(viewableText);
viewableText.on('dblclick', editBox);
//resizing parent box if necessary
var parent = viewableText.parent();
if(viewableText.height()>parent.height()){
parent.height(viewableText.height()/0.9);
}
if(viewableText.width()>parent.width()){
parent.width(viewableText.width()/0.9);
}
//adding text label drag handler
$('.text').draggable({
containment:"parent"
});
});
};
},
cloneContent: function(content){
var element = $("<div class='content text'>");
var html = content.text();
element.html(html);
return element;
}
};
}
function ImageContent(){
return {
getEditHandler: function(){
return function(){
var image = $(this).find('img');
//This is a hidden input field that we use for loading images
//Check html file for details
var loader = $('#imageLoader');
loader.on('change', function(ev){
var f = ev.target.files[0];
var fr = new FileReader();
fr.onload = function(ev2) {
///Adding loaded image to a corresponding field
image.attr('src', ev2.target.result);
};
fr.onloadend = function(){
//Removing all the listeners reagrdless of loading success
fr.onload = null;
fr.onloadend = null;
loader.off();
}
fr.readAsDataURL(f);
});
//Imitating input button click - as far as I understand, that's the only way to load something
loader[0].click();
}
},
cloneContent: function(content){
var element = $("<div class='content image'>");
var img = $("<img src='/'>");
img.attr('src', content.find('img').attr('src'));
element.append(img);
return element;
}
}
}
function getContentClass(element){
var supportedTypes = ["text", "image"];
var classNames = element.attr("class").split(' ');
for(var i=0; i<classNames.length; i++){
switch(classNames[i]){
case "text":
return TextContent();
break;
case "image":
return ImageContent();
break;
default:
//continue search
break;
}
}
throw Error("Element's classes do not correspond to any supported ContentClass");
}