86
86
// Generally useful functions.
87
/* Helper to select the io_provider. */
88
module.get_io_provider = function(io_provider) {
89
if (io_provider === undefined) {
95
/* Helper to select the io_provider from a config. */
96
module.get_configured_io_provider = function(config, key) {
97
if (key === undefined) {
100
if (config === undefined || config[key] === undefined) {
106
87
module.append_qs = function(qs, key, value) {
107
88
/* Append a key-value pair to a query string. */
108
89
var elems = (qs && qs.length > 0) ? [qs] : [];
188
* Get the URL of the view for an Entry
189
* @method get_view_url
190
* @param {Entry} entry
191
* @param {String} view_name
192
* @param {String} namespace
193
* @param {String} query (optional) structured query variables to use.
194
* @return {String} URL
196
module.get_view_url = function(entry, view_name, namespace, query){
197
entry_url = Y.lp.get_url_path(entry.get('web_link'));
198
querystring = Y.QueryString.stringify(query);
199
if (querystring !== '') {
200
querystring = '?' + querystring;
203
entry_url + '/' + view_name + '/++' + namespace + '++' + querystring);
208
* Get the URL of the form for a view for an Entry
209
* @method get_form_url
210
* @param {Entry} entry
211
* @param {String} view_name
212
* @return {String} URL
214
module.get_form_url = function(entry, view_name) {
215
return module.get_view_url(entry, view_name, 'form');
220
* Load the model for a view.
222
* @param entry An Entry, i.e. a Lanchpad API object
223
* @param view_name The name of the view to retrieve the model for
224
* @param config An IO config.
225
* @param query (optional) The structured query variables to use.
227
module.load_model = function(entry, view_name, config, query){
228
var url = module.get_view_url(entry, view_name, 'model', query);
229
var old_on_success = config.on.success;
231
on.success = module.wrap_resource_on_success;
234
'arguments': [entry.lp_client, url, old_on_success, false]
236
var io_provider = module.get_configured_io_provider(config);
237
io_provider.io(url, y_config);
241
167
module.add_accept = function(config, headers) {
242
168
if (headers === undefined) {
341
267
var media_type = response.getResponseHeader('Content-Type');
342
268
if (media_type.substring(0,16) === 'application/json') {
343
269
representation = Y.JSON.parse(response.responseText);
344
// If the response contains a notification header, display the
346
notificaxns = response.getResponseHeader('X-Lazr-Notifications');
347
if (notificaxns !== null) {
348
module.display_notifications(notificaxns);
270
display_notifications(
271
response.getResponseHeader('X-Lazr-Notifications'));
350
272
wrapped = client.wrap_resource(uri, representation);
351
273
result = old_on_success(wrapped);
352
274
if (update_cache) {
382
304
* Display a list of notifications - error, warning, informational or debug.
383
305
* @param notifications An json encoded array of (level, message) tuples.
385
module.display_notifications = function (notifications) {
307
function display_notifications(notifications) {
386
308
if (notifications === undefined) {
389
if (notifications === 'null' || notifications === null) {
390
module.remove_notifications();
394
312
var notifications_by_level = {
473
390
'headers': {"Content-Type": hosted_file.content_type,
474
391
"Content-Disposition": disposition},
475
392
'arguments': args,
476
'data': hosted_file.contents,
477
'sync': this.lp_client.sync
479
this.io_provider.io(module.normalize_uri(hosted_file.uri), y_config);
393
'data': hosted_file.contents};
394
Y.io(module.normalize_uri(hosted_file.uri), y_config);
482
397
'lp_delete' : function(config) {
485
400
var args = hosted_file;
486
401
var y_config = { method: "DELETE",
489
sync: this.lp_client.sync
491
this.io_provider.io(hosted_file.uri, y_config);
404
Y.io(hosted_file.uri, y_config);
565
478
// The service root resource.
566
Root = function(client, representation, uri) {
479
module.Root = function(client, representation, uri) {
567
480
/* The root of the Launchpad web service. */
568
481
this.init(client, representation, uri);
570
Root.prototype = new Resource();
483
module.Root.prototype = new Resource();
575
486
var Collection = function(client, representation, uri) {
670
582
// The Launchpad client itself.
672
var Launchpad = function(config) {
584
var Launchpad = function() {
673
585
/* A client that makes HTTP requests to Launchpad's web service. */
674
this.io_provider = module.get_configured_io_provider(config);
675
this.sync = (config ? config.sync : false);
678
588
Launchpad.prototype = {
700
610
'arguments': [client, uri, old_on_success, update_cache],
701
611
'headers': headers,
705
return this.io_provider.io(uri, y_config);
614
return Y.io(uri, y_config);
708
617
'named_get' : function(uri, operation_name, config) {
754
663
'arguments': [client, uri, old_on_success, update_cache],
758
this.io_provider.io(uri, y_config);
761
669
'patch': function(uri, representation, config, headers) {
762
var on = Y.merge(config.on);
763
671
var data = Y.JSON.stringify(representation);
764
672
uri = module.normalize_uri(uri);
789
697
'headers': extra_headers,
790
698
'arguments': args,
794
this.io_provider.io(uri, y_config);
797
704
'wrap_resource': function(uri, representation) {
799
var new_representation;
800
705
/* Given a representation, turn it into a subclass of Resource. */
801
706
if (representation === null || representation === undefined) {
802
707
return representation;
812
717
// It's a list. Treat it as a collection;
813
718
// it should be slicable.
814
719
return new Collection(this, representation, uri);
815
} else if (Y.Lang.isObject(representation)) {
816
// It's an Array or mapping. Recurse into it.
817
if (Y.Lang.isArray(representation)) {
818
new_representation = [];
821
new_representation = {};
823
for (key in representation) {
824
if (representation.hasOwnProperty(key)) {
825
var value = representation[key];
826
if (Y.Lang.isValue(value)) {
827
value = this.wrap_resource(
828
value.self_link, value);
830
new_representation[key] = value;
833
return new_representation;
835
721
// It's a random JSON object. Leave it alone.
836
722
return representation;
886
772
showError: function (error_msg) {},
889
* Handle an error from an XHR request.
891
* This method is invoked before any generic error handling is done.
893
* @method handleError
894
* @param ioId The request id.
895
* @param response The XHR call response object.
896
* @return {Boolean} Return true if the error has been fully processed and
897
* any further generic error handling is not required.
899
handleError: function(ioId, response) {
904
775
* Return a failure handler function for XHR requests.
906
777
* Assign the result of this function as the failure handler when
913
784
return function(ioId, o) {
914
785
self.clearProgressUI();
915
// Perform any user specified error handling. If true is returned,
916
// we do not do any further processing.
917
if( self.handleError(ioId, o) ) {
920
786
// If it was a timeout...
921
787
if (o.status === 503) {
978
844
module.FormErrorHandler = FormErrorHandler;
982
{"requires":["attribute", "io", "querystring", "json-parse",
983
"json-stringify", "lp"]});
847
}, "0.1" ,{"requires":["attribute", "io", "json-parse", "json-stringify"]});
986
850
YUI.add('lp.client.plugins', function (Y) {
1201
1065
Y.namespace('lp.client.plugins');
1202
1066
Y.lp.client.plugins.PATCHPlugin = PATCHPlugin;
1204
}, "0.1", {"requires": [
1205
"plugin", "dump", "lazr.editor", "lp.client"]});
1068
}, "0.1", {"requires": ["plugin", "dump", "lazr.editor", "lp.client"]});