-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanno-parse-plugin.js
151 lines (134 loc) · 4.21 KB
/
anno-parse-plugin.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
/**
* A simple storage connector plugin to the Parse REST interface.
*
* Note: the plugin requires jQuery to be linked into the host page.
*
* THIS PLUGIN IS FOR DEMO PURPOSES ONLY - DON'T USE IN A PRODUCTION
* ENVIRONMENT.
*/
annotorious.plugin.Parse = function(opt_config_options) {
/** @private **/
this._APP_ID = opt_config_options['app_id'];
this._JS_KEY = opt_config_options['js_key'];
this._DEBUG = opt_config_options['debug'] || false;
/** @private **/
this._collection = null;
/** @private **/
this._loadIndicators = [];
};
annotorious.plugin.Parse.prototype.initPlugin = function(anno) {
var self = this;
// initialize Parse
Parse.$ = jQuery;
Parse.initialize(this._APP_ID, this._JS_KEY);
var Annotation = Parse.Object.extend("Annotation");
var AnnotationCollection = Parse.Collection.extend({
model: Annotation
});
this._collection = new AnnotationCollection();
anno.addHandler('onAnnotationCreated', function(annotation) {
self._create(annotation);
});
anno.addHandler('onAnnotationUpdated', function(annotation) {
self._update(annotation);
});
anno.addHandler('onAnnotationRemoved', function(annotation) {
self._delete(annotation);
});
self._loadAnnotations(anno);
};
annotorious.plugin.Parse.prototype.onInitAnnotator = function(annotator) {
var spinner = this._newLoadIndicator();
annotator.element.appendChild(spinner);
this._loadIndicators.push(spinner);
}
annotorious.plugin.Parse.prototype._newLoadIndicator = function() {
var outerDIV = document.createElement('div');
outerDIV.className = 'annotorious-parse-plugin-load-outer';
var innerDIV = document.createElement('div');
innerDIV.className = 'annotorious-parse-plugin-load-inner';
outerDIV.appendChild(innerDIV);
return outerDIV;
};
/**
* @private
*/
annotorious.plugin.Parse.prototype._loadAnnotations = function(anno) {
var self = this;
var removeSpinner = function() {
// Remove all load indicators
jQuery.each(self._loadIndicators, function(idx, spinner) {
jQuery(spinner).remove();
});
}
this._collection.fetch({
success: function(coll) {
coll.each(function(annotation) {
self._DEBUG && console.log("load success", annotation);
if (!annotation.get("shape") && annotation.get("shapes")[0].geometry) {
anno.addAnnotation(annotation.toJSON());
}
});
removeSpinner();
},
error: function(coll, error) {
self._DEBUG && console.log("load error", coll, error);
removeSpinner();
}
});
};
/**
* @private
*/
annotorious.plugin.Parse.prototype._create = function(annotation_data) {
var self = this;
var Annotation = Parse.Object.extend("Annotation");
var annotation = new Annotation();
annotation.save(annotation_data,
{
success: function(parseAnnotation) {
self._DEBUG && console.log("create success", parseAnnotation);
annotation_data.objectId = parseAnnotation.id;
self._collection.add(annotation);
},
error: function(parseAnnotation, error) {
self._DEBUG && console.log("create error", parseAnnotation, error);
}
}
);
};
/**
* @private
*/
annotorious.plugin.Parse.prototype._update = function(annotation_data) {
var self = this;
var annotation = this._collection.get(annotation_data.objectId);
delete annotation_data.objectId;
annotation.save(annotation_data,
{
success: function(parseAnnotation) {
self._DEBUG && console.log("update success", parseAnnotation);
annotation_data.objectId = parseAnnotation.id;
},
error: function(parseAnnotation, error) {
self._DEBUG && console.log("update error", parseAnnotation, error);
}
}
);
};
/**
* @private
*/
annotorious.plugin.Parse.prototype._delete = function(annotation_data) {
self = this;
var annotation = this._collection.get(annotation_data.objectId);
annotation.destroy({
success: function(parseAnnotation) {
self._DEBUG && console.log("delete success", parseAnnotation);
// the model should be automatically removed from any collections that was containing it.
},
error: function(parseAnnotation, error) {
self._DEBUG && console.log("delete error", parseAnnotation, error);
}
});
};