@@ -35,7 +35,7 @@ $(document).ready(function(){
35
35
} ) ;
36
36
37
37
$ ( '#graph-render' ) . on ( 'click' , function ( ) {
38
- var graph = JSON . parse ( $ ( 'pre#log' ) . text ( ) . trim ( ) ) ;
38
+ var graph = JSON . parse ( $ ( 'pre#graph- log' ) . text ( ) . trim ( ) ) ;
39
39
$ ( '#graph' ) . html ( '' ) ;
40
40
renderGraph ( graph , '#graph' ) ;
41
41
} ) ;
@@ -62,7 +62,8 @@ actions.processResponse = function(response) {
62
62
$ ( '#entities' ) . html ( '' ) ;
63
63
if ( responseObj [ 'entities' ] . length ) {
64
64
$ ( '#raw-results' ) . show ( ) ;
65
- $ ( '#log' ) . text ( JSON . stringify ( responseObj . graph , undefined , 2 ) ) ;
65
+ $ ( '#entities-log' ) . text ( JSON . stringify ( responseObj . entities , undefined , 2 ) ) ;
66
+ $ ( '#graph-log' ) . text ( JSON . stringify ( responseObj . graph , undefined , 2 ) ) ;
66
67
actions . displayEntities ( responseObj [ 'entities' ] )
67
68
}
68
69
} ;
@@ -104,8 +105,18 @@ actions.renderSnippet = function(el, entity) {
104
105
//console.log(operation)
105
106
var actionWidgetClass = actions . actionTypeToWidgetMap [ operation [ '@type' ] ] ;
106
107
if ( actionWidgetClass ) {
107
- var widget = new actionWidgetClass ( operation ) ;
108
- widget . render ( $ ( '.action-widget' ) , $ ( '.action-log' ) ) ;
108
+ try {
109
+ var widget = new actionWidgetClass ( operation ) ;
110
+ widget . render ( $ ( '.action-widget' ) , $ ( '.action-log' ) ) ;
111
+ } catch ( e ) {
112
+
113
+ var msg = ( e instanceof actions . ActionWidget . Exception ) ?
114
+ e . message : 'Application error' ;
115
+ if ( $ ( '#validation-errors' ) . text ( ) ) {
116
+ $ ( '#validation-errors' ) . append ( $ ( '<br>' ) ) ;
117
+ }
118
+ $ ( '#validation-errors' ) . append ( msg ) ;
119
+ }
109
120
} else {
110
121
if ( $ ( '#validation-errors' ) . text ( ) ) {
111
122
$ ( '#validation-errors' ) . append ( $ ( '<br>' ) ) ;
@@ -121,19 +132,24 @@ actions.renderSnippet = function(el, entity) {
121
132
// Actions widget base class ***************************************************
122
133
123
134
124
- actions . ActionWidget = function ( operation ) {
125
- this . operation_ = operation ;
135
+ actions . ActionWidget = function ( name , handler ) {
126
136
this . button_ = null ;
127
137
this . log_ = null ;
138
+ if ( ! name ) {
139
+ throw new actions . ActionWidget . Exception ( 'Missing action name' ) ;
140
+ }
141
+ if ( ! handler ) {
142
+ throw new actions . ActionWidget . Exception ( 'Missing action handler' ) ;
143
+ }
144
+ this . name_ = name ;
145
+ this . handler_ = handler ;
146
+ } ;
128
147
129
- var handler = operation [ 'http://schema.org/actionHandler' ] ;
130
- this . method_ = handler [ 'http://schema.org/httpMethod' ] ;
131
- this . name_ = handler [ 'http://schema.org/name' ] ;
132
- this . url_ = handler [ 'http://schema.org/url' ] [ '@id' ] ;
133
148
134
- var handlerClass = actions . actionHandlerTypes [ handler [ '@type' ] ] ;
135
- this . handler_ = new handlerClass ( this . url_ ) ;
136
- } ;
149
+ actions . ActionWidget . Exception = function ( message ) {
150
+ this . message = message ;
151
+ this . name = 'ActionWidgetException' ;
152
+ }
137
153
138
154
139
155
actions . ActionWidget . prototype . render = function ( widgetParent , logParent ) {
@@ -212,6 +228,51 @@ actions.QuoteActionWidget.prototype.launch = function() {
212
228
} ;
213
229
214
230
231
+ // SearchAction widget **********************************************************
232
+
233
+ actions . SearchActionWidget = function ( operation ) {
234
+
235
+ //TODO(ewag): Factor out.
236
+
237
+ var handler = operation [ 'http://schema.org/actionHandler' ] ;
238
+ if ( ! handler ) {
239
+ throw new actions . ActionWidget . Exception ( 'Missing action handler' ) ;
240
+ }
241
+ var name = handler [ 'http://schema.org/name' ] || 'Search' ;
242
+ if ( ! ( handler [ 'http://schema.org/url' ] &&
243
+ handler [ 'http://schema.org/url' ] [ '@id' ] ) ) {
244
+ throw new actions . ActionWidget . Exception ( 'Missing handler url' )
245
+ }
246
+ var url = handler [ 'http://schema.org/url' ] [ '@id' ] ;
247
+
248
+ var handlerClass = actions . actionHandlerTypes [ handler [ '@type' ] ] ;
249
+ if ( ! handlerClass ) {
250
+ throw new actions . ActionWidget . Exception (
251
+ 'Unknown handler type: ' + handler [ '@type' ] ) ;
252
+ }
253
+ var actionHandler = new handlerClass ( url ) ;
254
+
255
+ actions . ActionWidget . call ( this , name , actionHandler ) ;
256
+ } ;
257
+ actions . SearchActionWidget . prototype = Object . create (
258
+ actions . ActionWidget . prototype ) ;
259
+
260
+
261
+ actions . SearchActionWidget . prototype . launch = function ( ) {
262
+ var el = $ ( '<div class="result"></div>' ) ;
263
+ this . log_ . append ( el ) ;
264
+
265
+ console . log ( this . handler_ )
266
+
267
+ var callback = function ( success , content ) {
268
+ this . updateWidgetState ( success ) ;
269
+ console . log ( JSON . parse ( content ) ) ;
270
+ }
271
+
272
+ this . handler_ . trigger ( { 'test' : 'someval' } , el , $ . proxy ( callback , this ) ) ;
273
+ } ;
274
+
275
+
215
276
// Action Handlers *************************************************************
216
277
217
278
actions . ActionHandler = function ( url ) {
@@ -246,8 +307,8 @@ actions.HttpHandler.prototype.trigger = function(params, resultBox, callback) {
246
307
} ) ,
247
308
contentType : 'application/json; charset=utf-8' ,
248
309
complete : $ . proxy ( function ( e ) {
249
- var success = this . callback ( resultBox , e ) ;
250
- callback ( success ) ;
310
+ var successAndResponse = this . callback ( resultBox , e ) ;
311
+ callback ( successAndResponse [ 0 ] , successAndResponse [ 1 ] ) ;
251
312
} , this )
252
313
}
253
314
// What is the name of the param?
@@ -260,7 +321,7 @@ actions.HttpHandler.prototype.callback = function(el, e) {
260
321
var success = false ;
261
322
if ( e . status !== 200 ) {
262
323
el . append ( $ ( '<p><span class="text-danger">Server error</span></p> ' ) ) ;
263
- return success ;
324
+ return [ success , null ] ;
264
325
}
265
326
var response = JSON . parse ( e . responseText )
266
327
if ( response . errors && response . errors . length ) {
@@ -283,7 +344,8 @@ actions.HttpHandler.prototype.callback = function(el, e) {
283
344
'</span></p>' +
284
345
'<p class="debug">Debug: ' + response . result . debug + '</p>' ) ) ;
285
346
}
286
- return success ;
347
+ var content = response . result ? response . result . content : null ;
348
+ return [ success , content ] ;
287
349
}
288
350
289
351
@@ -310,7 +372,8 @@ actions.WebPageHandler.prototype.trigger = function(
310
372
311
373
actions . actionTypeToWidgetMap = {
312
374
'http://schema.org/ReviewAction' : actions . ReviewActionWidget ,
313
- 'http://schema.org/QuoteAction' : actions . QuoteActionWidget
375
+ 'http://schema.org/QuoteAction' : actions . QuoteActionWidget ,
376
+ 'http://schema.org/SearchAction' : actions . SearchActionWidget
314
377
} ;
315
378
316
379
0 commit comments