|
Lines 1-5
Link Here
|
| 1 |
define( function() { |
1 |
define( [ 'resources' ], function( Resources ) { |
|
|
2 |
var _widgets = {}; |
| 3 |
|
| 2 |
var Widget = { |
4 |
var Widget = { |
|
|
5 |
Register: function( tagfield, widget ) { |
| 6 |
_widgets[tagfield] = widget; |
| 7 |
}, |
| 8 |
|
| 9 |
PadNum: function( number, length ) { |
| 10 |
var result = number.toString(); |
| 11 |
|
| 12 |
while ( result.length < length ) result = '0' + result; |
| 13 |
|
| 14 |
return result; |
| 15 |
}, |
| 16 |
|
| 17 |
PadString: function( result, length ) { |
| 18 |
while ( result.length < length ) result = ' ' + result; |
| 19 |
|
| 20 |
return result; |
| 21 |
}, |
| 22 |
|
| 23 |
PadStringRight: function( result, length ) { |
| 24 |
result = '' + result; |
| 25 |
while ( result.length < length ) result += ' '; |
| 26 |
|
| 27 |
return result; |
| 28 |
}, |
| 29 |
|
| 3 |
Base: { |
30 |
Base: { |
| 4 |
// Marker utils |
31 |
// Marker utils |
| 5 |
clearToText: function() { |
32 |
clearToText: function() { |
|
Lines 17-25
define( function() {
Link Here
|
| 17 |
var $node = $( this.node ).find( sel ); |
44 |
var $node = $( this.node ).find( sel ); |
| 18 |
$node.val( this.getFixed( start, end ) ); |
45 |
$node.val( this.getFixed( start, end ) ); |
| 19 |
|
46 |
|
| 20 |
$node.change( $.proxy( function() { |
47 |
var widget = this; |
| 21 |
this.setFixed( start, end, $node.val(), '+input' ); |
48 |
var $collapsed = $( '<span class="fixed-collapsed" title="' + $node.attr('title') + '">' + $node.val() + '</span>' ).insertAfter( $node ); |
| 22 |
}, this ) ); |
49 |
|
|
|
50 |
function show() { |
| 51 |
$collapsed.hide(); |
| 52 |
$node.val( widget.getFixed( start, end ).replace(/\s+$/, '') ); |
| 53 |
$node.show(); |
| 54 |
$node[0].focus(); |
| 55 |
} |
| 56 |
|
| 57 |
function hide() { |
| 58 |
$node.hide(); |
| 59 |
$collapsed.text( Widget.PadStringRight( $node.val(), end - start ) ).show(); |
| 60 |
} |
| 61 |
|
| 62 |
$node.on( 'change keyup', function() { |
| 63 |
widget.setFixed( start, end, $node.val(), '+input' ); |
| 64 |
} ).focus( show ).blur( hide ); |
| 65 |
|
| 66 |
hide(); |
| 67 |
|
| 68 |
$collapsed.click( show ); |
| 23 |
}, |
69 |
}, |
| 24 |
|
70 |
|
| 25 |
getFixed: function( start, end ) { |
71 |
getFixed: function( start, end ) { |
|
Lines 27-33
define( function() {
Link Here
|
| 27 |
}, |
73 |
}, |
| 28 |
|
74 |
|
| 29 |
setFixed: function( start, end, value, source ) { |
75 |
setFixed: function( start, end, value, source ) { |
| 30 |
this.setText( this.text.substring( 0, start ) + this.padString( value.toString().substr( 0, end - start ), end - start ) + this.text.substring( end ), source ); |
76 |
this.setText( this.text.substring( 0, start ) + Widget.PadStringRight( value.toString().substr( 0, end - start ), end - start ) + this.text.substring( end ), source ); |
| 31 |
}, |
77 |
}, |
| 32 |
|
78 |
|
| 33 |
setText: function( text, source ) { |
79 |
setText: function( text, source ) { |
|
Lines 36-41
define( function() {
Link Here
|
| 36 |
this.editor.startNotify(); |
82 |
this.editor.startNotify(); |
| 37 |
}, |
83 |
}, |
| 38 |
|
84 |
|
|
|
85 |
createFromXML: function( resourceId ) { |
| 86 |
var widget = this; |
| 87 |
|
| 88 |
Resources[resourceId].done( function( xml ) { |
| 89 |
$(widget.node).find('.widget-loading').remove(); |
| 90 |
var $matSelect = $('<select class="material-select"></select>').appendTo(widget.node); |
| 91 |
var $contents = $('<span class="material-contents"/>').appendTo(widget.node); |
| 92 |
var materialInfo = {}; |
| 93 |
|
| 94 |
$('Tagfield', xml).children('Material').each( function() { |
| 95 |
$matSelect.append( '<option value="' + $(this).attr('id') + '">' + $(this).attr('id') + ' - ' + $(this).children('name').text() + '</option>' ); |
| 96 |
|
| 97 |
materialInfo[ $(this).attr('id') ] = this; |
| 98 |
} ); |
| 99 |
|
| 100 |
$matSelect.change( function() { |
| 101 |
widget.loadXMLMaterial( materialInfo[ $matSelect.val() ] ); |
| 102 |
} ).change(); |
| 103 |
} ); |
| 104 |
}, |
| 105 |
|
| 106 |
loadXMLMaterial: function( materialInfo ) { |
| 107 |
var $contents = $(this.node).children('.material-contents'); |
| 108 |
$contents.empty(); |
| 109 |
|
| 110 |
var widget = this; |
| 111 |
|
| 112 |
$(materialInfo).children('Position').each( function() { |
| 113 |
var match = $(this).attr('pos').match(/(\d+)(?:-(\d+))?/); |
| 114 |
if (!match) return; |
| 115 |
|
| 116 |
var start = parseInt(match[1]); |
| 117 |
var end = ( match[2] ? parseInt(match[2]) : start ) + 1; |
| 118 |
var $input; |
| 119 |
var $values = $(this).children('Value'); |
| 120 |
|
| 121 |
if ($values.length == 0) { |
| 122 |
$contents.append( '<span title="' + $(this).children('name').text() + '">' + widget.getFixed(start, end) + '</span>' ); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
if ( match[2] ) { |
| 127 |
$input = $( '<input name="f' + Widget.PadNum(start, 2) + '" title="' + $(this).children('name').text() + '" maxlength="' + (end - start) + '" />' ); |
| 128 |
} else { |
| 129 |
$input = $( '<select name="f' + Widget.PadNum(start, 2) + '" title="' + $(this).children('name').text() + '"></select>' ); |
| 130 |
|
| 131 |
$values.each( function() { |
| 132 |
$input.append( '<option value="' + $(this).attr('code') + '">' + $(this).attr('code') + ' - ' + $(this).children('description').text() + '</option>' ); |
| 133 |
} ); |
| 134 |
} |
| 135 |
|
| 136 |
$contents.append( $input ); |
| 137 |
widget.bindFixed( $input, start, end ); |
| 138 |
} ); |
| 139 |
}, |
| 140 |
|
| 141 |
nodeChanged: function() { |
| 142 |
this.mark.changed(); |
| 143 |
var widget = this; |
| 144 |
|
| 145 |
var $inputs = $(this.node).find('input, select'); |
| 146 |
if ( !$inputs.length ) return; |
| 147 |
|
| 148 |
$inputs.off('keydown.marc-tab'); |
| 149 |
var editor = widget.editor; |
| 150 |
|
| 151 |
$inputs.each( function( i ) { |
| 152 |
$(this).on( 'keydown.marc-tab', function( e ) { |
| 153 |
if ( e.which != 9 ) return; // Tab |
| 154 |
|
| 155 |
var span = widget.mark.find(); |
| 156 |
var cur = editor.cm.getCursor(); |
| 157 |
|
| 158 |
if ( e.shiftKey ) { |
| 159 |
if ( i > 0 ) { |
| 160 |
$inputs.eq(i - 1).trigger( 'focus' ); |
| 161 |
} else { |
| 162 |
editor.cm.setCursor( span.from ); |
| 163 |
// FIXME: ugly hack |
| 164 |
editor.cm.options.extraKeys['Shift-Tab']( editor.cm ); |
| 165 |
editor.focus(); |
| 166 |
} |
| 167 |
} else { |
| 168 |
if ( i < $inputs.length - 1 ) { |
| 169 |
$inputs.eq(i + 1).trigger( 'focus' ); |
| 170 |
} else { |
| 171 |
editor.cm.setCursor( span.to ); |
| 172 |
editor.focus(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return false; |
| 177 |
} ); |
| 178 |
} ); |
| 179 |
}, |
| 180 |
|
| 39 |
// Template utils |
181 |
// Template utils |
| 40 |
insertTemplate: function( sel ) { |
182 |
insertTemplate: function( sel ) { |
| 41 |
var wsOnly = /^\s*$/; |
183 |
var wsOnly = /^\s*$/; |
|
Lines 45-64
define( function() {
Link Here
|
| 45 |
} |
187 |
} |
| 46 |
} ).appendTo( this.node ); |
188 |
} ).appendTo( this.node ); |
| 47 |
}, |
189 |
}, |
| 48 |
|
|
|
| 49 |
padNum: function( number, length ) { |
| 50 |
var result = number.toString(); |
| 51 |
|
| 52 |
while ( result.length < length ) result = '0' + result; |
| 53 |
|
| 54 |
return result; |
| 55 |
}, |
| 56 |
|
| 57 |
padString: function( result, length ) { |
| 58 |
while ( result.length < length ) result = ' ' + result; |
| 59 |
|
| 60 |
return result; |
| 61 |
} |
| 62 |
}, |
190 |
}, |
| 63 |
|
191 |
|
| 64 |
ActivateAt: function( editor, cur, idx ) { |
192 |
ActivateAt: function( editor, cur, idx ) { |
|
Lines 119-126
define( function() {
Link Here
|
| 119 |
} |
247 |
} |
| 120 |
} |
248 |
} |
| 121 |
|
249 |
|
| 122 |
if ( !editorWidgets[id] ) return; |
250 |
if ( !_widgets[id] ) return; |
| 123 |
var fullBase = $.extend( Object.create( Widget.Base ), editorWidgets[id] ); |
251 |
var fullBase = $.extend( Object.create( Widget.Base ), _widgets[id] ); |
| 124 |
var widget = Object.create( fullBase ); |
252 |
var widget = Object.create( fullBase ); |
| 125 |
|
253 |
|
| 126 |
if ( subfield.from == subfield.to ) { |
254 |
if ( subfield.from == subfield.to ) { |
|
Lines 148-167
define( function() {
Link Here
|
| 148 |
|
276 |
|
| 149 |
if ( widget.postCreate ) { |
277 |
if ( widget.postCreate ) { |
| 150 |
widget.postCreate(); |
278 |
widget.postCreate(); |
| 151 |
mark.changed(); |
|
|
| 152 |
} |
279 |
} |
| 153 |
|
280 |
|
| 154 |
var $lastInput = $(widget.node).find('input, select').eq(-1); |
281 |
widget.nodeChanged(); |
| 155 |
if ( $lastInput.length ) { |
|
|
| 156 |
$lastInput.bind( 'keypress', 'tab', function() { |
| 157 |
var cur = editor.cm.getCursor(); |
| 158 |
editor.cm.setCursor( { line: cur.line } ); |
| 159 |
// FIXME: ugly hack |
| 160 |
editor.cm.options.extraKeys.Tab( editor.cm ); |
| 161 |
editor.focus(); |
| 162 |
return false; |
| 163 |
} ); |
| 164 |
} |
| 165 |
} ); |
282 |
} ); |
| 166 |
}, |
283 |
}, |
| 167 |
}; |
284 |
}; |