~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/javascript/client.js

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-06-25 08:55:37 UTC
  • mfrom: (13287.1.8 bug-800652)
  • Revision ID: launchpad@pqm.canonical.com-20110625085537-moikyoo2pe98zs7r
[r=jcsackett, julian-edwards][bug=800634,
        800652] Enable and display overrides on sync package uploads.

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
};
85
85
 
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) {
90
 
        return Y;
91
 
    }
92
 
    return io_provider;
93
 
};
94
 
 
95
 
/* Helper to select the io_provider from a config. */
96
 
module.get_configured_io_provider = function(config, key) {
97
 
    if (key === undefined) {
98
 
        key = 'io_provider';
99
 
    }
100
 
    if (config === undefined || config[key] === undefined) {
101
 
        return Y;
102
 
    }
103
 
    return config[key];
104
 
};
105
 
 
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] : [];
183
164
    }
184
165
};
185
166
 
186
 
 
187
 
/**
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
195
 
 */
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;
201
 
    }
202
 
    return (
203
 
        entry_url + '/' + view_name + '/++' + namespace + '++' + querystring);
204
 
};
205
 
 
206
 
 
207
 
/**
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
213
 
 */
214
 
module.get_form_url = function(entry, view_name) {
215
 
    return module.get_view_url(entry, view_name, 'form');
216
 
};
217
 
 
218
 
 
219
 
/**
220
 
 * Load the model for a view.
221
 
 *
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.
226
 
 */
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;
230
 
    var on = config.on;
231
 
    on.success = module.wrap_resource_on_success;
232
 
    var y_config = {
233
 
        on: on,
234
 
        'arguments': [entry.lp_client, url, old_on_success, false]
235
 
    };
236
 
    var io_provider = module.get_configured_io_provider(config);
237
 
    io_provider.io(url, y_config);
238
 
};
239
 
 
240
 
 
241
167
module.add_accept = function(config, headers) {
242
168
    if (headers === undefined) {
243
169
        headers = {};
258
184
    return data;
259
185
};
260
186
 
261
 
var update_cached_object = function (cache_name, cache, entry)
 
187
function update_cached_object(cache_name, cache, entry)
262
188
{
263
189
  var fields_changed = [];
264
190
  var name;
305
231
    };
306
232
    Y.fire(event_name, event_);
307
233
  }
308
 
};
 
234
}
309
235
 
310
236
 
311
237
module.update_cache = function(entry) {
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
345
 
            // notifications.
346
 
            notificaxns = response.getResponseHeader('X-Lazr-Notifications');
347
 
            if (notificaxns !== null) {
348
 
                module.display_notifications(notificaxns);
349
 
            }
 
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.
384
306
 */
385
 
module.display_notifications = function (notifications) {
 
307
function display_notifications(notifications) {
386
308
    if (notifications === undefined) {
387
309
        return;
388
310
    }
389
 
    if (notifications === 'null' || notifications === null) {
390
 
        module.remove_notifications();
391
 
        return;
392
 
    }
393
311
 
394
312
    var notifications_by_level = {
395
313
        'level10': {
431
349
            last_message = node;
432
350
        });
433
351
    });
434
 
};
 
352
}
435
353
 
436
354
/**
437
355
 * Remove any notifications that are currently displayed.
456
374
    this.uri = uri;
457
375
    this.content_type = content_type;
458
376
    this.contents = contents;
459
 
    this.io_provider = client.io_provider;
460
377
};
461
378
 
462
379
HostedFile.prototype = {
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
478
 
        };
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);
480
395
    },
481
396
 
482
397
    'lp_delete' : function(config) {
485
400
        var args = hosted_file;
486
401
        var y_config = { method: "DELETE",
487
402
                         on: on,
488
 
                         'arguments': args,
489
 
                         sync: this.lp_client.sync
490
 
                       };
491
 
        this.io_provider.io(hosted_file.uri, y_config);
 
403
                         'arguments': args };
 
404
        Y.io(hosted_file.uri, y_config);
492
405
    }
493
406
};
494
407
 
563
476
 
564
477
 
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);
569
482
};
570
 
Root.prototype = new Resource();
571
 
 
572
 
module.Root = Root;
 
483
module.Root.prototype = new Resource();
573
484
 
574
485
 
575
486
var Collection = function(client, representation, uri) {
628
539
 
629
540
Entry.prototype.lp_save = function(config) {
630
541
    /* Write modifications to this entry back to the web service. */
 
542
    var on = config.on;
631
543
    var representation = {};
632
544
    var entry = this;
633
545
    Y.each(this.dirty_attributes, function(attribute, key) {
669
581
 
670
582
// The Launchpad client itself.
671
583
 
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);
676
586
};
677
587
 
678
588
Launchpad.prototype = {
699
609
            on: on,
700
610
            'arguments': [client, uri, old_on_success, update_cache],
701
611
            'headers': headers,
702
 
            data: data,
703
 
            sync: this.sync
 
612
            data: data
704
613
        };
705
 
        return this.io_provider.io(uri, y_config);
 
614
        return Y.io(uri, y_config);
706
615
    },
707
616
 
708
617
    'named_get' : function(uri, operation_name, config) {
752
661
            method: "POST",
753
662
            on: on,
754
663
            'arguments': [client, uri, old_on_success, update_cache],
755
 
            data: data,
756
 
            sync: this.sync
 
664
            data: data
757
665
        };
758
 
        this.io_provider.io(uri, y_config);
 
666
        Y.io(uri, y_config);
759
667
    },
760
668
 
761
669
    'patch': function(uri, representation, config, headers) {
762
 
        var on = Y.merge(config.on);
 
670
        var on = config.on;
763
671
        var data = Y.JSON.stringify(representation);
764
672
        uri = module.normalize_uri(uri);
765
673
 
788
696
            'on': on,
789
697
            'headers': extra_headers,
790
698
            'arguments': args,
791
 
            'data': data,
792
 
            'sync': this.sync
 
699
            'data': data
793
700
        };
794
 
        this.io_provider.io(uri, y_config);
 
701
        Y.io(uri, y_config);
795
702
    },
796
703
 
797
704
    'wrap_resource': function(uri, representation) {
798
 
        var key;
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 = [];
819
 
                }
820
 
                else {
821
 
                    new_representation = {};
822
 
                }
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);
829
 
                        }
830
 
                        new_representation[key] = value;
831
 
                    }
832
 
                }
833
 
                return new_representation;
834
720
            } else {
835
721
                // It's a random JSON object. Leave it alone.
836
722
                return representation;
886
772
    showError: function (error_msg) {},
887
773
 
888
774
    /**
889
 
     * Handle an error from an XHR request.
890
 
     *
891
 
     * This method is invoked before any generic error handling is done.
892
 
     *
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.
898
 
     */
899
 
    handleError: function(ioId, response) {
900
 
        return false;
901
 
    },
902
 
 
903
 
    /**
904
775
     * Return a failure handler function for XHR requests.
905
776
     *
906
777
     * Assign the result of this function as the failure handler when
912
783
        var self = this;
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) ) {
918
 
                return;
919
 
            }
920
786
            // If it was a timeout...
921
787
            if (o.status === 503) {
922
788
                self.showError(
978
844
module.FormErrorHandler = FormErrorHandler;
979
845
 
980
846
 
981
 
}, "0.1",
982
 
    {"requires":["attribute", "io", "querystring", "json-parse",
983
 
                 "json-stringify", "lp"]});
 
847
}, "0.1" ,{"requires":["attribute", "io", "json-parse", "json-stringify"]});
984
848
 
985
849
 
986
850
YUI.add('lp.client.plugins', function (Y) {
999
863
 * @class PATCHPlugin
1000
864
 * @extends Widget
1001
865
 */
1002
 
var PATCHPlugin = function PATCHPlugin () {
 
866
function PATCHPlugin () {
1003
867
    PATCHPlugin.superclass.constructor.apply(this, arguments);
1004
 
};
 
868
}
1005
869
 
1006
870
Y.mix(PATCHPlugin, {
1007
871
    /**
1201
1065
Y.namespace('lp.client.plugins');
1202
1066
Y.lp.client.plugins.PATCHPlugin = PATCHPlugin;
1203
1067
 
1204
 
  }, "0.1", {"requires": [
1205
 
                  "plugin", "dump", "lazr.editor", "lp.client"]});
 
1068
  }, "0.1", {"requires": ["plugin", "dump", "lazr.editor", "lp.client"]});