-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-cep-dropzone.js
168 lines (135 loc) · 4.84 KB
/
jquery-cep-dropzone.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
(function($) {
$.fn.cepDropZone = function(options) {
/**
* Define some default settings
*/
var defaults = {
onSuccess : function(event, svg) {
console.log('CEP Drag-n-Drop finished successfully', svg);
},
onError : function(event, error) {
console.error('Oops! CEP Drag-n-Drop encountered an error.');
}
};
var onSuccess = function(){}
if (options.onSuccess instanceof Function) {
onSuccess = options.onSuccess;
}
var onError = function(){}
if (options.onError instanceof Function) {
onError = options.onError;
}
/**
* Merge the runtime options with the default settings
*/
var options = $.extend({}, defaults, options);
/**
* Appends the dropzone to the document.
*/
function getDropZone() {
var $dropzone = $('<textarea/>')
.attr({
class : 'dropzone',
id : 'dropzone--' + new Date().getTime(),
ondragover : '(function(e) { e.preventDefault() })(event)',
placeholder : 'Drop page items here'
});
return $dropzone;
}
/**
* Adds default styling to the window. Object is not styled inline
* so that styles can be over-ridden via CSS stylesheets.
*/
function appendStyles() {
var rules = '', bodyWidth;
function getStyleRule(selector, rules) {
var props = [];
for (var prop in rules) {
props.push(prop + ' : ' + rules[prop]);
}
return selector + ' { ' + props.join('; ') + ' } ';
}
bodyWidth = parseInt($('body').width()) - (parseInt($('body').css('padding')) * 2);
rules += getStyleRule('.dropzone', {
'display' : 'inline-flex',
'width' : (bodyWidth - 48) + 'px',
'height' : '20px',
'padding' : '40px 20px',
'background' : '#aaa',
'border' : '4px dashed #888',
'border-radius' : '6px',
'resize' : 'none',
'text-align' : 'center',
'overflow' : 'hidden'
});
rules += getStyleRule('.dropzone:hover', {
'background' : '#bbb',
'border-color' : '#555',
'cursor' : 'move'
});
var $style = $('<style/>');
$style.text(rules);
$style.appendTo($('head'));
};
/**
* Prettifies the SVG. Not really necessary. Just obsessive.
* @param {string} svg The SVG string.
* @returns {string}
*/
function cleanSvg(svg) {
svg = svg.replace(/\s\s+/g, ' ');
var lines = svg.split('\n');
lines.map(function(line) {
line = line.replace(/\s\s+/g, ' ');
line = line.replace(/\t+/g, ' ');
return line;
});
return lines.join('');
}
/**
* Handles the drop event.
* @param {Event} event The drop event.
* @returns void
*/
function dropEventHandler(event) {
event.preventDefault();
var $target = $(event.target);
csInterface.evalScript('app.executeMenuCommand("copy")', function(result) {
try {
var svg,
$svg = $('<svg/>');
$target.focus();
$target.val('');
document.execCommand("paste");
svg = $target.val();
svg = cleanSvg(svg);
$target.val(svg);
if (onSuccess instanceof Function) {
onSuccess.apply(this, [event, svg]);
}
}
catch(error) {
console.error(error);
if (onError instanceof Function) {
onError.apply(this, [event, error]);
}
}
});
}
/*
* Add default styles.
*/
appendStyles();
/**
* Return the object to preserve method chaining
*/
return this.each(function(i) {
var $anchor = $(this);
console.log('Adding cep-dropzone ' + i);
var $dropzone = getDropZone()
$dropzone.appendTo($anchor);
$dropzone.on('drop', dropEventHandler);
return this;
});
};
})(jQuery);