| 1 | /*! |
| 2 | * VisualEditor ContentEditable Annotation class. |
| 3 | * |
| 4 | * @copyright See AUTHORS.txt |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * Generic ContentEditable annotation. |
| 9 | * |
| 10 | * This is an abstract class, annotations should extend this and call this constructor from their |
| 11 | * constructor. You should not instantiate this class directly. |
| 12 | * |
| 13 | * Subclasses of ve.dm.Annotation should have a corresponding subclass here that controls rendering. |
| 14 | * |
| 15 | * @abstract |
| 16 | * @extends ve.ce.View |
| 17 | * |
| 18 | * @constructor |
| 19 | * @param {ve.dm.Annotation} model Model to observe |
| 20 | * @param {ve.ce.ContentBranchNode} [parentNode] Node rendering this annotation |
| 21 | * @param {Object} [config] Configuration options |
| 22 | */ |
| 23 | ve.ce.Annotation = function VeCeAnnotation( model, parentNode, config ) { |
| 24 | // Parent constructor |
| 25 | ve.ce.Annotation.super.call( this, model, config ); |
| 26 | |
| 27 | // Properties |
| 28 | this.parentNode = parentNode || null; |
| 29 | |
| 30 | this.$element |
| 31 | // Make data available before setup |
| 32 | .data( 'view', this ) |
| 33 | .addClass( 've-ce-annotation' ); |
| 34 | }; |
| 35 | |
| 36 | /* Inheritance */ |
| 37 | |
| 38 | OO.inheritClass( ve.ce.Annotation, ve.ce.View ); |
| 39 | |
| 40 | /* Static Properties */ |
| 41 | |
| 42 | ve.ce.Annotation.static.tagName = 'span'; |
| 43 | |
| 44 | /** |
| 45 | * Annotations which can be active get a ve-ce-annotation-active class when focused. |
| 46 | */ |
| 47 | ve.ce.Annotation.static.canBeActive = false; |
| 48 | |
| 49 | /* Static Methods */ |
| 50 | |
| 51 | /** |
| 52 | * Get a plain text description. |
| 53 | * |
| 54 | * @static |
| 55 | * @inheritable |
| 56 | * @param {ve.dm.Annotation} annotation Annotation model |
| 57 | * @return {string} Description of annotation |
| 58 | */ |
| 59 | ve.ce.Annotation.static.getDescription = function () { |
| 60 | return ''; |
| 61 | }; |
| 62 | |
| 63 | /* Methods */ |
| 64 | |
| 65 | /** |
| 66 | * Get the content branch node this annotation is rendered in, if any. |
| 67 | * |
| 68 | * @return {ve.ce.ContentBranchNode|null} Content branch node or null if none |
| 69 | */ |
| 70 | ve.ce.Annotation.prototype.getParentNode = function () { |
| 71 | return this.parentNode; |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * @inheritdoc |
| 76 | */ |
| 77 | ve.ce.Annotation.prototype.getModelHtmlDocument = function () { |
| 78 | return this.parentNode && this.parentNode.getModelHtmlDocument(); |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * Append a child node to the annotation |
| 83 | * |
| 84 | * @param {Node} childNode Child node to append |
| 85 | */ |
| 86 | ve.ce.Annotation.prototype.appendChild = function ( childNode ) { |
| 87 | this.$element[ 0 ].appendChild( childNode ); |
| 88 | }; |
| 89 | |
| 90 | /** |
| 91 | * Get the container into which annotation contents should be appended |
| 92 | * |
| 93 | * @return {HTMLElement} Content container |
| 94 | */ |
| 95 | ve.ce.Annotation.prototype.getContentContainer = function () { |
| 96 | return this.$element[ 0 ]; |
| 97 | }; |
| 98 | |
| 99 | /** |
| 100 | * Attach completed contents to the annotation as descendant nodes, if not already attached |
| 101 | * |
| 102 | * No further contents should be appended into the content container after calling this |
| 103 | */ |
| 104 | ve.ce.Annotation.prototype.attachContents = function () { |
| 105 | // Do nothing; already attached |
| 106 | }; |
| 107 | |
| 108 | /** |
| 109 | * Append the completed annotation to a parent node |
| 110 | * |
| 111 | * #attachContents should have been called first |
| 112 | * |
| 113 | * @param {Node} node Parent node |
| 114 | */ |
| 115 | ve.ce.Annotation.prototype.appendTo = function ( node ) { |
| 116 | node.appendChild( this.$element[ 0 ] ); |
| 117 | }; |
| 118 | |
| 119 | /** |
| 120 | * Check if the annotation can be active |
| 121 | * |
| 122 | * @return {boolean} |
| 123 | */ |
| 124 | ve.ce.Annotation.prototype.canBeActive = function () { |
| 125 | return this.constructor.static.canBeActive; |
| 126 | }; |
| 127 | |
| 128 | /** |
| 129 | * Release all memory |
| 130 | */ |
| 131 | ve.ce.Annotation.prototype.destroy = function () { |
| 132 | this.parentNode = null; |
| 133 | |
| 134 | // Parent method |
| 135 | ve.ce.Annotation.super.prototype.destroy.call( this ); |
| 136 | }; |