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>.
21 var NAV_FAILED = new Object();
22 var NAV_SUCCEEDED = new Object();
24 var _commandGenerators = [
25 [ /^copy field data$/i, function() {
26 return function( editor, state ) {
27 if ( state.field == null ) return;
29 return state.field.getText();
32 [ /^copy subfield data$/i, function() {
33 return function( editor, state ) {
34 if ( state.field == null ) return;
36 var cur = editor.getCursor();
37 var subfields = state.field.getSubfields();
39 for (var i = 0; i < subfields.length; i++) {
40 if ( cur.ch > subfields[i].end ) continue;
42 state.clipboard = subfields[i].text;
49 [ /^del(ete)? field$/i, function() {
50 return function( editor, state ) {
51 if ( state.field == null ) return;
57 [ /^goto field end$/i, function() {
58 return function( editor, state ) {
59 if ( state.field == null ) return NAV_FAILED;
60 var cur = editor.cm.getCursor();
62 editor.cm.setCursor( { line: cur.line } );
66 [ /^goto field (\w{3})$/i, function(tag) {
67 return function( editor, state ) {
68 var field = editor.getFirstField(tag);
69 if ( field == null ) return NAV_FAILED;
75 [ /^goto subfield end$/i, function() {
76 return function( editor, state ) {
77 if ( state.field == null ) return NAV_FAILED;
79 var cur = editor.getCursor();
80 var subfields = state.field.getSubfields();
82 for (var i = 0; i < subfields.length; i++) {
83 if ( cur.ch > subfields[i].end ) continue;
92 [ /^goto subfield (\w)$/i, function( code ) {
93 return function( editor, state ) {
94 if ( state.field == null ) return NAV_FAILED;
96 var subfield = state.field.getFirstSubfield( code );
97 if ( subfield == null ) return NAV_FAILED;
100 return NAV_SUCCEEDED;
103 [ /^insert (new )?field (\w{3}) data=(.*)/i, function(undef, tag, text) {
104 text = text.replace(/\\([0-9a-z])/g, '$$$1 ');
105 return function( editor, state ) {
106 editor.createFieldGrouped(tag).setText(text).focus();
107 return NAV_SUCCEEDED;
110 [ /^insert (new )?subfield (\w) data=(.*)/i, function(undef, code, text) {
111 return function( editor, state ) {
112 if ( state.field == null ) return;
114 state.field.appendSubfield(code).setText(text);
117 [ /^paste$/i, function() {
118 return function( editor, state ) {
119 var cur = editor.cm.getCursor();
121 editor.cm.replaceRange( state.clipboard, cur, null, 'marcAware' );
124 [ /^set indicator([12])=([ _0-9])$/i, function( ind, value ) {
125 return function( editor, state ) {
126 if ( state.field == null ) return;
127 if ( state.field.isControlField ) return false;
130 state.field.setIndicator1(value);
132 } else if ( ind == '2' ) {
133 state.field.setIndicator2(value);
140 [ /^set indicators=([ _0-9])([ _0-9])?$/i, function( ind1, ind2 ) {
141 return function( editor, state ) {
142 if ( state.field == null ) return;
143 if ( state.field.isControlField ) return false;
145 state.field.setIndicator1(ind1);
146 state.field.setIndicator2(ind2 || '_');
152 Compile: function( macro ) {
153 var result = { commands: [], errors: [] };
155 $.each( macro.split(/\r\n|\n/), function( line, contents ) {
158 if ( contents.match(/^\s*$/) ) return;
160 $.each( _commandGenerators, function( undef, gen ) {
163 if ( !( match = gen[0].exec( contents ) ) ) return;
165 command = gen[1].apply(null, match.slice(1));
170 result.errors.push( { line: line, error: 'unrecognized' } );
173 result.commands.push( { func: command, orig: contents, line: line } );
178 Run: function( editor, macro ) {
179 var compiled = ITSMacro.Compile(macro);
180 if ( compiled.errors.length ) return { errors: compiled.errors };
186 var run_result = { errors: [] };
188 editor.cm.operation( function() {
189 $.each( compiled.commands, function( undef, command ) {
190 var result = command.func( editor, state );
192 if ( result == NAV_FAILED ) {
194 } else if ( result == NAV_SUCCEEDED ) {
195 state.field = editor.getCurrentField();
196 } else if ( result === false ) {
197 run_result.errors.push( { line: command.line, error: 'failed' } );