-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetawidget-webcomponent.js
254 lines (186 loc) · 7.6 KB
/
metawidget-webcomponent.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
// 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.
/**
* Web Component wrapper for Metawidget.
*
* @author <a href="http://kennardconsulting.com">Richard Kennard</a>
*/
var metawidget = metawidget || {};
( function( globalScope ) {
'use strict';
/**
* Use the value of the given HTML 5 attribute to lookup an object in the
* global scope. This includes traversing simple namespace paths such as
* 'foo.bar'
*/
function _lookupObject( attributeName ) {
var attributeValue = this.getAttribute( attributeName );
if ( attributeValue === null ) {
return;
}
var typeAndNames = metawidget.util.splitPath( attributeValue );
if ( typeAndNames === undefined ) {
return;
}
var lookup = globalScope[typeAndNames.type];
return metawidget.util.traversePath( lookup, typeAndNames.names );
}
if ( globalScope.document !== undefined && globalScope.document.registerElement !== undefined ) {
var metawidgetPrototype = Object.create( HTMLElement.prototype );
/**
* Upon attachedCallback, initialize an internal metawidget.Metawidget
* object using the current 'config' attribute (if any).
* <p>
* During initialization, a Metawidget create a shadow root so this must
* be called after the document is ready.
*/
metawidgetPrototype.attachedCallback = function() {
// First time in, create a shadow root. This allows us to preserve
// our original override nodes (if any)
var shadowRoot = this.createShadowRoot();
// Pipeline (private)
this._pipeline = new metawidget.Pipeline( shadowRoot );
// Configure defaults
this._pipeline.inspector = new metawidget.inspector.PropertyTypeInspector();
this._pipeline.widgetBuilder = new metawidget.widgetbuilder.CompositeWidgetBuilder( [ new metawidget.widgetbuilder.OverriddenWidgetBuilder(),
new metawidget.widgetbuilder.ReadOnlyWidgetBuilder(), new metawidget.widgetbuilder.HtmlWidgetBuilder() ] );
this._pipeline.widgetProcessors = [ new metawidget.widgetprocessor.IdProcessor(), new metawidget.widgetprocessor.RequiredAttributeProcessor(),
new metawidget.widgetprocessor.PlaceholderAttributeProcessor(), new metawidget.widgetprocessor.DisabledAttributeProcessor(),
new metawidget.widgetprocessor.SimpleBindingProcessor() ];
this._pipeline.layout = new metawidget.layout.HeadingTagLayoutDecorator( new metawidget.layout.TableLayout() );
this._pipeline.configure( [ _lookupObject.call( this, 'config' ), this.config ] );
this.buildWidgets();
};
/**
* If 'path', 'readonly' or 'config' are updated, rebuild the
* Metawidget.
*/
metawidgetPrototype.attributeChangedCallback = function( attrName, oldVal, newVal ) {
if ( this._pipeline === undefined ) {
return;
}
switch ( attrName ) {
case 'path':
this.buildWidgets();
break;
case 'readonly':
this.buildWidgets();
break;
case 'config':
this._pipeline.configure( _lookupObject.call( this, 'config' ) );
break;
}
};
/**
* Clear all child elements from the shadow root.
*/
metawidgetPrototype.clearWidgets = function() {
while ( this.shadowRoot.childNodes.length > 0 ) {
this.shadowRoot.removeChild( this.shadowRoot.childNodes[0] );
}
};
/**
* Rebuild the Metawidget, using the value of the current 'path'
* attribute.
*
* @param inspectionResult
* optional inspectionResult to use
*/
metawidgetPrototype.buildWidgets = function( inspectionResult ) {
// Take a copy of the original nodes. These may be inserted into the
// shadow DOM if the WidgetBuilders/Layouts wish
this.overriddenNodes = [];
for ( var loop = 0, length = this.childNodes.length; loop < length; loop++ ) {
if ( this.childNodes[loop].nodeType === 1 ) {
this.overriddenNodes.push( this.childNodes[loop].cloneNode( true ) );
}
}
// Traverse and build
if ( this.getAttribute( 'path' ) !== null ) {
this.path = this.getAttribute( 'path' );
this.readOnly = metawidget.util.isTrueOrTrueString( this.getAttribute( 'readonly' ) );
// 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 buildWidgets( undefined ) may cause infinite loop. Check your argument, or pass no arguments instead" );
}
var splitPath = metawidget.util.splitPath( this.path );
this.toInspect = globalScope[splitPath.type];
inspectionResult = this._pipeline.inspect( this.toInspect, splitPath.type, splitPath.names, this );
}
}
// Build widgets
this._pipeline.buildWidgets( inspectionResult, this );
// Note: we don't attempt to use Object.observe on this.toInspect,
// at least not by default (clients could observe and call
// buildWidgets if they want). AngularJS Metawidget does this, but
// in Angular all sub-widgets are 2-way bound by default, so you
// never risk losing data when you rebuild. In Web Components,
// however, sub-widget values are only saved when clients call
// save()
};
/**
* Returns a nested version of this same Metawidget, using the given
* attributes.
* <p>
* Subclasses should override this method to use their preferred widget
* creation methodology.
*/
metawidgetPrototype.buildNestedMetawidget = function( attributes, config ) {
var nestedMetawidget = metawidget.util.createElement( this, 'x-metawidget' );
// Wire up getMetawidget manually, because shadowRoot is not
// initialized until attachedCallback. This is important for
// SimpleBindingProcessor and nested Metawidgets
nestedMetawidget.getMetawidget = function() {
return nestedMetawidget;
};
// Duck-type our 'pipeline' as the 'config' of the nested
// Metawidget. This neatly passes everything down, including a
// decremented 'maximumInspectionDepth'
nestedMetawidget.setAttribute( 'path', metawidget.util.appendPath( attributes, this ) );
nestedMetawidget.setAttribute( 'readonly', this.readOnly || metawidget.util.isTrueOrTrueString( attributes.readOnly ) );
nestedMetawidget.config = this._pipeline;
return nestedMetawidget;
};
/**
* Save the contents of the Metawidget using a SimpleBindingProcessor.
* <p>
* This is a convenience method. To access other Metawidget APIs,
* clients can use the 'getWidgetProcessor' method
*
* @returns true if the 'toInspect' was updated (i.e. is dirty)
*/
metawidgetPrototype.save = function() {
return this.getWidgetProcessor( function( widgetProcessor ) {
return widgetProcessor instanceof metawidget.widgetprocessor.SimpleBindingProcessor;
} ).save( this );
};
/**
* Useful for WidgetBuilders to perform nested inspections (eg. for
* Collections).
*/
metawidgetPrototype.inspect = function( toInspect, type, names ) {
return this._pipeline.inspect( toInspect, type, names, this );
};
metawidgetPrototype.getWidgetProcessor = function( testInstanceOf ) {
return this._pipeline.getWidgetProcessor( testInstanceOf );
};
metawidgetPrototype.setLayout = function( layout ) {
this._pipeline.layout = layout;
};
// Register Metawidget as a Web Component
globalScope.document.registerElement( 'x-metawidget', {
prototype: metawidgetPrototype
} );
}
} )( this );