~launchpad-pqm/launchpad/devel

14487.2.2 by Aaron Bentley
Get most tests passing.
1
/* Copyright 2011 Canonical Ltd.  This software is licensed under the
2
 * GNU Affero General Public License version 3 (see the file LICENSE).
3
 *
4
 * Client-side rendering of bug listings.
5
 *
6
 * @module bugs
7
 * @submodule buglisting
8
 */
9
10
YUI.add('lp.bugs.buglisting', function(Y) {
11
14487.2.13 by Aaron Bentley
namespace -> module
12
var module = Y.namespace('lp.bugs.buglisting');
14487.2.2 by Aaron Bentley
Get most tests passing.
13
14
15
/**
16
 * Constructor.
17
 *
18
 * This is the model of the current batch, including the ordering, position,
19
 * and what fields are visibile.
20
 *
21
 * These values are stored in the History object, so that the browser
22
 * back/next buttons correctly adjust.  The system defaults for field
23
 * visibility are fixed, so they are stored directly on the object.
24
 *
25
 * Accepts a config containing:
26
 *  - field_visibility the requested field visibility as an associative array
27
 *  - field_visibility_defaults the system defaults for field visibility as an
28
 *    associative array.
29
 *  - batch_key: A string representing the position and ordering of the
14487.2.10 by Aaron Bentley
Turn get_batch_key into a plain function.
30
 *    current batch, as returned by listing_navigator.get_batch_key
14487.2.2 by Aaron Bentley
Get most tests passing.
31
 */
14487.2.19 by Aaron Bentley
Change function curly brace formatting.
32
module.BugListingModel = function() {
14487.2.13 by Aaron Bentley
namespace -> module
33
    module.BugListingModel.superclass.constructor.apply(this, arguments);
14487.2.2 by Aaron Bentley
Get most tests passing.
34
};
35
36
14487.2.13 by Aaron Bentley
namespace -> module
37
module.BugListingModel.NAME = 'buglisting-model';
38
39
40
module.BugListingModel.ATTRS = {
14487.2.2 by Aaron Bentley
Get most tests passing.
41
    field_visibility_defaults: {
42
        value: null
43
    }
44
};
45
46
14487.2.13 by Aaron Bentley
namespace -> module
47
Y.extend(module.BugListingModel, Y.Base, {
14487.2.2 by Aaron Bentley
Get most tests passing.
48
    /**
49
     * Initializer sets up the History object that stores most of the model
50
     * data.
51
     */
14487.2.19 by Aaron Bentley
Change function curly brace formatting.
52
    initializer: function(config) {
14487.2.2 by Aaron Bentley
Get most tests passing.
53
        this.set('history', new Y.History({
54
            initialState: Y.merge(
55
                config.field_visibility, {batch_key: config.batch_key})
56
        }));
57
    },
58
59
    /**
60
     * Return the current field visibility, as an associative array.
61
     * Since the history contains field values that are not field-visibility,
62
     * use field_visibility_defaults to filter out non-field-visibility
63
     * values.
64
     */
14487.2.19 by Aaron Bentley
Change function curly brace formatting.
65
    get_field_visibility: function() {
14487.2.2 by Aaron Bentley
Get most tests passing.
66
        var result = this.get('history').get();
67
        var key_source = this.get('field_visibility_defaults');
14487.2.19 by Aaron Bentley
Change function curly brace formatting.
68
        Y.each(result, function(value, key) {
14487.2.2 by Aaron Bentley
Get most tests passing.
69
            if (!key_source.hasOwnProperty(key)){
70
                delete result[key];
71
            }
72
        });
73
        return result;
74
    },
75
76
    /**
77
     * Set the field visibility, updating history.  Accepts an associative
78
     * array.
79
     */
14487.2.19 by Aaron Bentley
Change function curly brace formatting.
80
    set_field_visibility: function(value) {
14487.2.2 by Aaron Bentley
Get most tests passing.
81
        this.get('history').add(value);
14487.2.9 by Aaron Bentley
Provide abstract batch_key interface for BugModel.
82
    },
83
84
    /**
85
     * Return the current batch key.
86
     */
14487.2.19 by Aaron Bentley
Change function curly brace formatting.
87
    get_batch_key: function() {
14487.2.9 by Aaron Bentley
Provide abstract batch_key interface for BugModel.
88
        return this.get('history').get('batch_key');
89
    },
90
91
    /**
92
     * Set the current batch.  The batch_key and the query mapping identifying
93
     * the batch must be supplied.
94
     */
14487.2.19 by Aaron Bentley
Change function curly brace formatting.
95
    set_batch: function(batch_key, query) {
14487.2.9 by Aaron Bentley
Provide abstract batch_key interface for BugModel.
96
        var url = '?' + Y.QueryString.stringify(query);
97
        this.get('history').addValue('batch_key', batch_key, {url: url});
14487.2.2 by Aaron Bentley
Get most tests passing.
98
    }
99
});
100
101
14487.2.17 by Aaron Bentley
Cleanup.
102
/**
103
 * Constructor.
104
 * current_url is used to determine search params.
105
 * cache is the JSONRequestCache for the batch.
106
 * template is the template to use for rendering batches.
107
 * target is a YUI node to update when rendering batches.
108
 * navigation_indices is a YUI NodeList of nodes to update with the current
109
 * batch info.
110
 * io_provider is something providing the Y.io interface, typically used for
111
 * testing.  Defaults to Y.io.
112
 */
14487.2.13 by Aaron Bentley
namespace -> module
113
module.BugListingNavigator = function(config) {
114
    module.BugListingNavigator.superclass.constructor.apply(
14487.2.2 by Aaron Bentley
Get most tests passing.
115
        this, arguments);
116
};
117
14487.2.13 by Aaron Bentley
namespace -> module
118
module.BugListingNavigator.ATTRS = {
14487.2.2 by Aaron Bentley
Get most tests passing.
119
};
120
121
Y.extend(
14487.2.13 by Aaron Bentley
namespace -> module
122
    module.BugListingNavigator,
14487.2.8 by Aaron Bentley
Move event handling to BugListingNavigator.
123
    Y.lp.app.listing_navigator.ListingNavigator, {
14515.1.2 by Rick Harding
Merge with devel and readjust the help init with changes to listing navigator
124
    _bindUI: function () {
125
        initInlineHelp();
126
    },
127
14487.2.19 by Aaron Bentley
Change function curly brace formatting.
128
    initializer: function(config) {
14487.2.8 by Aaron Bentley
Move event handling to BugListingNavigator.
129
        this.get('model').get('history').after(
130
            'change', this.history_changed, this);
131
    },
132
    /**
133
     * Event handler for history:change events.
134
     */
14487.2.19 by Aaron Bentley
Change function curly brace formatting.
135
    history_changed: function(e) {
14487.2.8 by Aaron Bentley
Move event handling to BugListingNavigator.
136
        if (e.newVal.hasOwnProperty('batch_key')) {
137
            var batch_key = e.newVal.batch_key;
138
            var batch = this.get('batches')[batch_key];
139
            this.pre_fetch_batches();
140
            this.render();
14515.1.2 by Rick Harding
Merge with devel and readjust the help init with changes to listing navigator
141
            this._bindUI();
14487.2.8 by Aaron Bentley
Move event handling to BugListingNavigator.
142
        }
143
        else {
144
            // Handle Chrom(e|ium)'s initial popstate.
145
            this.get('model').get('history').replace(e.prevVal);
146
        }
14487.2.15 by Aaron Bentley
Remove field_visibility from basic ListingNavigator model.
147
    },
148
14487.2.17 by Aaron Bentley
Cleanup.
149
    /**
150
     * Return the model to use for rendering the batch.  This will include
151
     * updates to field visibility.
152
     */
14487.2.19 by Aaron Bentley
Change function curly brace formatting.
153
    get_render_model: function(current_batch) {
14487.2.15 by Aaron Bentley
Remove field_visibility from basic ListingNavigator model.
154
        return Y.merge(
155
            current_batch.mustache_model,
156
            this.get('model').get_field_visibility());
157
    },
158
159
    /**
160
     * Handle a previously-unseen batch by storing it in the cache and
161
     * stripping out field_visibility values that would otherwise shadow the
162
     * real values.
163
     */
14487.2.19 by Aaron Bentley
Change function curly brace formatting.
164
    handle_new_batch: function(batch) {
14487.2.15 by Aaron Bentley
Remove field_visibility from basic ListingNavigator model.
165
        var key, i;
166
        Y.each(batch.field_visibility, function(value, key) {
14487.2.20 by Aaron Bentley
bugtasks -> items in mustache model for full generality.
167
            for (i = 0; i < batch.mustache_model.items.length; i++) {
168
                delete batch.mustache_model.items[i][key];
14487.2.15 by Aaron Bentley
Remove field_visibility from basic ListingNavigator model.
169
            }
170
        });
171
        return this.constructor.superclass.handle_new_batch.call(this, batch);
14487.2.16 by Aaron Bentley
Cleanup.
172
    }
14487.2.15 by Aaron Bentley
Remove field_visibility from basic ListingNavigator model.
173
14487.2.8 by Aaron Bentley
Move event handling to BugListingNavigator.
174
},{
14487.2.2 by Aaron Bentley
Get most tests passing.
175
    make_model: function(batch_key, cache) {
14487.2.13 by Aaron Bentley
namespace -> module
176
        return new module.BugListingModel({
14487.2.2 by Aaron Bentley
Get most tests passing.
177
                batch_key: batch_key,
178
                field_visibility: cache.field_visibility,
179
                field_visibility_defaults: cache.field_visibility_defaults
180
        });
14487.2.6 by Aaron Bentley
Extract search param functionality.
181
    },
14487.2.19 by Aaron Bentley
Change function curly brace formatting.
182
    get_search_params: function(config) {
14487.2.6 by Aaron Bentley
Extract search param functionality.
183
        var search_params = Y.lp.app.listing_navigator.get_query(
184
            config.current_url);
185
        delete search_params.start;
186
        delete search_params.memo;
187
        delete search_params.direction;
188
        delete search_params.orderby;
189
        return search_params;
14487.2.2 by Aaron Bentley
Get most tests passing.
190
    }
191
});
192
193
/**
14487.2.10 by Aaron Bentley
Turn get_batch_key into a plain function.
194
 * Factory to return a BugListingNavigator for the given page.
14487.2.2 by Aaron Bentley
Get most tests passing.
195
 */
14487.2.13 by Aaron Bentley
namespace -> module
196
module.BugListingNavigator.from_page = function() {
14487.2.2 by Aaron Bentley
Get most tests passing.
197
    var target = Y.one('#client-listing');
198
    if (Y.Lang.isNull(target)){
199
        return null;
200
    }
201
    var navigation_indices = Y.all('.batch-navigation-index');
14487.2.5 by Aaron Bentley
fix lint.
202
    var pre_fetch = Y.lp.app.listing_navigator.get_feature_flag(
203
        'bugs.dynamic_bug_listings.pre_fetch');
14487.2.3 by Aaron Bentley
Extract bug-specific tests.
204
    Y.lp.app.listing_navigator.linkify_navigation();
14487.2.13 by Aaron Bentley
namespace -> module
205
    var navigator = new module.BugListingNavigator({
14487.2.2 by Aaron Bentley
Get most tests passing.
206
        current_url: window.location,
207
        cache: LP.cache,
208
        template: LP.mustache_listings,
209
        target: target,
210
        navigation_indices: navigation_indices,
211
        pre_fetch: Boolean(pre_fetch)
212
    });
213
    navigator.set('backwards_navigation', Y.all('.first,.previous'));
214
    navigator.set('forwards_navigation', Y.all('.last,.next'));
215
    navigator.clickAction('.first', navigator.first_batch);
216
    navigator.clickAction('.next', navigator.next_batch);
217
    navigator.clickAction('.previous', navigator.prev_batch);
218
    navigator.clickAction('.last', navigator.last_batch);
219
    navigator.render_navigation();
220
    return navigator;
221
};
222
223
224
225
}, "0.1", {
226
    "requires": [
14487.2.12 by Aaron Bentley
Clean up dependencies.
227
        "history", "node", 'lp.app.listing_navigator']
14487.2.2 by Aaron Bentley
Get most tests passing.
228
});