| /** |
| * Get information on matches, |
| * generate annotation tables and trees. |
| * |
| * @author Nils Diewald |
| */ |
| /* |
| * - Highlight (at least mark as bold) the match |
| * - Scroll to match vertically per default |
| */ |
| define([ |
| 'match/infolayer', |
| 'match/info', |
| 'util' |
| ], function (infoLayerClass, |
| infoClass) { |
| |
| // Localization values |
| var loc = KorAP.Locale; |
| loc.ADDTREE = loc.ADDTREE || 'Add tree view'; |
| loc.SHOWINFO = loc.SHOWINFO || 'Show information'; |
| loc.CLOSE = loc.CLOSE || 'Close'; |
| |
| // KorAP._AvailableRE = new RegExp("^([^\/]+?)\/([^=]+?)(?:=(spans|rels|tokens))?$"); |
| // KorAP._TermRE = new RegExp("^(?:([^\/]+?)\/)?([^:]+?):(.+?)$"); |
| var _matchTerms = ['corpusID', 'docID', 'textID', 'matchID', 'available']; |
| |
| /** |
| * Match object |
| */ |
| return { |
| |
| /** |
| * Create a new annotation object. |
| * Expects an array of available foundry/layer=type terms. |
| * Supported types are 'spans', 'tokens' and 'rels'. |
| */ |
| create : function (match) { |
| return Object.create(this)._init(match); |
| }, |
| |
| /** |
| * Initialize match. |
| */ |
| _init : function (match) { |
| this._element = null; |
| |
| // No match defined |
| if (arguments.length < 1 || |
| match === null || |
| match === undefined) { |
| throw new Error('Missing parameters'); |
| } |
| |
| // Match defined as a node |
| else if (match instanceof Node) { |
| this._element = match; |
| |
| // Circular reference !! |
| match["_match"] = this; |
| |
| this.corpusID = match.getAttribute('data-corpus-id'), |
| this.docID = match.getAttribute('data-doc-id'), |
| this.textID = match.getAttribute('data-text-id'), |
| this.matchID = match.getAttribute('data-match-id') |
| |
| // List of available annotations |
| this.available = match.getAttribute('data-available-info').split(' '); |
| } |
| |
| // Match as an object |
| else { |
| |
| // Iterate over allowed match terms |
| for (var i in _matchTerms) { |
| var term = _matchTerms[i]; |
| if (match[term] !== undefined) { |
| this[term] = match[term]; |
| } |
| else { |
| this[term] = undefined; |
| } |
| }; |
| }; |
| |
| this._available = { |
| tokens : [], |
| spans : [], |
| rels : [] |
| }; |
| |
| // Iterate over info layers |
| for (var i = 0; i < this.available.length; i++) { |
| var term = this.available[i]; |
| |
| // Create info layer objects |
| try { |
| var layer = infoLayerClass.create(term); |
| this._available[layer.type].push(layer); |
| } |
| catch (e) { |
| continue; |
| }; |
| }; |
| |
| return this; |
| }, |
| |
| |
| /** |
| * Return a list of parseable tree annotations. |
| */ |
| getSpans : function () { |
| return this._available.spans; |
| }, |
| |
| |
| /** |
| * Return a list of parseable token annotations. |
| */ |
| getTokens : function () { |
| return this._available.tokens; |
| }, |
| |
| |
| /** |
| * Return a list of parseable relation annotations. |
| */ |
| getRels : function () { |
| return this._available.rels; |
| }, |
| |
| /** |
| * Open match |
| */ |
| open : function () { |
| |
| // Add actions unless it's already activated |
| var element = this._element; |
| |
| // There is an element to open |
| if (this._element === undefined || this._element === null) |
| return false; |
| |
| // The element is already opened |
| if (element.classList.contains('active')) |
| return false; |
| |
| // Add active class to element |
| element.classList.add('active'); |
| |
| // Create action buttons |
| var ul = document.createElement('ul'); |
| ul.classList.add('action', 'right'); |
| element.appendChild(ul); |
| |
| // Use localization |
| var loc = KorAP.Locale; |
| |
| // Add close button |
| var close = document.createElement('li'); |
| close.appendChild(document.createElement('span')) |
| .appendChild(document.createTextNode(loc.CLOSE)); |
| close.classList.add('close'); |
| close.setAttribute('title', loc.CLOSE); |
| |
| // Add info button |
| var info = document.createElement('li'); |
| info.appendChild(document.createElement('span')) |
| .appendChild(document.createTextNode(loc.SHOWINFO)); |
| info.classList.add('info'); |
| info.setAttribute('title', loc.SHOWINFO); |
| |
| var that = this; |
| |
| // Close match |
| close.addEventListener('click', function (e) { |
| e.halt(); |
| that.close() |
| }); |
| |
| // Add information, unless it already exists |
| info.addEventListener('click', function (e) { |
| e.halt(); |
| that.info().toggle(); |
| }); |
| |
| ul.appendChild(close); |
| ul.appendChild(info); |
| |
| return true; |
| }, |
| |
| /** |
| * Close info view |
| */ |
| close : function () { |
| this._element.classList.remove('active'); |
| |
| /* |
| if (this._info !== undefined) { |
| this._info.destroy(); |
| }; |
| */ |
| }, |
| |
| |
| /** |
| * Get and open associated match info. |
| */ |
| info : function () { |
| |
| // Create match info |
| if (this._info === undefined) |
| this._info = infoClass.create(this); |
| |
| // There is an element to append |
| if (this._element === undefined || |
| this._element === null) |
| return this._info; |
| |
| // Info is already activated |
| if (this._info._element !== undefined) |
| return this._info; |
| |
| return this._info; |
| }, |
| |
| |
| /** |
| * Get match element. |
| */ |
| element : function () { |
| |
| // May be null |
| return this._element; |
| } |
| }; |
| }); |