2 * Copyright 2015 ByWater Solutions
4 * This file is part of Koha.
6 * Koha is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Koha is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Koha; if not, see <http://www.gnu.org/licenses>.
20 define( [ 'marc-record', 'koha-backend', 'preferences', 'text-marc', 'widget' ], function( MARC, KohaBackend, Preferences, TextMARC, Widget ) {
22 var NOTIFY_TIMEOUT = 250;
24 function editorCursorActivity( cm ) {
25 var editor = cm.marceditor;
26 var field = editor.getCurrentField();
29 // Set overwrite mode for tag numbers/indicators and contents of fixed fields
30 if ( field.isControlField || cm.getCursor().ch < 8 ) {
31 cm.toggleOverwrite(true);
33 cm.toggleOverwrite(false);
36 editor.onCursorActivity();
39 // This function exists to prevent inserting or partially deleting text that belongs to a
40 // widget. The 'marcAware' change source exists for other parts of the editor code to bypass
42 function editorBeforeChange( cm, change ) {
43 var editor = cm.marceditor;
44 if ( editor.textMode || change.origin == 'marcAware' || change.origin == 'widget.clearToText' ) return;
46 // FIXME: Should only cancel changes if this is a control field/subfield widget
47 if ( change.from.line !== change.to.line || Math.abs( change.from.ch - change.to.ch ) > 1 || change.text.length != 1 || change.text[0].length != 0 ) return; // Not single-char change
49 if ( change.from.ch == change.to.ch - 1 && cm.findMarksAt( { line: change.from.line, ch: change.from.ch + 1 } ).length ) {
51 } else if ( change.from.ch == change.to.ch && cm.findMarksAt(change.from).length && !change.text[0] == '‡' ) {
56 function editorChanges( cm, changes ) {
57 var editor = cm.marceditor;
58 if ( editor.textMode ) return;
60 for (var i = 0; i < changes.length; i++) {
61 var change = changes[i];
63 var origin = change.from.line;
64 var newTo = CodeMirror.changeEnd(change);
66 for (var delLine = origin; delLine <= change.to.line; delLine++) {
67 // Line deleted; currently nothing to do
70 for (var line = origin; line <= newTo.line; line++) {
71 if ( Preferences.user.fieldWidgets ) Widget.UpdateLine( cm.marceditor, line );
72 if ( change.origin != 'setValue' && change.origin != 'marcWidgetPrefill' && change.origin != 'widget.clearToText' ) {
73 cm.addLineClass( line, 'wrapper', 'modified-line' );
74 editor.modified = true;
79 Widget.ActivateAt( cm, cm.getCursor() );
80 cm.marceditor.startNotify();
83 function editorSetOverwriteMode( cm, newState ) {
84 var editor = cm.marceditor;
86 editor.overwriteMode = newState;
89 // Editor helper functions
90 function activateTabPosition( cm, pos, idx ) {
91 // Allow tabbing to as-yet-nonexistent positions
92 var lenDiff = pos.ch - cm.getLine( pos.line ).length;
95 while ( lenDiff-- > 0 ) extra += ' ';
96 if ( pos.prefill ) extra += pos.prefill;
97 cm.replaceRange( extra, { line: pos.line } );
101 Widget.ActivateAt( cm, pos, idx );
104 function getTabPositions( editor, cur ) {
105 cur = cur || editor.cm.getCursor();
106 var field = editor.getFieldAt( cur.line );
109 if ( field.isControlField ) {
110 var positions = [ { ch: 0 }, { ch: 4 } ];
112 $.each( positions, function( undef, pos ) {
118 var positions = [ { ch: 0 }, { ch: 4, prefill: '_' }, { ch: 6, prefill: '_' } ];
120 $.each( positions, function( undef, pos ) {
123 $.each( field.getSubfields(), function( undef, subfield ) {
124 positions.push( { line: cur.line, ch: subfield.contentsStart } );
127 // Allow to tab to start of empty field
128 if ( field.getSubfields().length == 0 ) {
129 positions.push( { line: cur.line, ch: 8 } );
138 var _editorKeys = {};
140 _editorKeys[insert_copyright] = function( cm ) {
141 cm.replaceRange( '©', cm.getCursor() );
144 _editorKeys[insert_copyright_sound] = function( cm ) {
145 cm.replaceRange( '℗', cm.getCursor() );
148 _editorKeys[new_line] = function( cm ) {
149 var cursor = cm.getCursor();
150 cm.replaceRange( '\n', { line: cursor.line }, null, 'marcAware' );
151 cm.setCursor( { line: cursor.line + 1, ch: 0 } );
154 _editorKeys[line_break] = function( cm ) {
155 var cur = cm.getCursor();
157 cm.replaceRange( "\n", cur, null );
160 _editorKeys[delete_field] = function( cm ) {
161 // Delete line (or cut)
162 if ( cm.somethingSelected() ) return true;
163 var curLine = cm.getLine( cm.getCursor().line );
165 $("#clipboard").prepend('<option>'+curLine+'</option>');
167 cm.execCommand('deleteLine');
170 _editorKeys[link_authorities] = function( cm ) {
171 // Launch the auth search popup
172 var field = cm.marceditor.getCurrentField();
174 if ( !field ) return;
175 if ( authInfo[field.tag] == undefined ) return;
176 authtype = authInfo[field.tag].authtypecode;
177 index = 'tag_'+field.tag+'_rancor';
178 var mainmainstring = '';
179 if( field.getSubfields( authInfo[field.tag].subfield ).length != 0 ){
180 mainmainstring += field.getSubfields( authInfo[field.tag].subfield )[0].text;
183 var subfields = field.getSubfields();
185 for(i=0;i < subfields.length ;i++){
186 if ( authInfo[field.tag].subfield == subfields[i].code ) continue;
187 if( subfields[i].code == '9' ) continue;
188 mainstring += subfields[i].text+' ';
190 newin=window.open("../authorities/auth_finder.pl?source=biblio&authtypecode="+authtype+"&index="+index+"&value_mainstr="+encodeURI(mainmainstring)+"&value_main="+encodeURI(mainstring), "_blank",'width=700,height=550,toolbar=false,scrollbars=yes');
194 _editorKeys[delete_subfield] = function( cm ) {
196 var field = cm.marceditor.getCurrentField();
197 if ( !field ) return;
199 var curCursor = cm.getCursor();
200 var subfield = field.getSubfieldAt( curCursor().ch );
201 var subfieldText= cm.getRange({line:curCursor.line,ch:subfield.start},{line:curCursor.line,ch:subfield.end});
203 $("#clipboard").prepend('<option>'+subfieldText+'</option>');
208 _editorKeys[copy_line] = function( cm ) {
210 if ( cm.somethingSelected() ) return true;
211 var curLine = cm.getLine( cm.getCursor().line );
212 $("#clipboard").prepend('<option>'+curLine+'</option>');
215 _editorKeys[copy_subfield] = function( cm ) {
217 var field = cm.marceditor.getCurrentField();
218 if ( !field ) return;
220 var curCursor = cm.getCursor();
221 var subfield = field.getSubfieldAt( curCursor().ch );
222 var subfieldText= cm.getRange({line:curCursor.line,ch:subfield.start},{line:curCursor.line,ch:subfield.end});
224 $("#clipboard").prepend('<option>'+subfieldText+'</option>');
228 _editorKeys[paste_line] = function( cm ) {
229 // Paste line from "clipboard"
230 if ( cm.somethingSelected() ) return true;
231 var cBoard = document.getElementById("clipboard");
232 var strUser = cBoard.options[cBoard.selectedIndex].text;
233 cm.replaceRange( strUser, cm.getCursor(), null );
236 _editorKeys[insert_line] = function( cm ) {
237 // Copy line and insert below
238 if ( cm.somethingSelected() ) return true;
239 var curLine = cm.getLine( cm.getCursor().line );
240 cm.execCommand('newlineAndIndent');
241 cm.replaceRange( curLine, cm.getCursor(), null );
244 _editorKeys[next_position] = function( cm ) {
245 // Move through parts of tag/fixed fields
246 var positions = getTabPositions( cm.marceditor );
247 var cur = cm.getCursor();
249 for ( var i = 0; i < positions.length; i++ ) {
250 if ( positions[i].ch > cur.ch ) {
251 activateTabPosition( cm, positions[i] );
256 cm.setCursor( { line: cur.line + 1, ch: 0 } );
259 _editorKeys[prev_position] = function( cm ) {
260 // Move backwards through parts of tag/fixed fields
261 var positions = getTabPositions( cm.marceditor );
262 var cur = cm.getCursor();
264 for ( var i = positions.length - 1; i >= 0; i-- ) {
265 if ( positions[i].ch < cur.ch ) {
266 activateTabPosition( cm, positions[i], -1 );
271 if ( cur.line == 0 ) return;
273 var prevPositions = getTabPositions( cm.marceditor, { line: cur.line - 1, ch: cm.getLine( cur.line - 1 ).length } );
275 if ( prevPositions.length ) {
276 activateTabPosition( cm, prevPositions[ prevPositions.length - 1 ], -1 );
278 cm.setCursor( { line: cur.line - 1, ch: 0 } );
282 _editorKeys[insert_delimiter] = function(cm){
283 var cur = cm.getCursor();
285 cm.replaceRange( "‡", cur, null );
288 _editorKeys[toggle_keyboard] = function( cm ) {
289 let keyboard = $(cm.getInputField()).getkeyboard();
290 keyboard.isVisible()?keyboard.close():keyboard.reveal();
293 // The objects below are part of a field/subfield manipulation API, accessed through the base
296 // Each one is tied to a particular line; this means that using a field or subfield object after
297 // any other changes to the record will cause entertaining explosions. The objects are meant to
298 // be temporary, and should only be reused with great care. The macro code does this only
299 // because it is careful to dispose of the object after any other updates.
301 // Note, however, tha you can continue to use a field object after changing subfields. It's just
302 // the subfield objects that become invalid.
304 // This is an exception raised by the EditorSubfield and EditorField when an invalid change is
306 function FieldError(line, message) {
308 this.message = message;
311 FieldError.prototype.toString = function() {
312 return 'FieldError(' + this.line + ', "' + this.message + '")';
315 // This is the temporary object for a particular subfield in a field. Any change to any other
316 // subfields will invalidate this subfield object.
317 function EditorSubfield( field, index, start, end ) {
323 if ( this.field.isControlField ) {
324 this.contentsStart = start;
327 this.contentsStart = start + 2;
328 this.code = this.field.contents.substr( this.start + 1, 1 );
333 var marks = this.cm.findMarksAt( { line: field.line, ch: this.contentsStart } );
334 if ( marks[0] && marks[0].widget ) {
335 this.widget = marks[0].widget;
337 this.text = this.widget.text;
338 this.setText = this.widget.setText;
339 this.getFixed = this.widget.getFixed;
340 this.setFixed = this.widget.setFixed;
343 this.text = this.field.contents.substr( this.contentsStart, end - this.contentsStart );
347 $.extend( EditorSubfield.prototype, {
348 _invalid: function() {
349 return this.field._subfieldsInvalid();
353 this.cm.replaceRange( "", { line: this.field.line, ch: this.start }, { line: this.field.line, ch: this.end }, 'marcAware' );
356 this.cm.setCursor( { line: this.field.line, ch: this.contentsStart } );
358 focusEnd: function() {
359 this.cm.setCursor( { line: this.field.line, ch: this.end } );
361 getText: function() {
364 setText: function( text ) {
365 if ( !this._invalid() ) throw new FieldError( this.field.line, 'subfield invalid' );
366 this.cm.replaceRange( text, { line: this.field.line, ch: this.contentsStart }, { line: this.field.line, ch: this.end }, 'marcAware' );
367 this.field._invalidateSubfields();
371 function EditorField( editor, line ) {
372 this.editor = editor;
378 this.tag = this.contents.substr( 0, 3 );
379 this.isControlField = ( this.tag < '010' );
381 if ( this.isControlField ) {
382 this._ind1 = this.contents.substr( 4, 1 );
383 this._ind2 = this.contents.substr( 6, 1 );
389 this.subfields = null;
392 $.extend( EditorField.prototype, {
393 _subfieldsInvalid: function() {
394 return !this.subfields;
396 _invalidateSubfields: function() {
397 this._subfields = null;
400 _updateInfo: function() {
401 this.info = this.editor.getLineInfo( { line: this.line, ch: 0 } );
402 if ( this.info == null ) throw new FieldError( 'Invalid field' );
403 this.contents = this.info.contents;
405 _scanSubfields: function() {
408 if ( this.isControlField ) {
409 this._subfields = [ new EditorSubfield( this, 0, 4, this.contents.length ) ];
412 var subfields = this.info.subfields;
413 this._subfields = [];
415 for (var i = 0; i < this.info.subfields.length; i++) {
416 var end = i == subfields.length - 1 ? this.contents.length : subfields[i+1].ch;
418 this._subfields.push( new EditorSubfield( this, i, subfields[i].ch, end ) );
424 this.cm.replaceRange( "", { line: this.line, ch: 0 }, { line: this.line + 1, ch: 0 }, 'marcAware' );
427 this.cm.setCursor( { line: this.line, ch: 0 } );
432 getText: function() {
435 $.each( this.getSubfields(), function() {
436 if ( this.code != '@' ) result += '‡' + this.code;
438 result += this.getText();
443 setText: function( text ) {
444 var indicator_match = /^([_ 0-9])([_ 0-9])\‡/.exec( text );
445 if ( indicator_match ) {
446 text = text.substr(2);
447 this.setIndicator1( indicator_match[1] );
448 this.setIndicator2( indicator_match[2] );
451 this.cm.replaceRange( text, { line: this.line, ch: this.isControlField ? 4 : 8 }, { line: this.line }, 'marcAware' );
452 this._invalidateSubfields();
457 getIndicator1: function() {
460 getIndicator2: function() {
463 setIndicator1: function(val) {
464 if ( this.isControlField ) throw new FieldError('Cannot set indicators on control field');
466 this._ind1 = ( !val || val == ' ' ) ? '_' : val;
467 this.cm.replaceRange( this._ind1, { line: this.line, ch: 4 }, { line: this.line, ch: 5 }, 'marcAware' );
471 setIndicator2: function(val) {
472 if ( this.isControlField ) throw new FieldError('Cannot set indicators on control field');
474 this._ind2 = ( !val || val == ' ' ) ? '_' : val;
475 this.cm.replaceRange( this._ind2, { line: this.line, ch: 6 }, { line: this.line, ch: 7 }, 'marcAware' );
480 appendSubfield: function( code ) {
481 if ( this.isControlField ) throw new FieldError('Cannot add subfields to control field');
483 this._invalidateSubfields();
484 this.cm.replaceRange( '‡' + code, { line: this.line }, null, 'marcAware' );
485 var subfields = this.getSubfields();
487 return subfields[ subfields.length - 1 ];
489 insertSubfield: function( code, position ) {
490 if ( this.isControlField ) throw new FieldError('Cannot add subfields to control field');
492 position = position || 0;
494 var subfields = this.getSubfields();
495 this._invalidateSubfields();
496 this.cm.replaceRange( '‡' + code, { line: this.line, ch: subfields[position] ? subfields[position].start : null }, null, 'marcAware' );
497 subfields = this.getSubfields();
499 return subfields[ position ];
501 getSubfields: function( code ) {
502 if ( !this._subfields ) this._scanSubfields();
503 if ( code == null ) return this._subfields;
507 $.each( this._subfields, function() {
508 if ( code == null || this.code == code ) result.push(this);
513 getFirstSubfield: function( code ) {
514 var result = this.getSubfields( code );
516 return ( result && result.length ) ? result[0] : null;
518 getSubfieldAt: function( ch ) {
519 var subfields = this.getSubfields();
521 for (var i = 0; i < subfields.length; i++) {
522 if ( subfields[i].start < ch && subfields[i].end >= ch ) return subfields[i];
527 function MARCEditor( options ) {
528 this.frameworkcode = '';
530 this.cm = CodeMirror(
533 extraKeys: _editorKeys,
535 'modified-line-gutter',
540 nonRepeatableTags: KohaBackend.GetTagsBy( '', 'repeatable', '0' ),
541 nonRepeatableSubfields: KohaBackend.GetSubfieldsBy( '', 'repeatable', '0' )
545 var inf = this.cm.getInputField();
547 var kb = $(inf).keyboard({
548 //keyBinding: "mousedown touchstart",
552 autoAcceptOnEsc: true,
557 of: $("#statusbar"), // optional - null (attach to input/textarea) or a jQuery object (attach elsewhere)
560 at2: 'center bottom' // used when "usePreview" is false (centers keyboard at bottom of the input/textarea)
562 beforeInsert: function(evnt, keyboard, elem, txt) {
563 var position = self.cm.getCursor();
565 self.cm.execCommand("delCharBefore");
567 if (txt === "\b" && position.ch === 0 && position.line !== 0) {
568 elem.value = self.cm.getLine(position.line) || "";
573 visible: function() {
574 $('#set-keyboard-layout').removeClass('hide');
576 hidden: function(e, keyboard, el, accepted) {
578 $('#set-keyboard-layout').addClass('hide');
583 Object.keys($.keyboard.layouts).forEach(function(layout) {
584 var div = $('#keyboard-layout .layouts').append('<div class="layout" data-layout="'+layout+'" data-name="'+($.keyboard.layouts[layout].name||layout)+'" >'+($.keyboard.layouts[layout].name||layout)+'</div>')
585 if(kb.layout == layout) {
586 div.addClass('active');
589 $('#keyboard-layout')
590 .on('show.bs.modal', function() {
592 $('#keyboard-layout .filter').focus();
593 $('#set-keyboard-layout').removeClass('hide');
595 .on('hide.bs.modal', function() {
596 !kb.isVisible() && kb.reveal();
598 $('#keyboard-layout .layout').click(function(event) {
599 $('#keyboard-layout .layout').removeClass('active');
600 $(this).addClass('active');
601 var layout = $(this).data().layout;
603 $('#keyboard-layout').modal('hide');
604 $('#keyboard-layout .filter').val('');
605 $('#keyboard-layout .layout').show();
607 $('#keyboard-layout .filter').keyup(function() {
608 var val = $(this).val();
609 if(!val||!val.length) return $('#keyboard-layout .layout').show();
610 var filter = new RegExp(val, 'i');
611 $('#keyboard-layout .layout').hide();
612 $('#keyboard-layout .layout').each(function() {
613 var name = $(this).data().name;
614 if(filter.test(name)) $(this).show();
618 this.cm.marceditor = this;
620 this.cm.on( 'beforeChange', editorBeforeChange );
621 this.cm.on( 'changes', editorChanges );
622 this.cm.on( 'cursorActivity', editorCursorActivity );
623 this.cm.on( 'overwriteToggle', editorSetOverwriteMode );
625 this.onCursorActivity = options.onCursorActivity;
627 this.subscribers = [];
628 this.subscribe( function( marceditor ) {
629 Widget.Notify( marceditor );
633 MARCEditor.FieldError = FieldError;
635 $.extend( MARCEditor.prototype, {
636 setUseWidgets: function( val ) {
638 for ( var line = 0; line <= this.cm.lastLine(); line++ ) {
639 Widget.UpdateLine( this, line );
642 $.each( this.cm.getAllMarks(), function( undef, mark ) {
643 if ( mark.widget ) mark.widget.clearToText();
652 getCursor: function() {
653 return this.cm.getCursor();
656 refresh: function() {
660 setFrameworkCode: function( code, updateFields, callback ) {
661 this.frameworkcode = code;
662 $( 'a.change-framework i.selected' ).addClass( 'hidden' );
663 $( 'a.change-framework i.unselected' ).removeClass( 'hidden' );
664 $( 'a.change-framework[data-frameworkcode="' + code + '"] i.unselected' ).addClass( 'hidden' );
665 $( 'a.change-framework[data-frameworkcode="' + code + '"] i.selected' ).removeClass( 'hidden ');
667 KohaBackend.InitFramework( code, function ( error ) {
668 cm.setOption( 'mode', {
670 nonRepeatableTags: KohaBackend.GetTagsBy( code, 'repeatable', '0' ),
671 nonRepeatableSubfields: KohaBackend.GetSubfieldsBy( code, 'repeatable', '0' )
673 if ( updateFields ) {
674 var record = TextMARC.TextToRecord( cm.getValue() );
675 KohaBackend.FillRecord( code, record );
676 cm.setValue( TextMARC.RecordToText(record) );
682 displayRecord: function( record ) {
683 this.cm.setValue( TextMARC.RecordToText(record) );
684 this.modified = false;
685 this.setFrameworkCode(
686 typeof record.frameworkcode !== 'undefined' ? record.frameworkcode : '',
689 if ( typeof error !== 'undefined' ) {
690 humanMsg.displayAlert( _(error), { className: 'humanError' } );
696 getRecord: function() {
697 this.textMode = true;
699 $.each( this.cm.getAllMarks(), function( undef, mark ) {
700 if ( mark.widget ) mark.widget.clearToText();
702 var record = TextMARC.TextToRecord( this.cm.getValue() );
703 for ( var line = 0; line <= this.cm.lastLine(); line++ ) {
704 if ( Preferences.user.fieldWidgets ) Widget.UpdateLine( this, line );
707 this.textMode = false;
709 record.frameworkcode = this.frameworkcode;
713 getLineInfo: function( pos ) {
714 var contents = this.cm.getLine( pos.line );
715 if ( contents == null ) return {};
717 var tagNumber = contents.match( /^([A-Za-z0-9]{3})/ );
719 if ( !tagNumber ) return null; // No tag at all on this line
720 tagNumber = tagNumber[1];
722 if ( tagNumber < '010' ) return { tagNumber: tagNumber, contents: contents }; // No current subfield
724 var matcher = /‡([a-z0-9%])/g;
730 while ( ( match = matcher.exec(contents) ) ) {
731 subfields.push( { code: match[1], ch: match.index } );
732 if ( match.index < pos.ch ) currentSubfield = match[1];
735 return { tagNumber: tagNumber, subfields: subfields, currentSubfield: currentSubfield, contents: contents };
738 addError: function( line, error ) {
742 if ( line == null ) {
744 options.above = true;
747 $.each( this.cm.getLineHandle(line).widgets || [], function( undef, widget ) {
748 if ( !widget.isErrorMarker ) return;
752 $( widget.node ).append( '; ' + error );
760 var node = $( '<div class="structure-error"><i class="fa fa-remove"></i> ' + error + '</div>' )[0];
761 var widget = this.cm.addLineWidget( line, node, options );
764 widget.isErrorMarker = true;
767 removeErrors: function() {
768 for ( var line = 0; line < this.cm.lineCount(); line++ ) {
769 $.each( this.cm.getLineHandle( line ).widgets || [], function( undef, lineWidget ) {
770 if ( lineWidget.isErrorMarker ) lineWidget.clear();
775 startNotify: function() {
776 if ( this.notifyTimeout ) clearTimeout( this.notifyTimeout );
777 this.notifyTimeout = setTimeout( $.proxy( function() {
780 this.notifyTimeout = null;
781 }, this ), NOTIFY_TIMEOUT );
784 notifyAll: function() {
785 $.each( this.subscribers, $.proxy( function( undef, subscriber ) {
790 subscribe: function( subscriber ) {
791 this.subscribers.push( subscriber );
794 createField: function( tag, line ) {
795 var contents = tag + ( tag < '010' ? ' ' : ' _ _ ' );
797 if ( line > this.cm.lastLine() ) {
798 contents = '\n' + contents;
800 contents = contents + '\n';
803 this.cm.replaceRange( contents, { line: line, ch: 0 }, null, 'marcAware' );
805 return new EditorField( this, line );
808 createFieldOrdered: function( tag ) {
811 for ( line = 0; (contents = this.cm.getLine(line)); line++ ) {
812 if ( contents && contents.substr(0, 3) > tag ) break;
815 return this.createField( tag, line );
818 createFieldGrouped: function( tag ) {
819 // Control fields should be inserted in actual order, whereas other fields should be
821 if ( tag < '010' ) return this.createFieldOrdered( tag );
825 for ( line = 0; (contents = this.cm.getLine(line)); line++ ) {
826 if ( contents && contents[0] > tag[0] ) break;
829 return this.createField( tag, line );
832 getFieldAt: function( line ) {
834 return new EditorField( this, line );
840 getCurrentField: function() {
841 return this.getFieldAt( this.cm.getCursor().line );
844 getFields: function( tag ) {
847 if ( tag != null ) tag += ' ';
849 for ( var line = 0; line < this.cm.lineCount(); line++ ) {
850 if ( tag && this.cm.getLine(line).substr( 0, 4 ) != tag ) continue;
852 // If this throws a FieldError, pretend it doesn't exist
854 result.push( new EditorField( this, line ) );
856 if ( !( e instanceof FieldError ) ) throw e;
863 getFirstField: function( tag ) {
864 var result = this.getFields( tag );
866 return ( result && result.length ) ? result[0] : null;
869 getAllFields: function( tag ) {
870 return this.getFields( null );