Skip to content

Instantly share code, notes, and snippets.

@dommmel
Last active December 16, 2023 20:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dommmel/5849551 to your computer and use it in GitHub Desktop.
Save dommmel/5849551 to your computer and use it in GitHub Desktop.
Annotorious Plugin for Parse.com. A simple storage connector plugin to the Parse REST interface.
/**
* 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'];
/** @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._showError = function(error) {
// TODO proper error handling
window.alert('ERROR');
console.log(error);
}
/**
* @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) {
console.warn(annotation);
if (!annotation.get("shape") && annotation.get("shapes")[0].geometry) {
anno.addAnnotation(annotation.toJSON());
}
});
removeSpinner();
},
error: function(coll, error) {
debugger;
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) {
console.log("Success Creating");
console.log(annotation);
annotation_data.id = parseAnnotation.id;
},
error: function(annotation) {
console.log("Error Creating");
console.log(annotation);
}
}
);
this._collection.add([annotation]);
}
/**
* @private
*/
annotorious.plugin.Parse.prototype._update = function(annotation_data) {
var annotation = this._collection.get(annotation_data.objectId);
annotation.save(annotation_data,
{
success: function(parseAnnotation) {
console.log("Success Updating");
console.log(parseAnnotation);
annotation_data.id = parseAnnotation.id;
},
error: function(parseAnnotation) {
console.log("Error Updating");
console.log(parseAnnotation);
}
}
);
}
/**
* @private
*/
annotorious.plugin.Parse.prototype._delete = function(annotation_data) {
self = this;
var annotation = this._collection.get(annotation_data.objectId);
annotation.destroy({
success: function(anno) {
console.log("Success Deleting");
console.log(anno);
self._collection.remove([annotation]);
},
error: function(annotation, error) {
console.log("Error Deleting");
console.log(annotation);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment