Lines 1-38
Link Here
|
|
|
1 |
/** |
2 |
* Copyright 2015 ByWater Solutions |
3 |
* |
4 |
* This file is part of Koha. |
5 |
* |
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. |
10 |
* |
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. |
15 |
* |
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>. |
18 |
*/ |
19 |
|
1 |
define( [ 'marc-record', 'koha-backend', 'preferences', 'text-marc', 'widget' ], function( MARC, KohaBackend, Preferences, TextMARC, Widget ) { |
20 |
define( [ 'marc-record', 'koha-backend', 'preferences', 'text-marc', 'widget' ], function( MARC, KohaBackend, Preferences, TextMARC, Widget ) { |
2 |
var NOTIFY_TIMEOUT = 250; |
21 |
var NOTIFY_TIMEOUT = 250; |
3 |
|
22 |
|
4 |
function editorCursorActivity( cm ) { |
23 |
function editorCursorActivity( cm ) { |
5 |
var editor = cm.marceditor; |
24 |
var editor = cm.marceditor; |
6 |
if ( editor.textMode ) return; |
25 |
var field = editor.getCurrentField(); |
7 |
|
26 |
if ( !field ) return; |
8 |
$('#status-tag-info').empty(); |
|
|
9 |
$('#status-subfield-info').empty(); |
10 |
|
11 |
var info = editor.getLineInfo( cm.getCursor() ); |
12 |
|
13 |
if ( !info ) return; // No tag at all on this line |
14 |
|
15 |
var taginfo = KohaBackend.GetTagInfo( '', info.tagNumber ); |
16 |
$('#status-tag-info').html( '<strong>' + info.tagNumber + ':</strong> ' ); |
17 |
|
18 |
if ( taginfo ) { |
19 |
$('#status-tag-info').append( taginfo.lib ); |
20 |
|
27 |
|
21 |
if ( !info.currentSubfield ) return; // No current subfield |
28 |
// Set overwrite mode for tag numbers/indicators and contents of fixed fields |
22 |
|
29 |
if ( field.isControlField || cm.getCursor().ch < 8 ) { |
23 |
var subfieldinfo = taginfo.subfields[info.currentSubfield]; |
30 |
cm.toggleOverwrite(true); |
24 |
$('#status-subfield-info').html( '<strong>$' + info.currentSubfield + ':</strong> ' ); |
|
|
25 |
|
26 |
if ( subfieldinfo ) { |
27 |
$('#status-subfield-info').append( subfieldinfo.lib ); |
28 |
} else { |
29 |
$('#status-subfield-info').append( '<em>' + _("Unknown subfield") + '</em>' ); |
30 |
} |
31 |
} else { |
31 |
} else { |
32 |
$('#status-tag-info').append( '<em>' + _("Unknown tag") + '</em>' ); |
32 |
cm.toggleOverwrite(false); |
33 |
} |
33 |
} |
|
|
34 |
|
35 |
editor.onCursorActivity(); |
34 |
} |
36 |
} |
35 |
|
37 |
|
|
|
38 |
// This function exists to prevent inserting or partially deleting text that belongs to a |
39 |
// widget. The 'marcAware' change source exists for other parts of the editor code to bypass |
40 |
// this check. |
36 |
function editorBeforeChange( cm, change ) { |
41 |
function editorBeforeChange( cm, change ) { |
37 |
var editor = cm.marceditor; |
42 |
var editor = cm.marceditor; |
38 |
if ( editor.textMode || change.origin == 'marcAware' ) return; |
43 |
if ( editor.textMode || change.origin == 'marcAware' ) return; |
Lines 62-76
define( [ 'marc-record', 'koha-backend', 'preferences', 'text-marc', 'widget' ],
Link Here
|
62 |
} |
67 |
} |
63 |
|
68 |
|
64 |
var startLine, endLine; |
69 |
var startLine, endLine; |
65 |
if ( change.text.length == 2 && from.line == to.line && from.ch == to.ch) { |
70 |
if ( change.text.length == 2 && from.line == to.line && from.ch == to.ch ) { |
66 |
if ( from.ch == 0 ) { |
71 |
if ( from.ch == 0 ) { |
67 |
startLine = endLine = from.line; |
72 |
startLine = endLine = from.line; |
68 |
} else if ( from.ch == cm.getLine(from.line).length ){ |
73 |
} else if ( from.ch == cm.getLine(from.line).length ){ |
69 |
startLine = endLine = from.line + 1; |
74 |
startLine = endLine = from.line + 1; |
70 |
} |
75 |
} |
71 |
} else { |
76 |
} else { |
72 |
startLine = (from.ch == cm.getLine(from.line).length && from.line < to.line) ? Math.min(cm.lastLine(), from.line + 1) : from.line; |
77 |
startLine = ( from.ch == cm.getLine( from.line ).length && from.line < to.line ) ? Math.min( cm.lastLine(), from.line + 1 ) : from.line; |
73 |
endLine = ((to.ch == 0 && from.line < to.line) ? Math.max(to.line - 1, 0) : to.line) + change.text.length - 1; |
78 |
endLine = ( ( to.ch == 0 && from.line < to.line ) ? Math.max( to.line - 1, 0 ) : to.line ) + change.text.length - 1; |
74 |
} |
79 |
} |
75 |
|
80 |
|
76 |
for ( var line = startLine; line <= endLine; line++ ) { |
81 |
for ( var line = startLine; line <= endLine; line++ ) { |
Lines 87-111
define( [ 'marc-record', 'koha-backend', 'preferences', 'text-marc', 'widget' ],
Link Here
|
87 |
} |
92 |
} |
88 |
|
93 |
|
89 |
// Editor helper functions |
94 |
// Editor helper functions |
90 |
function activateTabPosition( cm, cur, idx ) { |
95 |
function activateTabPosition( cm, pos, idx ) { |
91 |
cm.setCursor( cur ); |
96 |
// Allow tabbing to as-yet-nonexistent positions |
92 |
Widget.ActivateAt( cm, cur, idx ); |
97 |
var lenDiff = pos.ch - cm.getLine( pos.line ).length; |
|
|
98 |
if ( lenDiff > 0 ) { |
99 |
var extra = ''; |
100 |
while ( lenDiff-- > 0 ) extra += ' '; |
101 |
if ( pos.prefill ) extra += pos.prefill; |
102 |
cm.replaceRange( extra, { line: pos.line } ); |
103 |
} |
104 |
|
105 |
cm.setCursor( pos ); |
106 |
Widget.ActivateAt( cm, pos, idx ); |
93 |
} |
107 |
} |
94 |
|
108 |
|
95 |
function getTabPositions( editor, cur ) { |
109 |
function getTabPositions( editor, cur ) { |
96 |
var info = editor.getLineInfo( cur || editor.cm.getCursor() ); |
110 |
cur = cur || editor.cm.getCursor(); |
|
|
111 |
var field = editor.getFieldAt( cur.line ); |
97 |
|
112 |
|
98 |
if ( info ) { |
113 |
if ( field ) { |
99 |
if ( info.subfields ) { |
114 |
if ( field.isControlField ) { |
100 |
var positions = [ 0, 4, 6 ]; |
115 |
var positions = [ { ch: 0 }, { ch: 4 } ]; |
101 |
|
116 |
|
102 |
$.each( info.subfields, function( undef, subfield ) { |
117 |
$.each( positions, function( undef, pos ) { |
103 |
positions.push( subfield.ch + 3 ); |
118 |
pos.line = cur.line; |
104 |
} ); |
119 |
} ); |
105 |
|
120 |
|
106 |
return positions; |
121 |
return positions; |
107 |
} else { |
122 |
} else { |
108 |
return [ 0, 4 ]; |
123 |
var positions = [ { ch: 0 }, { ch: 4, prefill: '_' }, { ch: 6, prefill: '_' } ]; |
|
|
124 |
|
125 |
$.each( positions, function( undef, pos ) { |
126 |
pos.line = cur.line; |
127 |
} ); |
128 |
$.each( field.getSubfields(), function( undef, subfield ) { |
129 |
positions.push( { line: cur.line, ch: subfield.contentsStart } ); |
130 |
} ); |
131 |
|
132 |
// Allow to tab to start of empty field |
133 |
if ( field.getSubfields().length == 0 ) { |
134 |
positions.push( { line: cur.line, ch: 8 } ); |
135 |
} |
136 |
|
137 |
return positions; |
109 |
} |
138 |
} |
110 |
} else { |
139 |
} else { |
111 |
return []; |
140 |
return []; |
Lines 119-152
define( [ 'marc-record', 'koha-backend', 'preferences', 'text-marc', 'widget' ],
Link Here
|
119 |
cm.setCursor( { line: cursor.line + 1, ch: 0 } ); |
148 |
cm.setCursor( { line: cursor.line + 1, ch: 0 } ); |
120 |
}, |
149 |
}, |
121 |
|
150 |
|
122 |
'Shift-Enter': function( cm ) { |
|
|
123 |
var cursor = cm.getCursor(); |
124 |
cm.replaceRange( '\n', { line: cursor.line, ch: 0 }, null, 'marcAware' ); |
125 |
cm.setCursor( { line: cursor.line, ch: 0 } ); |
126 |
}, |
127 |
|
128 |
'Ctrl-X': function( cm ) { |
151 |
'Ctrl-X': function( cm ) { |
129 |
// Delete subfield (or cut) |
152 |
// Delete line (or cut) |
130 |
if ( cm.somethingSelected() ) return true; |
153 |
if ( cm.somethingSelected() ) return true; |
131 |
|
154 |
|
132 |
var cur = cm.getCursor(); |
155 |
var field = cm.marceditor.getCurrentField(); |
133 |
var info = cm.marceditor.getLineInfo( cur ); |
156 |
if ( field ) field.delete(); |
134 |
if ( !info || !info.subfields ) return true; |
|
|
135 |
|
136 |
for (var i = 0; i < info.subfields.length; i++) { |
137 |
var end = i == info.subfields.length - 1 ? info.contents.length : info.subfields[i+1].ch; |
138 |
if (cur.ch > end) continue; |
139 |
|
140 |
cm.replaceRange( "", { line: cur.line, ch: info.subfields[i].ch }, { line: cur.line, ch: end }, 'marcAware' ); |
141 |
return; |
142 |
} |
143 |
}, |
157 |
}, |
144 |
|
158 |
|
145 |
'Shift-Ctrl-X': function( cm ) { |
159 |
'Shift-Ctrl-X': function( cm ) { |
146 |
// Delete line |
160 |
// Delete subfield |
147 |
var cur = cm.getCursor(); |
161 |
var field = cm.marceditor.getCurrentField(); |
|
|
162 |
if ( !field ) return; |
148 |
|
163 |
|
149 |
cm.replaceRange( "", { line: cur.line, ch: 0 }, { line: cur.line + 1, ch: 0 }, 'marcAware' ); |
164 |
var subfield = field.getSubfieldAt( cm.getCursor().ch ); |
|
|
165 |
if ( subfield ) subfield.delete(); |
150 |
}, |
166 |
}, |
151 |
|
167 |
|
152 |
Tab: function( cm ) { |
168 |
Tab: function( cm ) { |
Lines 155-162
define( [ 'marc-record', 'koha-backend', 'preferences', 'text-marc', 'widget' ],
Link Here
|
155 |
var cur = cm.getCursor(); |
171 |
var cur = cm.getCursor(); |
156 |
|
172 |
|
157 |
for ( var i = 0; i < positions.length; i++ ) { |
173 |
for ( var i = 0; i < positions.length; i++ ) { |
158 |
if ( positions[i] > cur.ch ) { |
174 |
if ( positions[i].ch > cur.ch ) { |
159 |
activateTabPosition( cm, { line: cur.line, ch: positions[i] } ); |
175 |
activateTabPosition( cm, positions[i] ); |
160 |
return false; |
176 |
return false; |
161 |
} |
177 |
} |
162 |
} |
178 |
} |
Lines 170-196
define( [ 'marc-record', 'koha-backend', 'preferences', 'text-marc', 'widget' ],
Link Here
|
170 |
var cur = cm.getCursor(); |
186 |
var cur = cm.getCursor(); |
171 |
|
187 |
|
172 |
for ( var i = positions.length - 1; i >= 0; i-- ) { |
188 |
for ( var i = positions.length - 1; i >= 0; i-- ) { |
173 |
if ( positions[i] < cur.ch ) { |
189 |
if ( positions[i].ch < cur.ch ) { |
174 |
activateTabPosition( cm, { line: cur.line, ch: positions[i] } ); |
190 |
activateTabPosition( cm, positions[i] ); |
175 |
return false; |
191 |
return false; |
176 |
} |
192 |
} |
177 |
} |
193 |
} |
178 |
|
194 |
|
179 |
if ( cur.line == 0 ) return; |
195 |
if ( cur.line == 0 ) return; |
180 |
|
196 |
|
181 |
var prevPositions = getTabPositions( cm, { line: cur.line - 1, ch: cm.getLine( cur.line - 1 ).length } ); |
197 |
var prevPositions = getTabPositions( cm.marceditor, { line: cur.line - 1, ch: cm.getLine( cur.line - 1 ).length } ); |
182 |
|
198 |
|
183 |
if ( prevPositions.length ) { |
199 |
if ( prevPositions.length ) { |
184 |
activateTabPosition( cm, { line: cur.line - 1, ch: prevPositions[ prevPositions.length - 1 ] }, -1 ); |
200 |
activateTabPosition( cm, prevPositions[ prevPositions.length - 1 ], -1 ); |
185 |
} else { |
201 |
} else { |
186 |
cm.setCursor( { line: cur.line - 1, ch: 0 } ); |
202 |
cm.setCursor( { line: cur.line - 1, ch: 0 } ); |
187 |
} |
203 |
} |
188 |
}, |
204 |
}, |
|
|
205 |
|
206 |
'Ctrl-D': function( cm ) { |
207 |
// Insert subfield delimiter |
208 |
// This will be extended later to allow either a configurable subfield delimiter or just |
209 |
// make it be the double cross. |
210 |
var cur = cm.getCursor(); |
211 |
|
212 |
cm.replaceRange( "$", cur, null ); |
213 |
}, |
214 |
}; |
215 |
|
216 |
// The objects below are part of a field/subfield manipulation API, accessed through the base |
217 |
// editor object. |
218 |
// |
219 |
// Each one is tied to a particular line; this means that using a field or subfield object after |
220 |
// any other changes to the record will cause entertaining explosions. The objects are meant to |
221 |
// be temporary, and should only be reused with great care. The macro code does this only |
222 |
// because it is careful to dispose of the object after any other updates. |
223 |
// |
224 |
// Note, however, tha you can continue to use a field object after changing subfields. It's just |
225 |
// the subfield objects that become invalid. |
226 |
|
227 |
// This is an exception raised by the EditorSubfield and EditorField when an invalid change is |
228 |
// attempted. |
229 |
function FieldError(line, message) { |
230 |
this.line = line; |
231 |
this.message = message; |
232 |
}; |
233 |
|
234 |
FieldError.prototype.toString = function() { |
235 |
return 'FieldError(' + this.line + ', "' + this.message + '")'; |
189 |
}; |
236 |
}; |
190 |
|
237 |
|
191 |
function MARCEditor( position ) { |
238 |
// This is the temporary object for a particular subfield in a field. Any change to any other |
|
|
239 |
// subfields will invalidate this subfield object. |
240 |
function EditorSubfield( field, index, start, end ) { |
241 |
this.field = field; |
242 |
this.index = index; |
243 |
this.start = start; |
244 |
this.end = end; |
245 |
|
246 |
if ( this.field.isControlField ) { |
247 |
this.contentsStart = start; |
248 |
this.code = '@'; |
249 |
} else { |
250 |
this.contentsStart = start + 3; |
251 |
this.code = this.field.contents.substr( this.start + 1, 1 ); |
252 |
} |
253 |
|
254 |
this.cm = field.cm; |
255 |
|
256 |
var marks = this.cm.findMarksAt( { line: field.line, ch: this.contentsStart } ); |
257 |
if ( marks[0] && marks[0].widget ) { |
258 |
this.widget = marks[0].widget; |
259 |
|
260 |
this.text = this.widget.text; |
261 |
this.setText = this.widget.setText; |
262 |
this.getFixed = this.widget.getFixed; |
263 |
this.setFixed = this.widget.setFixed; |
264 |
} else { |
265 |
this.widget = null; |
266 |
this.text = this.field.contents.substr( this.contentsStart, end - this.contentsStart ); |
267 |
} |
268 |
}; |
269 |
|
270 |
$.extend( EditorSubfield.prototype, { |
271 |
_invalid: function() { |
272 |
return this.field._subfieldsInvalid(); |
273 |
}, |
274 |
|
275 |
focus: function() { |
276 |
this.cm.setCursor( { line: this.field.line, ch: this.contentsStart } ); |
277 |
}, |
278 |
focusEnd: function() { |
279 |
this.cm.setCursor( { line: this.field.line, ch: this.end } ); |
280 |
}, |
281 |
getText: function() { |
282 |
return this.text; |
283 |
}, |
284 |
setText: function( text ) { |
285 |
if ( !this._invalid() ) throw new FieldError( this.field.line, 'subfield invalid' ); |
286 |
this.cm.replaceRange( text, { line: this.field.line, ch: this.contentsStart }, { line: this.field.line, ch: this.end }, 'marcAware' ); |
287 |
this.field._invalidateSubfields(); |
288 |
}, |
289 |
} ); |
290 |
|
291 |
function EditorField( editor, line ) { |
292 |
this.editor = editor; |
293 |
this.line = line; |
294 |
|
295 |
this.cm = editor.cm; |
296 |
|
297 |
this._updateInfo(); |
298 |
this.tag = this.contents.substr( 0, 3 ); |
299 |
this.isControlField = ( this.tag < '010' ); |
300 |
|
301 |
if ( this.isControlField ) { |
302 |
this._ind1 = this.contents.substr( 4, 1 ); |
303 |
this._ind2 = this.contents.substr( 6, 1 ); |
304 |
} else { |
305 |
this._ind1 = null; |
306 |
this._ind2 = null; |
307 |
} |
308 |
|
309 |
this.subfields = null; |
310 |
} |
311 |
|
312 |
$.extend( EditorField.prototype, { |
313 |
_subfieldsInvalid: function() { |
314 |
return !this.subfields; |
315 |
}, |
316 |
_invalidateSubfields: function() { |
317 |
this._subfields = null; |
318 |
}, |
319 |
|
320 |
_updateInfo: function() { |
321 |
this.info = this.editor.getLineInfo( { line: this.line, ch: 0 } ); |
322 |
if ( this.info == null ) throw new FieldError( 'Invalid field' ); |
323 |
this.contents = this.info.contents; |
324 |
}, |
325 |
_scanSubfields: function() { |
326 |
this._updateInfo(); |
327 |
|
328 |
if ( this.isControlField ) { |
329 |
this._subfields = [ new EditorSubfield( this, 0, 4, this.contents.length ) ]; |
330 |
} else { |
331 |
var field = this; |
332 |
var subfields = this.info.subfields; |
333 |
this._subfields = []; |
334 |
|
335 |
for (var i = 0; i < this.info.subfields.length; i++) { |
336 |
var end = i == subfields.length - 1 ? this.contents.length : subfields[i+1].ch; |
337 |
|
338 |
this._subfields.push( new EditorSubfield( this, i, subfields[i].ch, end ) ); |
339 |
} |
340 |
} |
341 |
}, |
342 |
|
343 |
delete: function() { |
344 |
this.cm.replaceRange( "", { line: this.line, ch: 0 }, { line: this.line + 1, ch: 0 }, 'marcAware' ); |
345 |
}, |
346 |
focus: function() { |
347 |
this.cm.setCursor( { line: this.line, ch: 0 } ); |
348 |
|
349 |
return this; |
350 |
}, |
351 |
|
352 |
getText: function() { |
353 |
var result = ''; |
354 |
|
355 |
$.each( this.getSubfields(), function() { |
356 |
if ( this.code != '@' ) result += '$' + this.code + ' '; |
357 |
|
358 |
result += this.getText(); |
359 |
} ); |
360 |
|
361 |
return result; |
362 |
}, |
363 |
setText: function( text ) { |
364 |
var indicator_match = /^([_ 0-9])([_ 0-9])\$/.exec( text ); |
365 |
if ( indicator_match ) { |
366 |
text = text.substr(2); |
367 |
this.setIndicator1( indicator_match[1] ); |
368 |
this.setIndicator2( indicator_match[2] ); |
369 |
} |
370 |
|
371 |
this.cm.replaceRange( text, { line: this.line, ch: this.isControlField ? 4 : 8 }, { line: this.line }, 'marcAware' ); |
372 |
this._invalidateSubfields(); |
373 |
|
374 |
return this; |
375 |
}, |
376 |
|
377 |
getIndicator1: function() { |
378 |
return this._ind1; |
379 |
}, |
380 |
getIndicator2: function() { |
381 |
return this._ind2; |
382 |
}, |
383 |
setIndicator1: function(val) { |
384 |
if ( this.isControlField ) throw new FieldError('Cannot set indicators on control field'); |
385 |
|
386 |
this._ind1 = ( !val || val == ' ' ) ? '_' : val; |
387 |
this.cm.replaceRange( this._ind1, { line: this.line, ch: 4 }, { line: this.line, ch: 5 }, 'marcAware' ); |
388 |
|
389 |
return this; |
390 |
}, |
391 |
setIndicator2: function(val) { |
392 |
if ( this.isControlField ) throw new FieldError('Cannot set indicators on control field'); |
393 |
|
394 |
this._ind2 = ( !val || val == ' ' ) ? '_' : val; |
395 |
this.cm.replaceRange( this._ind2, { line: this.line, ch: 6 }, { line: this.line, ch: 7 }, 'marcAware' ); |
396 |
|
397 |
return this; |
398 |
}, |
399 |
|
400 |
appendSubfield: function( code ) { |
401 |
if ( this.isControlField ) throw new FieldError('Cannot add subfields to control field'); |
402 |
|
403 |
this._invalidateSubfields(); |
404 |
this.cm.replaceRange( '$' + code + ' ', { line: this.line }, null, 'marcAware' ); |
405 |
var subfields = this.getSubfields(); |
406 |
|
407 |
return subfields[ subfields.length - 1 ]; |
408 |
}, |
409 |
insertSubfield: function( code, position ) { |
410 |
if ( this.isControlField ) throw new FieldError('Cannot add subfields to control field'); |
411 |
|
412 |
position = position || 0; |
413 |
|
414 |
var subfields = this.getSubfields(); |
415 |
this._invalidateSubfields(); |
416 |
this.cm.replaceRange( '$' + code + ' ', { line: this.line, ch: subfields[position] ? subfields[position].start : null }, null, 'marcAware' ); |
417 |
subfields = this.getSubfields(); |
418 |
|
419 |
return subfields[ position ]; |
420 |
}, |
421 |
getSubfields: function( code ) { |
422 |
if ( !this._subfields ) this._scanSubfields(); |
423 |
if ( code == null ) return this._subfields; |
424 |
|
425 |
var result = []; |
426 |
|
427 |
$.each( this._subfields, function() { |
428 |
if ( code == null || this.code == code ) result.push(this); |
429 |
} ); |
430 |
|
431 |
return result; |
432 |
}, |
433 |
getFirstSubfield: function( code ) { |
434 |
var result = this.getSubfields( code ); |
435 |
|
436 |
return ( result && result.length ) ? result[0] : null; |
437 |
}, |
438 |
getSubfieldAt: function( ch ) { |
439 |
var subfields = this.getSubfields(); |
440 |
|
441 |
for (var i = 0; i < subfields.length; i++) { |
442 |
if ( subfields[i].start < ch && subfields[i].end >= ch ) return subfields[i]; |
443 |
} |
444 |
}, |
445 |
} ); |
446 |
|
447 |
function MARCEditor( options ) { |
192 |
this.cm = CodeMirror( |
448 |
this.cm = CodeMirror( |
193 |
position, |
449 |
options.position, |
194 |
{ |
450 |
{ |
195 |
extraKeys: _editorKeys, |
451 |
extraKeys: _editorKeys, |
196 |
gutters: [ |
452 |
gutters: [ |
Lines 210-358
define( [ 'marc-record', 'koha-backend', 'preferences', 'text-marc', 'widget' ],
Link Here
|
210 |
this.cm.on( 'change', editorChange ); |
466 |
this.cm.on( 'change', editorChange ); |
211 |
this.cm.on( 'cursorActivity', editorCursorActivity ); |
467 |
this.cm.on( 'cursorActivity', editorCursorActivity ); |
212 |
|
468 |
|
|
|
469 |
this.onCursorActivity = options.onCursorActivity; |
470 |
|
213 |
this.subscribers = []; |
471 |
this.subscribers = []; |
214 |
this.subscribe( function( marceditor ) { |
472 |
this.subscribe( function( marceditor ) { |
215 |
Widget.Notify( marceditor ); |
473 |
Widget.Notify( marceditor ); |
216 |
} ); |
474 |
} ); |
217 |
} |
475 |
} |
218 |
|
476 |
|
219 |
MARCEditor.prototype.setUseWidgets = function( val ) { |
477 |
MARCEditor.FieldError = FieldError; |
220 |
if ( val ) { |
478 |
|
221 |
for ( var line = 0; line <= this.cm.lastLine(); line++ ) { |
479 |
$.extend( MARCEditor.prototype, { |
222 |
Widget.UpdateLine( this, line ); |
480 |
setUseWidgets: function( val ) { |
|
|
481 |
if ( val ) { |
482 |
for ( var line = 0; line <= this.cm.lastLine(); line++ ) { |
483 |
Widget.UpdateLine( this, line ); |
484 |
} |
485 |
} else { |
486 |
$.each( this.cm.getAllMarks(), function( undef, mark ) { |
487 |
if ( mark.widget ) mark.widget.clearToText(); |
488 |
} ); |
223 |
} |
489 |
} |
224 |
} else { |
490 |
}, |
|
|
491 |
|
492 |
focus: function() { |
493 |
this.cm.focus(); |
494 |
}, |
495 |
|
496 |
getCursor: function() { |
497 |
return this.cm.getCursor(); |
498 |
}, |
499 |
|
500 |
refresh: function() { |
501 |
this.cm.refresh(); |
502 |
}, |
503 |
|
504 |
displayRecord: function( record ) { |
505 |
this.cm.setValue( TextMARC.RecordToText(record) ); |
506 |
}, |
507 |
|
508 |
getRecord: function() { |
509 |
this.textMode = true; |
510 |
|
225 |
$.each( this.cm.getAllMarks(), function( undef, mark ) { |
511 |
$.each( this.cm.getAllMarks(), function( undef, mark ) { |
226 |
if ( mark.widget ) mark.widget.clearToText(); |
512 |
if ( mark.widget ) mark.widget.clearToText(); |
227 |
} ); |
513 |
} ); |
228 |
} |
514 |
var record = TextMARC.TextToRecord( this.cm.getValue() ); |
229 |
}; |
515 |
for ( var line = 0; line <= this.cm.lastLine(); line++ ) { |
|
|
516 |
if ( Preferences.user.fieldWidgets ) Widget.UpdateLine( this, line ); |
517 |
} |
230 |
|
518 |
|
231 |
MARCEditor.prototype.focus = function() { |
519 |
this.textMode = false; |
232 |
this.cm.focus(); |
|
|
233 |
}; |
234 |
|
520 |
|
235 |
MARCEditor.prototype.refresh = function() { |
521 |
return record; |
236 |
this.cm.refresh(); |
522 |
}, |
237 |
}; |
|
|
238 |
|
523 |
|
239 |
MARCEditor.prototype.displayRecord = function( record ) { |
524 |
getLineInfo: function( pos ) { |
240 |
this.cm.setValue( TextMARC.RecordToText(record) ); |
525 |
var contents = this.cm.getLine( pos.line ); |
241 |
}; |
526 |
if ( contents == null ) return {}; |
242 |
|
527 |
|
243 |
MARCEditor.prototype.getRecord = function() { |
528 |
var tagNumber = contents.match( /^([A-Za-z0-9]{3})/ ); |
244 |
this.textMode = true; |
|
|
245 |
|
529 |
|
246 |
$.each( this.cm.getAllMarks(), function( undef, mark ) { |
530 |
if ( !tagNumber ) return null; // No tag at all on this line |
247 |
if ( mark.widget ) mark.widget.clearToText(); |
531 |
tagNumber = tagNumber[1]; |
248 |
} ); |
|
|
249 |
var record = TextMARC.TextToRecord( this.cm.getValue() ); |
250 |
for ( var line = 0; line <= this.cm.lastLine(); line++ ) { |
251 |
if ( Preferences.user.fieldWidgets ) Widget.UpdateLine( this, line ); |
252 |
} |
253 |
|
532 |
|
254 |
this.textMode = false; |
533 |
if ( tagNumber < '010' ) return { tagNumber: tagNumber, contents: contents }; // No current subfield |
255 |
|
534 |
|
256 |
return record; |
535 |
var matcher = /[$|ǂ‡]([a-z0-9%]) /g; |
257 |
}; |
536 |
var match; |
258 |
|
537 |
|
259 |
MARCEditor.prototype.getLineInfo = function( pos ) { |
538 |
var subfields = []; |
260 |
var contents = this.cm.getLine( pos.line ); |
539 |
var currentSubfield; |
261 |
if ( contents == null ) return {}; |
|
|
262 |
|
540 |
|
263 |
var tagNumber = contents.match( /^([A-Za-z0-9]{3}) / ); |
541 |
while ( ( match = matcher.exec(contents) ) ) { |
|
|
542 |
subfields.push( { code: match[1], ch: match.index } ); |
543 |
if ( match.index < pos.ch ) currentSubfield = match[1]; |
544 |
} |
264 |
|
545 |
|
265 |
if ( !tagNumber ) return null; // No tag at all on this line |
546 |
return { tagNumber: tagNumber, subfields: subfields, currentSubfield: currentSubfield, contents: contents }; |
266 |
tagNumber = tagNumber[1]; |
547 |
}, |
267 |
|
548 |
|
268 |
if ( tagNumber < '010' ) return { tagNumber: tagNumber, contents: contents }; // No current subfield |
549 |
addError: function( line, error ) { |
|
|
550 |
var found = false; |
551 |
var options = {}; |
269 |
|
552 |
|
270 |
var matcher = /[$|ǂ‡]([a-z0-9%]) /g; |
553 |
if ( line == null ) { |
271 |
var match; |
554 |
line = 0; |
|
|
555 |
options.above = true; |
556 |
} |
272 |
|
557 |
|
273 |
var subfields = []; |
558 |
$.each( this.cm.getLineHandle(line).widgets || [], function( undef, widget ) { |
274 |
var currentSubfield; |
559 |
if ( !widget.isErrorMarker ) return; |
275 |
|
560 |
|
276 |
while ( ( match = matcher.exec(contents) ) ) { |
561 |
found = true; |
277 |
subfields.push( { code: match[1], ch: match.index } ); |
|
|
278 |
if ( match.index < pos.ch ) currentSubfield = match[1]; |
279 |
} |
280 |
|
562 |
|
281 |
return { tagNumber: tagNumber, subfields: subfields, currentSubfield: currentSubfield, contents: contents }; |
563 |
$( widget.node ).append( '; ' + error ); |
282 |
}; |
564 |
widget.changed(); |
283 |
|
|
|
284 |
MARCEditor.prototype.addError = function( line, error ) { |
285 |
var found = false; |
286 |
var options = {}; |
287 |
|
565 |
|
288 |
if ( line == null ) { |
566 |
return false; |
289 |
line = 0; |
567 |
} ); |
290 |
options.above = true; |
|
|
291 |
} |
292 |
|
568 |
|
293 |
$.each( this.cm.getLineHandle(line).widgets || [], function( undef, widget ) { |
569 |
if ( found ) return; |
294 |
if ( !widget.isErrorMarker ) return; |
|
|
295 |
|
570 |
|
296 |
found = true; |
571 |
var node = $( '<div class="structure-error"><i class="icon-remove"></i> ' + error + '</div>' )[0]; |
|
|
572 |
var widget = this.cm.addLineWidget( line, node, options ); |
297 |
|
573 |
|
298 |
$( widget.node ).append( '; ' + error ); |
574 |
widget.node = node; |
299 |
widget.changed(); |
575 |
widget.isErrorMarker = true; |
|
|
576 |
}, |
300 |
|
577 |
|
301 |
return false; |
578 |
removeErrors: function() { |
302 |
} ); |
579 |
for ( var line = 0; line < this.cm.lineCount(); line++ ) { |
|
|
580 |
$.each( this.cm.getLineHandle( line ).widgets || [], function( undef, lineWidget ) { |
581 |
if ( lineWidget.isErrorMarker ) lineWidget.clear(); |
582 |
} ); |
583 |
} |
584 |
}, |
303 |
|
585 |
|
304 |
if ( found ) return; |
586 |
startNotify: function() { |
|
|
587 |
if ( this.notifyTimeout ) clearTimeout( this.notifyTimeout ); |
588 |
this.notifyTimeout = setTimeout( $.proxy( function() { |
589 |
this.notifyAll(); |
305 |
|
590 |
|
306 |
var node = $( '<div class="structure-error"><i class="icon-remove"></i> ' + error + '</div>' )[0]; |
591 |
this.notifyTimeout = null; |
307 |
var widget = this.cm.addLineWidget( line, node, options ); |
592 |
}, this ), NOTIFY_TIMEOUT ); |
|
|
593 |
}, |
308 |
|
594 |
|
309 |
widget.node = node; |
595 |
notifyAll: function() { |
310 |
widget.isErrorMarker = true; |
596 |
$.each( this.subscribers, $.proxy( function( undef, subscriber ) { |
311 |
}; |
597 |
subscriber(this); |
|
|
598 |
}, this ) ); |
599 |
}, |
312 |
|
600 |
|
313 |
MARCEditor.prototype.removeErrors = function() { |
601 |
subscribe: function( subscriber ) { |
314 |
for ( var line = 0; line < this.cm.lineCount(); line++ ) { |
602 |
this.subscribers.push( subscriber ); |
315 |
$.each( this.cm.getLineHandle( line ).widgets || [], function( undef, lineWidget ) { |
603 |
}, |
316 |
if ( lineWidget.isErrorMarker ) lineWidget.clear(); |
|
|
317 |
} ); |
318 |
} |
319 |
}; |
320 |
|
604 |
|
321 |
MARCEditor.prototype.getFixedField = function(field) { |
605 |
createField: function( tag, line ) { |
322 |
field += ' '; |
606 |
var contents = tag + ( tag < '010' ? ' ' : ' _ _ ' ); |
323 |
for ( var line = 0; line < this.cm.lineCount(); line++ ) { |
|
|
324 |
var contents = this.cm.getLine(line); |
325 |
if ( contents.substr( 0, 4 ) != field ) continue; |
326 |
|
607 |
|
327 |
var marks = this.cm.findMarksAt( { line: line, ch: 4 } ); |
608 |
if ( line > this.cm.lastLine() ) { |
328 |
if ( marks[0] && marks[0].widget ) { |
609 |
contents = '\n' + contents; |
329 |
return marks[0].widget.text; |
|
|
330 |
} else { |
610 |
} else { |
331 |
return contents.substr(4); |
611 |
contents = contents + '\n'; |
332 |
} |
612 |
} |
333 |
} |
|
|
334 |
|
613 |
|
335 |
return null; |
614 |
this.cm.replaceRange( contents, { line: line, ch: 0 }, null, 'marcAware' ); |
336 |
}; |
|
|
337 |
|
615 |
|
338 |
MARCEditor.prototype.startNotify = function() { |
616 |
return new EditorField( this, line ); |
339 |
if ( this.notifyTimeout ) clearTimeout( this.notifyTimeout ); |
617 |
}, |
340 |
this.notifyTimeout = setTimeout( $.proxy( function() { |
|
|
341 |
this.notifyAll(); |
342 |
|
618 |
|
343 |
this.notifyTimeout = null; |
619 |
createFieldOrdered: function( tag ) { |
344 |
}, this ), NOTIFY_TIMEOUT ); |
620 |
var line, contents; |
345 |
}; |
|
|
346 |
|
621 |
|
347 |
MARCEditor.prototype.notifyAll = function() { |
622 |
for ( line = 0; (contents = this.cm.getLine(line)); line++ ) { |
348 |
$.each( this.subscribers, $.proxy( function( undef, subscriber ) { |
623 |
if ( contents && contents.substr(0, 3) > tag ) break; |
349 |
subscriber(this); |
624 |
} |
350 |
}, this ) ); |
|
|
351 |
}; |
352 |
|
625 |
|
353 |
MARCEditor.prototype.subscribe = function( subscriber ) { |
626 |
return this.createField( tag, line ); |
354 |
this.subscribers.push( subscriber ); |
627 |
}, |
355 |
}; |
628 |
|
|
|
629 |
createFieldGrouped: function( tag ) { |
630 |
// Control fields should be inserted in actual order, whereas other fields should be |
631 |
// inserted grouped |
632 |
if ( tag < '010' ) return this.createFieldOrdered( tag ); |
633 |
|
634 |
var line, contents; |
635 |
|
636 |
for ( line = 0; (contents = this.cm.getLine(line)); line++ ) { |
637 |
if ( contents && contents[0] > tag[0] ) break; |
638 |
} |
639 |
|
640 |
return this.createField( tag, line ); |
641 |
}, |
642 |
|
643 |
getFieldAt: function( line ) { |
644 |
try { |
645 |
return new EditorField( this, line ); |
646 |
} catch (e) { |
647 |
return null; |
648 |
} |
649 |
}, |
650 |
|
651 |
getCurrentField: function() { |
652 |
return this.getFieldAt( this.cm.getCursor().line ); |
653 |
}, |
654 |
|
655 |
getFields: function( tag ) { |
656 |
var result = []; |
657 |
|
658 |
if ( tag != null ) tag += ' '; |
659 |
|
660 |
for ( var line = 0; line < this.cm.lineCount(); line++ ) { |
661 |
if ( tag && this.cm.getLine(line).substr( 0, 4 ) != tag ) continue; |
662 |
|
663 |
// If this throws a FieldError, pretend it doesn't exist |
664 |
try { |
665 |
result.push( new EditorField( this, line ) ); |
666 |
} catch (e) { |
667 |
if ( !( e instanceof FieldError ) ) throw e; |
668 |
} |
669 |
} |
670 |
|
671 |
return result; |
672 |
}, |
673 |
|
674 |
getFirstField: function( tag ) { |
675 |
var result = this.getFields( tag ); |
676 |
|
677 |
return ( result && result.length ) ? result[0] : null; |
678 |
}, |
679 |
|
680 |
getAllFields: function( tag ) { |
681 |
return this.getFields( null ); |
682 |
}, |
683 |
} ); |
356 |
|
684 |
|
357 |
return MARCEditor; |
685 |
return MARCEditor; |
358 |
} ); |
686 |
} ); |