-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetawidget-jqueryui.js
432 lines (309 loc) · 11.7 KB
/
metawidget-jqueryui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Metawidget 4.2
//
// This file is dual licensed under both the LGPL
// (http://www.gnu.org/licenses/lgpl-2.1.html) and the EPL
// (http://www.eclipse.org/org/documents/epl-v10.php). As a
// recipient of Metawidget, you may choose to receive it under either
// the LGPL or the EPL.
//
// Commercial licenses are also available. See http://metawidget.org
// for details.
var metawidget = metawidget || {};
( function() {
'use strict';
/**
* @namespace Metawidget for JQuery UI environments.
*/
metawidget.jqueryui = metawidget.jqueryui || {};
/**
* @namespace JQuery UI WidgetBuilders.
*/
metawidget.jqueryui.widgetbuilder = metawidget.jqueryui.widgetbuilder || {};
/**
* @class Builds widgets using JQuery UI.
* <p>
* Chooses JQuery UI widgets such as <tt>slider</tt> and
* <tt>spinner</tt> to suit the inspected fields. Returns undefined
* for everything else.
*/
metawidget.jqueryui.widgetbuilder.JQueryUIWidgetBuilder = function() {
if ( ! ( this instanceof metawidget.jqueryui.widgetbuilder.JQueryUIWidgetBuilder ) ) {
throw new Error( "Constructor called as a function" );
}
};
metawidget.jqueryui.widgetbuilder.JQueryUIWidgetBuilder.prototype.buildWidget = function( elementName, attributes, mw ) {
// Not for us?
if ( metawidget.util.isTrueOrTrueString( attributes.readOnly ) ) {
return;
}
if ( metawidget.util.isTrueOrTrueString( attributes.hidden ) ) {
return;
}
// Number
if ( attributes.type === 'number' || attributes.type === 'integer' ) {
if ( attributes.minimum && attributes.maximum ) {
var slider = metawidget.util.createElement( mw, 'div' );
$( slider ).slider();
return slider;
}
var spinner = metawidget.util.createElement( mw, 'input' );
$( spinner ).spinner();
return $( spinner ).spinner( 'widget' )[0];
}
// Datepicker
if ( attributes.type === 'date' ) {
var date = metawidget.util.createElement( mw, 'input' );
$( date ).datepicker();
return date;
}
};
/**
* @namespace JQuery UI WidgetProcessors.
*/
metawidget.jqueryui.widgetprocessor = metawidget.jqueryui.widgetprocessor || {};
/**
* @class Binds JQuery UI specific widgets, using the JQuery
* <tt>$( widget ).foo( 'value', value )</tt> syntax. Clients
* should still use SimpleBindingProcessor for all non-JQuery UI
* widgets.
*/
metawidget.jqueryui.widgetprocessor.JQueryUIBindingProcessor = function() {
if ( ! ( this instanceof metawidget.jqueryui.widgetprocessor.JQueryUIBindingProcessor ) ) {
throw new Error( "Constructor called as a function" );
}
};
metawidget.jqueryui.widgetprocessor.JQueryUIBindingProcessor.prototype.onStartBuild = function( mw ) {
mw._jQueryUIBindingProcessorBindings = {};
};
metawidget.jqueryui.widgetprocessor.JQueryUIBindingProcessor.prototype.processWidget = function( widget, elementName, attributes, mw ) {
var value;
var typeAndNames = metawidget.util.splitPath( mw.path );
var toInspect = metawidget.util.traversePath( mw.toInspect, typeAndNames.names );
if ( elementName !== 'entity' && toInspect ) {
value = toInspect[attributes.name];
} else {
value = toInspect;
}
var isBindable = false;
if ( widget.hasAttribute( 'class' ) ) {
var styleClass = widget.getAttribute( 'class' );
if ( styleClass.indexOf( 'ui-slider' ) !== -1 ) {
$( widget ).slider( 'value', value );
isBindable = true;
} else if ( styleClass.indexOf( 'ui-spinner' ) !== -1 ) {
$( widget.childNodes[0] ).spinner( 'value', value );
isBindable = true;
}
}
if ( isBindable === true || widget.getMetawidget !== undefined ) {
mw._jQueryUIBindingProcessorBindings[attributes.name] = widget;
}
return widget;
};
/**
* Save the bindings associated with the given Metawidget.
*/
metawidget.jqueryui.widgetprocessor.JQueryUIBindingProcessor.prototype.save = function( mw ) {
var typeAndNames = metawidget.util.splitPath( mw.path );
var toInspect = metawidget.util.traversePath( mw.toInspect, typeAndNames.names );
for ( var name in mw._jQueryUIBindingProcessorBindings ) {
var widget = mw._jQueryUIBindingProcessorBindings[name];
if ( widget.getMetawidget !== undefined ) {
this.save( widget.getMetawidget() );
continue;
}
widget = mw.getElement().ownerDocument.getElementById( widget.id );
var styleClass = widget.getAttribute( 'class' );
if ( styleClass.indexOf( 'ui-slider' ) !== -1 ) {
toInspect[name] = $( widget ).slider( 'value' );
} else if ( styleClass.indexOf( 'ui-spinner' ) !== -1 ) {
toInspect[name] = $( widget.childNodes[0] ).spinner( 'value' );
}
}
};
metawidget.jqueryui.layout = metawidget.jqueryui.layout || {};
/**
* @class LayoutDecorator to decorate widgets from different sections using
* JQuery UI tabs.
*/
metawidget.jqueryui.layout.TabLayoutDecorator = function( config ) {
if ( ! ( this instanceof metawidget.jqueryui.layout.TabLayoutDecorator ) ) {
throw new Error( "Constructor called as a function" );
}
metawidget.layout.createNestedSectionLayoutDecorator( config, this, 'tabLayoutDecorator' );
var _superOnEndBuild = this.onEndBuild;
/**
* Wrap the tabs at the very end, to save using 'tabs.add'.
*/
this.onEndBuild = function( mw ) {
if ( mw.tabLayoutDecorator !== undefined ) {
for ( var loop = 0, length = mw.tabLayoutDecorator.length; loop < length; loop++ ) {
$( mw.tabLayoutDecorator[loop] ).tabs();
}
}
if ( _superOnEndBuild !== undefined ) {
_superOnEndBuild.call( this, mw );
}
};
};
metawidget.jqueryui.layout.TabLayoutDecorator.prototype.createSectionWidget = function( previousSectionWidget, section, attributes, container, mw ) {
var tabs = previousSectionWidget;
// Whole new tabbed pane?
if ( tabs === undefined ) {
tabs = metawidget.util.createElement( mw, 'div' );
tabs.setAttribute( 'id', metawidget.util.getId( "property", attributes, mw ) + '-tabs' );
tabs.appendChild( metawidget.util.createElement( mw, 'ul' ) );
this.getDelegate().layoutWidget( tabs, "property", {
wide: "true"
}, container, mw );
mw.tabLayoutDecorator = mw.tabLayoutDecorator || [];
mw.tabLayoutDecorator.push( tabs );
} else {
tabs = previousSectionWidget.parentNode;
}
// New Tab
var ul = tabs.childNodes[0];
var tabId = tabs.getAttribute( 'id' ) + ( ul.childNodes.length + 1 );
var li = metawidget.util.createElement( mw, 'li' );
var a = metawidget.util.createElement( mw, 'a' );
a.setAttribute( 'href', '#' + tabId );
a.hash = '#' + tabId;
li.appendChild( a );
ul.appendChild( li );
var tab = metawidget.util.createElement( mw, 'div' );
tab.setAttribute( 'id', tabId );
tabs.appendChild( tab );
// Tab name
a.innerHTML = section;
return tab;
};
/**
* JQuery UI WidgetFactory-based Metawidget.
*/
$.widget( "metawidget.metawidget", {
/**
* Default configuration
*/
options: {
inspector: new metawidget.inspector.PropertyTypeInspector(),
widgetBuilder: new metawidget.widgetbuilder.CompositeWidgetBuilder( [ new metawidget.widgetbuilder.OverriddenWidgetBuilder(),
new metawidget.jqueryui.widgetbuilder.JQueryUIWidgetBuilder(), new metawidget.widgetbuilder.ReadOnlyWidgetBuilder(), new metawidget.widgetbuilder.HtmlWidgetBuilder() ] ),
widgetProcessors: [ new metawidget.widgetprocessor.IdProcessor(), new metawidget.widgetprocessor.RequiredAttributeProcessor(),
new metawidget.widgetprocessor.PlaceholderAttributeProcessor(), new metawidget.widgetprocessor.DisabledAttributeProcessor(), new metawidget.jqueryui.widgetprocessor.JQueryUIBindingProcessor(),
new metawidget.widgetprocessor.SimpleBindingProcessor() ],
layout: new metawidget.layout.HeadingTagLayoutDecorator( new metawidget.layout.TableLayout() )
},
/**
* Constructor
*/
_create: function() {
// Pipeline (private, based on convention here:
// http://forum.jquery.com/topic/what-s-the-right-way-to-store-private-data-in-widget-s-instance)
this._pipeline = new metawidget.Pipeline( this.element[0] );
// Configure defaults
this._pipeline.configure( this.options );
// First time in, capture the contents of the Metawidget (if any)
this._overriddenNodes = [];
var element = this.element[0];
var mw = this;
element.getMetawidget = function() {
return mw;
};
for ( var loop = 0; loop < element.childNodes.length; ) {
if ( element.childNodes[loop].nodeType !== 1 ) {
loop++;
continue;
}
var childNode = element.childNodes[loop];
element.removeChild( childNode );
this._overriddenNodes.push( childNode );
}
},
/**
* Called when created, and later when changing options.
*/
_refresh: function( inspectionResult ) {
// Defensive copy
this.overriddenNodes = [];
for ( var loop = 0, length = this._overriddenNodes.length; loop < length; loop++ ) {
this.overriddenNodes.push( this._overriddenNodes[loop].cloneNode( true ) );
}
// Inspect (if necessary)
if ( inspectionResult === undefined ) {
// Safeguard against improperly implementing:
// http://blog.kennardconsulting.com/2013/02/metawidget-and-rest.html
if ( arguments.length > 0 ) {
throw new Error( "Calling _refresh( undefined ) may cause infinite loop. Check your argument, or pass no arguments instead" );
}
var splitPath = metawidget.util.splitPath( this.path );
inspectionResult = this._pipeline.inspect( this.toInspect, splitPath.type, splitPath.names, this );
}
// Build widgets
this._pipeline.buildWidgets( inspectionResult, this );
},
/**
* _setOptions is called with a hash of all options that are changing.
*/
_setOptions: function() {
this._superApply( arguments );
this._pipeline.configure( this.options );
},
setReadOnly: function( readOnly ) {
this.readOnly = readOnly;
},
/**
* Useful for WidgetBuilders to perform nested inspections (eg. for
* Collections).
*/
inspect: function( toInspect, type, names ) {
return this._pipeline.inspect( toInspect, type, names, this );
},
/**
* Overridden to use JQuery.empty (safer for memory leaks).
*/
clearWidgets: function() {
$( this.getElement() ).empty();
},
/**
* Inspect the given toInspect/path and build widgets.
* <p>
* Invoke using
* <tt>$( '#metawidget' ).metawidget( "buildWidgets", toInspect, path )</tt>.
*/
buildWidgets: function( toInspect, path ) {
if ( toInspect !== undefined ) {
this.toInspect = toInspect;
this.path = undefined;
}
if ( path !== undefined ) {
this.path = path;
}
this._refresh();
},
getWidgetProcessor: function( testInstanceOf ) {
return this._pipeline.getWidgetProcessor( testInstanceOf );
},
/**
* Returns the element this Metawidget is attached to.
*/
getElement: function() {
return this._pipeline.element;
},
buildNestedMetawidget: function( attributes, config ) {
// Create a 'div' not a 'metawidget', because whilst it's up to the
// user what they want their top-level element to be, for browser
// compatibility we should stick with something benign for nested
// elements
var nestedWidget = metawidget.util.createElement( this, 'div' );
// Duck-type our 'pipeline' as the 'config' of the nested
// Metawidget. This neatly passes everything down, including a
// decremented 'maximumInspectionDepth'
var nestedMetawidget = $( nestedWidget ).metawidget( this._pipeline );
nestedMetawidget.metawidget( "setReadOnly", this.readOnly || metawidget.util.isTrueOrTrueString( attributes.readOnly ) );
var nestedToInspect = this.toInspect;
var nestedPath = metawidget.util.appendPath( attributes, this );
nestedMetawidget.metawidget( "buildWidgets", nestedToInspect, nestedPath );
return nestedWidget;
}
} );
} )();