+ if($e.hasClass('annotation'))
+ {
+ if(this.currentOpen) return false;
+
+ var $p = $e.parent();
+ if(this.currentFocused)
+ {
+ console.log(this.currentFocused, $p);
+ if($p[0] == this.currentFocused[0]) {
+ console.log('unfocus of current');
+ this.unfocusAnnotation();
+ return false;
+ }
+
+ console.log('switch unfocus');
+ this.unfocusAnnotation();
+ }
+
+ this.focusAnnotation($p);
+ return false;
+ }
+
+ /*
+ * Clicking outside of focused area doesn't unfocus by default
+ * - this greatly simplifies the whole click check
+ */
+
+ /* other buttons */
+ if($e.hasClass('edit-button'))
+ this.openForEdit( this.editableFor($e) );
+
+ if($e.hasClass('accept-button'))
+ this.closeWithSave( this.editableFor($e) );
+
+ if($e.hasClass('reject-button'))
+ this.closeWithoutSave( this.editableFor($e) );
+ },
+
+ unfocusAnnotation: function()
+ {
+ if(!this.currentFocused)
+ {
+ console.log('Redundant unfocus');
+ return false;
+ }
+
+ if(this.currentOpen
+ && this.currentOpen.is("*[x-annotation-box]")
+ && this.currentOpen.parent()[0] == this.currentFocused[0])
+ {
+ console.log("Can't unfocus open box");
+ return false;
+ }
+
+ var $box = $("*[x-annotation-box]", this.currentFocused);
+ $box.css({'display': 'none'});
+ // this.currentFocused.removeAttr('x-focused');
+ // this.currentFocused.hide();
+ this.currentFocused = null;
+ },
+
+ focusAnnotation: function($e) {
+ this.currentFocused = $e;
+ var $box = $("*[x-annotation-box]", $e);
+ $box.css({'display': 'block'});
+
+ // $e.attr('x-focused', 'focused');
+ },
+
+ closeWithSave: function($e) {
+ var $edit = $e.data('edit-overlay');
+ var newText = $('textarea', $edit).val();
+
+ this.model.putXMLPart($e, newText, function($e, html) {
+ this.renderPart($e, html);
+ $edit.remove();
+ $e.removeAttr('x-open');
+ }.bind(this) );
+ this.currentOpen = null;
+ },
+
+ closeWithoutSave: function($e) {
+ var $edit = $e.data('edit-overlay');
+ $edit.remove();
+ $e.removeAttr('x-open');
+ this.currentOpen = null;
+ },
+
+ renderPart: function($e, html) {
+ // exceptions aren't good, but I don't have a better idea right now
+ if($e.attr('x-annotation-box')) {
+ // replace the whole annotation
+ var $p = $e.parent();
+ $p.html(html);
+ var $box = $('*[x-annotation-box]', $p);
+ $box.append( this.$menuTemplate.clone() );
+
+ if(this.currentFocused && $p[0] == this.currentFocused[0])
+ {
+ this.currentFocused = $p;
+ $box.css({'display': 'block'});
+ }
+
+ return;
+ }