author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:28:13 +0200 | |
changeset 9 | 177826044cd9 |
parent 7 | cf61fcea0001 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
9 | 1 |
/** |
2 |
* @output wp-admin/js/theme.js |
|
3 |
*/ |
|
4 |
||
5 |
/* global _wpThemeSettings, confirm, tb_position */ |
|
5 | 6 |
window.wp = window.wp || {}; |
7 |
||
8 |
( function($) { |
|
9 |
||
10 |
// Set up our namespace... |
|
11 |
var themes, l10n; |
|
12 |
themes = wp.themes = wp.themes || {}; |
|
13 |
||
14 |
// Store the theme data and settings for organized and quick access |
|
15 |
// themes.data.settings, themes.data.themes, themes.data.l10n |
|
16 |
themes.data = _wpThemeSettings; |
|
17 |
l10n = themes.data.l10n; |
|
18 |
||
19 |
// Shortcut for isInstall check |
|
20 |
themes.isInstall = !! themes.data.settings.isInstall; |
|
21 |
||
22 |
// Setup app structure |
|
23 |
_.extend( themes, { model: {}, view: {}, routes: {}, router: {}, template: wp.template }); |
|
24 |
||
25 |
themes.Model = Backbone.Model.extend({ |
|
26 |
// Adds attributes to the default data coming through the .org themes api |
|
27 |
// Map `id` to `slug` for shared code |
|
28 |
initialize: function() { |
|
29 |
var description; |
|
30 |
||
31 |
// If theme is already installed, set an attribute. |
|
32 |
if ( _.indexOf( themes.data.installedThemes, this.get( 'slug' ) ) !== -1 ) { |
|
33 |
this.set({ installed: true }); |
|
34 |
} |
|
35 |
||
36 |
// Set the attributes |
|
37 |
this.set({ |
|
38 |
// slug is for installation, id is for existing. |
|
39 |
id: this.get( 'slug' ) || this.get( 'id' ) |
|
40 |
}); |
|
41 |
||
42 |
// Map `section.description` to `description` |
|
43 |
// as the API sometimes returns it differently |
|
44 |
if ( this.has( 'sections' ) ) { |
|
45 |
description = this.get( 'sections' ).description; |
|
46 |
this.set({ description: description }); |
|
47 |
} |
|
48 |
} |
|
49 |
}); |
|
50 |
||
51 |
// Main view controller for themes.php |
|
52 |
// Unifies and renders all available views |
|
53 |
themes.view.Appearance = wp.Backbone.View.extend({ |
|
54 |
||
55 |
el: '#wpbody-content .wrap .theme-browser', |
|
56 |
||
57 |
window: $( window ), |
|
58 |
// Pagination instance |
|
59 |
page: 0, |
|
60 |
||
61 |
// Sets up a throttler for binding to 'scroll' |
|
62 |
initialize: function( options ) { |
|
63 |
// Scroller checks how far the scroll position is |
|
64 |
_.bindAll( this, 'scroller' ); |
|
65 |
||
66 |
this.SearchView = options.SearchView ? options.SearchView : themes.view.Search; |
|
67 |
// Bind to the scroll event and throttle |
|
68 |
// the results from this.scroller |
|
69 |
this.window.bind( 'scroll', _.throttle( this.scroller, 300 ) ); |
|
70 |
}, |
|
71 |
||
72 |
// Main render control |
|
73 |
render: function() { |
|
74 |
// Setup the main theme view |
|
75 |
// with the current theme collection |
|
76 |
this.view = new themes.view.Themes({ |
|
77 |
collection: this.collection, |
|
78 |
parent: this |
|
79 |
}); |
|
80 |
||
81 |
// Render search form. |
|
82 |
this.search(); |
|
83 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
this.$el.removeClass( 'search-loading' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
|
5 | 86 |
// Render and append |
87 |
this.view.render(); |
|
88 |
this.$el.empty().append( this.view.el ).addClass( 'rendered' ); |
|
89 |
}, |
|
90 |
||
91 |
// Defines search element container |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
searchContainer: $( '.search-form' ), |
5 | 93 |
|
94 |
// Search input and view |
|
95 |
// for current theme collection |
|
96 |
search: function() { |
|
97 |
var view, |
|
98 |
self = this; |
|
99 |
||
100 |
// Don't render the search if there is only one theme |
|
101 |
if ( themes.data.themes.length === 1 ) { |
|
102 |
return; |
|
103 |
} |
|
104 |
||
105 |
view = new this.SearchView({ |
|
106 |
collection: self.collection, |
|
107 |
parent: this |
|
108 |
}); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
self.SearchView = view; |
5 | 110 |
|
111 |
// Render and append after screen title |
|
112 |
view.render(); |
|
113 |
this.searchContainer |
|
114 |
.append( $.parseHTML( '<label class="screen-reader-text" for="wp-filter-search-input">' + l10n.search + '</label>' ) ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
115 |
.append( view.el ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
116 |
.on( 'submit', function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
117 |
event.preventDefault(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
}); |
5 | 119 |
}, |
120 |
||
121 |
// Checks when the user gets close to the bottom |
|
122 |
// of the mage and triggers a theme:scroll event |
|
123 |
scroller: function() { |
|
124 |
var self = this, |
|
125 |
bottom, threshold; |
|
126 |
||
127 |
bottom = this.window.scrollTop() + self.window.height(); |
|
128 |
threshold = self.$el.offset().top + self.$el.outerHeight( false ) - self.window.height(); |
|
129 |
threshold = Math.round( threshold * 0.9 ); |
|
130 |
||
131 |
if ( bottom > threshold ) { |
|
132 |
this.trigger( 'theme:scroll' ); |
|
133 |
} |
|
134 |
} |
|
135 |
}); |
|
136 |
||
137 |
// Set up the Collection for our theme data |
|
138 |
// @has 'id' 'name' 'screenshot' 'author' 'authorURI' 'version' 'active' ... |
|
139 |
themes.Collection = Backbone.Collection.extend({ |
|
140 |
||
141 |
model: themes.Model, |
|
142 |
||
143 |
// Search terms |
|
144 |
terms: '', |
|
145 |
||
146 |
// Controls searching on the current theme collection |
|
147 |
// and triggers an update event |
|
148 |
doSearch: function( value ) { |
|
149 |
||
150 |
// Don't do anything if we've already done this search |
|
151 |
// Useful because the Search handler fires multiple times per keystroke |
|
152 |
if ( this.terms === value ) { |
|
153 |
return; |
|
154 |
} |
|
155 |
||
156 |
// Updates terms with the value passed |
|
157 |
this.terms = value; |
|
158 |
||
159 |
// If we have terms, run a search... |
|
160 |
if ( this.terms.length > 0 ) { |
|
161 |
this.search( this.terms ); |
|
162 |
} |
|
163 |
||
164 |
// If search is blank, show all themes |
|
165 |
// Useful for resetting the views when you clean the input |
|
166 |
if ( this.terms === '' ) { |
|
167 |
this.reset( themes.data.themes ); |
|
168 |
$( 'body' ).removeClass( 'no-results' ); |
|
169 |
} |
|
170 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
// Trigger a 'themes:update' event |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
172 |
this.trigger( 'themes:update' ); |
5 | 173 |
}, |
174 |
||
175 |
// Performs a search within the collection |
|
176 |
// @uses RegExp |
|
177 |
search: function( term ) { |
|
178 |
var match, results, haystack, name, description, author; |
|
179 |
||
180 |
// Start with a full collection |
|
181 |
this.reset( themes.data.themes, { silent: true } ); |
|
182 |
||
183 |
// Escape the term string for RegExp meta characters |
|
184 |
term = term.replace( /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' ); |
|
185 |
||
186 |
// Consider spaces as word delimiters and match the whole string |
|
187 |
// so matching terms can be combined |
|
188 |
term = term.replace( / /g, ')(?=.*' ); |
|
189 |
match = new RegExp( '^(?=.*' + term + ').+', 'i' ); |
|
190 |
||
191 |
// Find results |
|
192 |
// _.filter and .test |
|
193 |
results = this.filter( function( data ) { |
|
194 |
name = data.get( 'name' ).replace( /(<([^>]+)>)/ig, '' ); |
|
195 |
description = data.get( 'description' ).replace( /(<([^>]+)>)/ig, '' ); |
|
196 |
author = data.get( 'author' ).replace( /(<([^>]+)>)/ig, '' ); |
|
197 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
198 |
haystack = _.union( [ name, data.get( 'id' ), description, author, data.get( 'tags' ) ] ); |
5 | 199 |
|
200 |
if ( match.test( data.get( 'author' ) ) && term.length > 2 ) { |
|
201 |
data.set( 'displayAuthor', true ); |
|
202 |
} |
|
203 |
||
204 |
return match.test( haystack ); |
|
205 |
}); |
|
206 |
||
207 |
if ( results.length === 0 ) { |
|
208 |
this.trigger( 'query:empty' ); |
|
209 |
} else { |
|
210 |
$( 'body' ).removeClass( 'no-results' ); |
|
211 |
} |
|
212 |
||
213 |
this.reset( results ); |
|
214 |
}, |
|
215 |
||
216 |
// Paginates the collection with a helper method |
|
217 |
// that slices the collection |
|
218 |
paginate: function( instance ) { |
|
219 |
var collection = this; |
|
220 |
instance = instance || 0; |
|
221 |
||
222 |
// Themes per instance are set at 20 |
|
223 |
collection = _( collection.rest( 20 * instance ) ); |
|
224 |
collection = _( collection.first( 20 ) ); |
|
225 |
||
226 |
return collection; |
|
227 |
}, |
|
228 |
||
229 |
count: false, |
|
230 |
||
231 |
// Handles requests for more themes |
|
232 |
// and caches results |
|
233 |
// |
|
234 |
// When we are missing a cache object we fire an apiCall() |
|
235 |
// which triggers events of `query:success` or `query:fail` |
|
236 |
query: function( request ) { |
|
237 |
/** |
|
238 |
* @static |
|
239 |
* @type Array |
|
240 |
*/ |
|
241 |
var queries = this.queries, |
|
242 |
self = this, |
|
243 |
query, isPaginated, count; |
|
244 |
||
245 |
// Store current query request args |
|
246 |
// for later use with the event `theme:end` |
|
247 |
this.currentQuery.request = request; |
|
248 |
||
249 |
// Search the query cache for matches. |
|
250 |
query = _.find( queries, function( query ) { |
|
251 |
return _.isEqual( query.request, request ); |
|
252 |
}); |
|
253 |
||
254 |
// If the request matches the stored currentQuery.request |
|
255 |
// it means we have a paginated request. |
|
256 |
isPaginated = _.has( request, 'page' ); |
|
257 |
||
258 |
// Reset the internal api page counter for non paginated queries. |
|
259 |
if ( ! isPaginated ) { |
|
260 |
this.currentQuery.page = 1; |
|
261 |
} |
|
262 |
||
263 |
// Otherwise, send a new API call and add it to the cache. |
|
264 |
if ( ! query && ! isPaginated ) { |
|
265 |
query = this.apiCall( request ).done( function( data ) { |
|
266 |
||
267 |
// Update the collection with the queried data. |
|
268 |
if ( data.themes ) { |
|
269 |
self.reset( data.themes ); |
|
270 |
count = data.info.results; |
|
271 |
// Store the results and the query request |
|
272 |
queries.push( { themes: data.themes, request: request, total: count } ); |
|
273 |
} |
|
274 |
||
275 |
// Trigger a collection refresh event |
|
276 |
// and a `query:success` event with a `count` argument. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
self.trigger( 'themes:update' ); |
5 | 278 |
self.trigger( 'query:success', count ); |
279 |
||
280 |
if ( data.themes && data.themes.length === 0 ) { |
|
281 |
self.trigger( 'query:empty' ); |
|
282 |
} |
|
283 |
||
284 |
}).fail( function() { |
|
285 |
self.trigger( 'query:fail' ); |
|
286 |
}); |
|
287 |
} else { |
|
288 |
// If it's a paginated request we need to fetch more themes... |
|
289 |
if ( isPaginated ) { |
|
290 |
return this.apiCall( request, isPaginated ).done( function( data ) { |
|
291 |
// Add the new themes to the current collection |
|
292 |
// @todo update counter |
|
293 |
self.add( data.themes ); |
|
294 |
self.trigger( 'query:success' ); |
|
295 |
||
296 |
// We are done loading themes for now. |
|
297 |
self.loadingThemes = false; |
|
298 |
||
299 |
}).fail( function() { |
|
300 |
self.trigger( 'query:fail' ); |
|
301 |
}); |
|
302 |
} |
|
303 |
||
304 |
if ( query.themes.length === 0 ) { |
|
305 |
self.trigger( 'query:empty' ); |
|
306 |
} else { |
|
307 |
$( 'body' ).removeClass( 'no-results' ); |
|
308 |
} |
|
309 |
||
310 |
// Only trigger an update event since we already have the themes |
|
311 |
// on our cached object |
|
312 |
if ( _.isNumber( query.total ) ) { |
|
313 |
this.count = query.total; |
|
314 |
} |
|
315 |
||
316 |
this.reset( query.themes ); |
|
317 |
if ( ! query.total ) { |
|
318 |
this.count = this.length; |
|
319 |
} |
|
320 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
this.trigger( 'themes:update' ); |
5 | 322 |
this.trigger( 'query:success', this.count ); |
323 |
} |
|
324 |
}, |
|
325 |
||
326 |
// Local cache array for API queries |
|
327 |
queries: [], |
|
328 |
||
329 |
// Keep track of current query so we can handle pagination |
|
330 |
currentQuery: { |
|
331 |
page: 1, |
|
332 |
request: {} |
|
333 |
}, |
|
334 |
||
335 |
// Send request to api.wordpress.org/themes |
|
336 |
apiCall: function( request, paginated ) { |
|
337 |
return wp.ajax.send( 'query-themes', { |
|
338 |
data: { |
|
339 |
// Request data |
|
340 |
request: _.extend({ |
|
9 | 341 |
per_page: 100 |
5 | 342 |
}, request) |
343 |
}, |
|
344 |
||
345 |
beforeSend: function() { |
|
346 |
if ( ! paginated ) { |
|
347 |
// Spin it |
|
348 |
$( 'body' ).addClass( 'loading-content' ).removeClass( 'no-results' ); |
|
349 |
} |
|
350 |
} |
|
351 |
}); |
|
352 |
}, |
|
353 |
||
354 |
// Static status controller for when we are loading themes. |
|
355 |
loadingThemes: false |
|
356 |
}); |
|
357 |
||
358 |
// This is the view that controls each theme item |
|
359 |
// that will be displayed on the screen |
|
360 |
themes.view.Theme = wp.Backbone.View.extend({ |
|
361 |
||
362 |
// Wrap theme data on a div.theme element |
|
363 |
className: 'theme', |
|
364 |
||
365 |
// Reflects which theme view we have |
|
366 |
// 'grid' (default) or 'detail' |
|
367 |
state: 'grid', |
|
368 |
||
369 |
// The HTML template for each element to be rendered |
|
370 |
html: themes.template( 'theme' ), |
|
371 |
||
372 |
events: { |
|
373 |
'click': themes.isInstall ? 'preview': 'expand', |
|
374 |
'keydown': themes.isInstall ? 'preview': 'expand', |
|
375 |
'touchend': themes.isInstall ? 'preview': 'expand', |
|
376 |
'keyup': 'addFocus', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
'touchmove': 'preventExpand', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
378 |
'click .theme-install': 'installTheme', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
379 |
'click .update-message': 'updateTheme' |
5 | 380 |
}, |
381 |
||
382 |
touchDrag: false, |
|
383 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
initialize: function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
385 |
this.model.on( 'change', this.render, this ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
386 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
|
5 | 388 |
render: function() { |
389 |
var data = this.model.toJSON(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
|
5 | 391 |
// Render themes using the html template |
392 |
this.$el.html( this.html( data ) ).attr({ |
|
393 |
tabindex: 0, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
'aria-describedby' : data.id + '-action ' + data.id + '-name', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
395 |
'data-slug': data.id |
5 | 396 |
}); |
397 |
||
398 |
// Renders active theme styles |
|
399 |
this.activeTheme(); |
|
400 |
||
401 |
if ( this.model.get( 'displayAuthor' ) ) { |
|
402 |
this.$el.addClass( 'display-author' ); |
|
403 |
} |
|
404 |
}, |
|
0 | 405 |
|
5 | 406 |
// Adds a class to the currently active theme |
407 |
// and to the overlay in detailed view mode |
|
408 |
activeTheme: function() { |
|
409 |
if ( this.model.get( 'active' ) ) { |
|
410 |
this.$el.addClass( 'active' ); |
|
411 |
} |
|
412 |
}, |
|
413 |
||
414 |
// Add class of focus to the theme we are focused on. |
|
415 |
addFocus: function() { |
|
416 |
var $themeToFocus = ( $( ':focus' ).hasClass( 'theme' ) ) ? $( ':focus' ) : $(':focus').parents('.theme'); |
|
417 |
||
418 |
$('.theme.focus').removeClass('focus'); |
|
419 |
$themeToFocus.addClass('focus'); |
|
420 |
}, |
|
421 |
||
422 |
// Single theme overlay screen |
|
423 |
// It's shown when clicking a theme |
|
424 |
expand: function( event ) { |
|
425 |
var self = this; |
|
426 |
||
427 |
event = event || window.event; |
|
428 |
||
429 |
// 'enter' and 'space' keys expand the details view when a theme is :focused |
|
430 |
if ( event.type === 'keydown' && ( event.which !== 13 && event.which !== 32 ) ) { |
|
431 |
return; |
|
432 |
} |
|
433 |
||
434 |
// Bail if the user scrolled on a touch device |
|
435 |
if ( this.touchDrag === true ) { |
|
436 |
return this.touchDrag = false; |
|
437 |
} |
|
438 |
||
439 |
// Prevent the modal from showing when the user clicks |
|
440 |
// one of the direct action buttons |
|
441 |
if ( $( event.target ).is( '.theme-actions a' ) ) { |
|
442 |
return; |
|
443 |
} |
|
444 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
// Prevent the modal from showing when the user clicks one of the direct action buttons. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
if ( $( event.target ).is( '.theme-actions a, .update-message, .button-link, .notice-dismiss' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
449 |
|
5 | 450 |
// Set focused theme to current element |
451 |
themes.focusedTheme = this.$el; |
|
452 |
||
453 |
this.trigger( 'theme:expand', self.model.cid ); |
|
454 |
}, |
|
455 |
||
456 |
preventExpand: function() { |
|
457 |
this.touchDrag = true; |
|
458 |
}, |
|
459 |
||
460 |
preview: function( event ) { |
|
461 |
var self = this, |
|
462 |
current, preview; |
|
463 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
464 |
event = event || window.event; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
|
5 | 466 |
// Bail if the user scrolled on a touch device |
467 |
if ( this.touchDrag === true ) { |
|
468 |
return this.touchDrag = false; |
|
469 |
} |
|
470 |
||
471 |
// Allow direct link path to installing a theme. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
472 |
if ( $( event.target ).not( '.install-theme-preview' ).parents( '.theme-actions' ).length ) { |
5 | 473 |
return; |
474 |
} |
|
475 |
||
476 |
// 'enter' and 'space' keys expand the details view when a theme is :focused |
|
477 |
if ( event.type === 'keydown' && ( event.which !== 13 && event.which !== 32 ) ) { |
|
478 |
return; |
|
479 |
} |
|
480 |
||
481 |
// pressing enter while focused on the buttons shouldn't open the preview |
|
482 |
if ( event.type === 'keydown' && event.which !== 13 && $( ':focus' ).hasClass( 'button' ) ) { |
|
483 |
return; |
|
484 |
} |
|
485 |
||
486 |
event.preventDefault(); |
|
487 |
||
488 |
event = event || window.event; |
|
489 |
||
490 |
// Set focus to current theme. |
|
491 |
themes.focusedTheme = this.$el; |
|
492 |
||
493 |
// Construct a new Preview view. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
494 |
themes.preview = preview = new themes.view.Preview({ |
5 | 495 |
model: this.model |
496 |
}); |
|
497 |
||
498 |
// Render the view and append it. |
|
499 |
preview.render(); |
|
500 |
this.setNavButtonsState(); |
|
501 |
||
502 |
// Hide previous/next navigation if there is only one theme |
|
503 |
if ( this.model.collection.length === 1 ) { |
|
504 |
preview.$el.addClass( 'no-navigation' ); |
|
505 |
} else { |
|
506 |
preview.$el.removeClass( 'no-navigation' ); |
|
507 |
} |
|
508 |
||
509 |
// Append preview |
|
510 |
$( 'div.wrap' ).append( preview.el ); |
|
511 |
||
512 |
// Listen to our preview object |
|
513 |
// for `theme:next` and `theme:previous` events. |
|
514 |
this.listenTo( preview, 'theme:next', function() { |
|
515 |
||
516 |
// Keep local track of current theme model. |
|
517 |
current = self.model; |
|
518 |
||
519 |
// If we have ventured away from current model update the current model position. |
|
520 |
if ( ! _.isUndefined( self.current ) ) { |
|
521 |
current = self.current; |
|
522 |
} |
|
523 |
||
524 |
// Get next theme model. |
|
525 |
self.current = self.model.collection.at( self.model.collection.indexOf( current ) + 1 ); |
|
526 |
||
527 |
// If we have no more themes, bail. |
|
528 |
if ( _.isUndefined( self.current ) ) { |
|
529 |
self.options.parent.parent.trigger( 'theme:end' ); |
|
530 |
return self.current = current; |
|
531 |
} |
|
532 |
||
533 |
preview.model = self.current; |
|
534 |
||
535 |
// Render and append. |
|
536 |
preview.render(); |
|
537 |
this.setNavButtonsState(); |
|
538 |
$( '.next-theme' ).focus(); |
|
539 |
}) |
|
540 |
.listenTo( preview, 'theme:previous', function() { |
|
541 |
||
542 |
// Keep track of current theme model. |
|
543 |
current = self.model; |
|
544 |
||
545 |
// Bail early if we are at the beginning of the collection |
|
546 |
if ( self.model.collection.indexOf( self.current ) === 0 ) { |
|
547 |
return; |
|
548 |
} |
|
549 |
||
550 |
// If we have ventured away from current model update the current model position. |
|
551 |
if ( ! _.isUndefined( self.current ) ) { |
|
552 |
current = self.current; |
|
553 |
} |
|
554 |
||
555 |
// Get previous theme model. |
|
556 |
self.current = self.model.collection.at( self.model.collection.indexOf( current ) - 1 ); |
|
557 |
||
558 |
// If we have no more themes, bail. |
|
559 |
if ( _.isUndefined( self.current ) ) { |
|
560 |
return; |
|
561 |
} |
|
562 |
||
563 |
preview.model = self.current; |
|
564 |
||
565 |
// Render and append. |
|
566 |
preview.render(); |
|
567 |
this.setNavButtonsState(); |
|
568 |
$( '.previous-theme' ).focus(); |
|
569 |
}); |
|
570 |
||
571 |
this.listenTo( preview, 'preview:close', function() { |
|
572 |
self.current = self.model; |
|
573 |
}); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
574 |
|
5 | 575 |
}, |
576 |
||
577 |
// Handles .disabled classes for previous/next buttons in theme installer preview |
|
578 |
setNavButtonsState: function() { |
|
579 |
var $themeInstaller = $( '.theme-install-overlay' ), |
|
9 | 580 |
current = _.isUndefined( this.current ) ? this.model : this.current, |
581 |
previousThemeButton = $themeInstaller.find( '.previous-theme' ), |
|
582 |
nextThemeButton = $themeInstaller.find( '.next-theme' ); |
|
5 | 583 |
|
584 |
// Disable previous at the zero position |
|
585 |
if ( 0 === this.model.collection.indexOf( current ) ) { |
|
9 | 586 |
previousThemeButton |
587 |
.addClass( 'disabled' ) |
|
588 |
.prop( 'disabled', true ); |
|
589 |
||
590 |
nextThemeButton.focus(); |
|
5 | 591 |
} |
592 |
||
593 |
// Disable next if the next model is undefined |
|
594 |
if ( _.isUndefined( this.model.collection.at( this.model.collection.indexOf( current ) + 1 ) ) ) { |
|
9 | 595 |
nextThemeButton |
596 |
.addClass( 'disabled' ) |
|
597 |
.prop( 'disabled', true ); |
|
598 |
||
599 |
previousThemeButton.focus(); |
|
5 | 600 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
601 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
602 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
603 |
installTheme: function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
604 |
var _this = this; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
605 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
606 |
event.preventDefault(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
607 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
608 |
wp.updates.maybeRequestFilesystemCredentials( event ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
609 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
610 |
$( document ).on( 'wp-theme-install-success', function( event, response ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
611 |
if ( _this.model.get( 'id' ) === response.slug ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
612 |
_this.model.set( { 'installed': true } ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
613 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
614 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
615 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
616 |
wp.updates.installTheme( { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
617 |
slug: $( event.target ).data( 'slug' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
618 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
619 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
620 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
621 |
updateTheme: function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
622 |
var _this = this; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
623 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
624 |
if ( ! this.model.get( 'hasPackage' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
625 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
626 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
627 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
628 |
event.preventDefault(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
629 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
wp.updates.maybeRequestFilesystemCredentials( event ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
631 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
632 |
$( document ).on( 'wp-theme-update-success', function( event, response ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
633 |
_this.model.off( 'change', _this.render, _this ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
634 |
if ( _this.model.get( 'id' ) === response.slug ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
635 |
_this.model.set( { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
636 |
hasUpdate: false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
637 |
version: response.newVersion |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
638 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
640 |
_this.model.on( 'change', _this.render, _this ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
641 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
642 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
643 |
wp.updates.updateTheme( { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
644 |
slug: $( event.target ).parents( 'div.theme' ).first().data( 'slug' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
645 |
} ); |
5 | 646 |
} |
647 |
}); |
|
648 |
||
649 |
// Theme Details view |
|
650 |
// Set ups a modal overlay with the expanded theme data |
|
651 |
themes.view.Details = wp.Backbone.View.extend({ |
|
652 |
||
653 |
// Wrap theme data on a div.theme element |
|
654 |
className: 'theme-overlay', |
|
655 |
||
656 |
events: { |
|
657 |
'click': 'collapse', |
|
658 |
'click .delete-theme': 'deleteTheme', |
|
659 |
'click .left': 'previousTheme', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
660 |
'click .right': 'nextTheme', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
'click #update-theme': 'updateTheme' |
5 | 662 |
}, |
663 |
||
664 |
// The HTML template for the theme overlay |
|
665 |
html: themes.template( 'theme-single' ), |
|
666 |
||
667 |
render: function() { |
|
668 |
var data = this.model.toJSON(); |
|
669 |
this.$el.html( this.html( data ) ); |
|
670 |
// Renders active theme styles |
|
671 |
this.activeTheme(); |
|
672 |
// Set up navigation events |
|
673 |
this.navigation(); |
|
674 |
// Checks screenshot size |
|
675 |
this.screenshotCheck( this.$el ); |
|
676 |
// Contain "tabbing" inside the overlay |
|
677 |
this.containFocus( this.$el ); |
|
678 |
}, |
|
679 |
||
680 |
// Adds a class to the currently active theme |
|
681 |
// and to the overlay in detailed view mode |
|
682 |
activeTheme: function() { |
|
683 |
// Check the model has the active property |
|
684 |
this.$el.toggleClass( 'active', this.model.get( 'active' ) ); |
|
685 |
}, |
|
686 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
687 |
// Set initial focus and constrain tabbing within the theme browser modal. |
5 | 688 |
containFocus: function( $el ) { |
689 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
690 |
// Set initial focus on the primary action control. |
5 | 691 |
_.delay( function() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
692 |
$( '.theme-overlay' ).focus(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
693 |
}, 100 ); |
5 | 694 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
695 |
// Constrain tabbing within the modal. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
696 |
$el.on( 'keydown.wp-themes', function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
697 |
var $firstFocusable = $el.find( '.theme-header button:not(.disabled)' ).first(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
698 |
$lastFocusable = $el.find( '.theme-actions a:visible' ).last(); |
5 | 699 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
700 |
// Check for the Tab key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
701 |
if ( 9 === event.which ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
702 |
if ( $firstFocusable[0] === event.target && event.shiftKey ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
703 |
$lastFocusable.focus(); |
5 | 704 |
event.preventDefault(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
705 |
} else if ( $lastFocusable[0] === event.target && ! event.shiftKey ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
706 |
$firstFocusable.focus(); |
5 | 707 |
event.preventDefault(); |
708 |
} |
|
709 |
} |
|
710 |
}); |
|
711 |
}, |
|
712 |
||
713 |
// Single theme overlay screen |
|
714 |
// It's shown when clicking a theme |
|
715 |
collapse: function( event ) { |
|
716 |
var self = this, |
|
717 |
scroll; |
|
718 |
||
719 |
event = event || window.event; |
|
720 |
||
721 |
// Prevent collapsing detailed view when there is only one theme available |
|
722 |
if ( themes.data.themes.length === 1 ) { |
|
723 |
return; |
|
724 |
} |
|
725 |
||
726 |
// Detect if the click is inside the overlay |
|
727 |
// and don't close it unless the target was |
|
728 |
// the div.back button |
|
729 |
if ( $( event.target ).is( '.theme-backdrop' ) || $( event.target ).is( '.close' ) || event.keyCode === 27 ) { |
|
730 |
||
731 |
// Add a temporary closing class while overlay fades out |
|
732 |
$( 'body' ).addClass( 'closing-overlay' ); |
|
733 |
||
734 |
// With a quick fade out animation |
|
735 |
this.$el.fadeOut( 130, function() { |
|
736 |
// Clicking outside the modal box closes the overlay |
|
737 |
$( 'body' ).removeClass( 'closing-overlay' ); |
|
738 |
// Handle event cleanup |
|
739 |
self.closeOverlay(); |
|
740 |
||
741 |
// Get scroll position to avoid jumping to the top |
|
742 |
scroll = document.body.scrollTop; |
|
743 |
||
744 |
// Clean the url structure |
|
745 |
themes.router.navigate( themes.router.baseUrl( '' ) ); |
|
746 |
||
747 |
// Restore scroll position |
|
748 |
document.body.scrollTop = scroll; |
|
749 |
||
750 |
// Return focus to the theme div |
|
751 |
if ( themes.focusedTheme ) { |
|
752 |
themes.focusedTheme.focus(); |
|
753 |
} |
|
754 |
}); |
|
755 |
} |
|
756 |
}, |
|
757 |
||
758 |
// Handles .disabled classes for next/previous buttons |
|
759 |
navigation: function() { |
|
760 |
||
761 |
// Disable Left/Right when at the start or end of the collection |
|
762 |
if ( this.model.cid === this.model.collection.at(0).cid ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
763 |
this.$el.find( '.left' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
764 |
.addClass( 'disabled' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
765 |
.prop( 'disabled', true ); |
5 | 766 |
} |
767 |
if ( this.model.cid === this.model.collection.at( this.model.collection.length - 1 ).cid ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
768 |
this.$el.find( '.right' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
769 |
.addClass( 'disabled' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
770 |
.prop( 'disabled', true ); |
5 | 771 |
} |
772 |
}, |
|
773 |
||
774 |
// Performs the actions to effectively close |
|
775 |
// the theme details overlay |
|
776 |
closeOverlay: function() { |
|
777 |
$( 'body' ).removeClass( 'modal-open' ); |
|
778 |
this.remove(); |
|
779 |
this.unbind(); |
|
780 |
this.trigger( 'theme:collapse' ); |
|
781 |
}, |
|
782 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
783 |
updateTheme: function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
784 |
var _this = this; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
785 |
event.preventDefault(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
786 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
787 |
wp.updates.maybeRequestFilesystemCredentials( event ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
788 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
789 |
$( document ).on( 'wp-theme-update-success', function( event, response ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
790 |
if ( _this.model.get( 'id' ) === response.slug ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
791 |
_this.model.set( { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
792 |
hasUpdate: false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
793 |
version: response.newVersion |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
794 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
795 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
796 |
_this.render(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
797 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
798 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
799 |
wp.updates.updateTheme( { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
800 |
slug: $( event.target ).data( 'slug' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
801 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
802 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
803 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
804 |
deleteTheme: function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
805 |
var _this = this, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
806 |
_collection = _this.model.collection, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
807 |
_themes = themes; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
808 |
event.preventDefault(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
809 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
810 |
// Confirmation dialog for deleting a theme. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
811 |
if ( ! window.confirm( wp.themes.data.settings.confirmDelete ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
812 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
813 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
815 |
wp.updates.maybeRequestFilesystemCredentials( event ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
816 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
817 |
$( document ).one( 'wp-theme-delete-success', function( event, response ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
818 |
_this.$el.find( '.close' ).trigger( 'click' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
819 |
$( '[data-slug="' + response.slug + '"]' ).css( { backgroundColor:'#faafaa' } ).fadeOut( 350, function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
820 |
$( this ).remove(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
821 |
_themes.data.themes = _.without( _themes.data.themes, _.findWhere( _themes.data.themes, { id: response.slug } ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
822 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
823 |
$( '.wp-filter-search' ).val( '' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
824 |
_collection.doSearch( '' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
825 |
_collection.remove( _this.model ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
826 |
_collection.trigger( 'themes:update' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
827 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
829 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
830 |
wp.updates.deleteTheme( { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
831 |
slug: this.model.get( 'id' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
832 |
} ); |
5 | 833 |
}, |
834 |
||
835 |
nextTheme: function() { |
|
836 |
var self = this; |
|
837 |
self.trigger( 'theme:next', self.model.cid ); |
|
838 |
return false; |
|
839 |
}, |
|
840 |
||
841 |
previousTheme: function() { |
|
842 |
var self = this; |
|
843 |
self.trigger( 'theme:previous', self.model.cid ); |
|
844 |
return false; |
|
845 |
}, |
|
846 |
||
847 |
// Checks if the theme screenshot is the old 300px width version |
|
848 |
// and adds a corresponding class if it's true |
|
849 |
screenshotCheck: function( el ) { |
|
850 |
var screenshot, image; |
|
851 |
||
852 |
screenshot = el.find( '.screenshot img' ); |
|
853 |
image = new Image(); |
|
854 |
image.src = screenshot.attr( 'src' ); |
|
855 |
||
856 |
// Width check |
|
857 |
if ( image.width && image.width <= 300 ) { |
|
858 |
el.addClass( 'small-screenshot' ); |
|
859 |
} |
|
860 |
} |
|
861 |
}); |
|
862 |
||
863 |
// Theme Preview view |
|
864 |
// Set ups a modal overlay with the expanded theme data |
|
865 |
themes.view.Preview = themes.view.Details.extend({ |
|
866 |
||
867 |
className: 'wp-full-overlay expanded', |
|
868 |
el: '.theme-install-overlay', |
|
869 |
||
870 |
events: { |
|
871 |
'click .close-full-overlay': 'close', |
|
872 |
'click .collapse-sidebar': 'collapse', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
873 |
'click .devices button': 'previewDevice', |
5 | 874 |
'click .previous-theme': 'previousTheme', |
875 |
'click .next-theme': 'nextTheme', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
876 |
'keyup': 'keyEvent', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
877 |
'click .theme-install': 'installTheme' |
5 | 878 |
}, |
879 |
||
880 |
// The HTML template for the theme preview |
|
881 |
html: themes.template( 'theme-preview' ), |
|
882 |
||
883 |
render: function() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
884 |
var self = this, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
885 |
currentPreviewDevice, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
886 |
data = this.model.toJSON(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
887 |
$body = $( document.body ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
888 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
889 |
$body.attr( 'aria-busy', 'true' ); |
5 | 890 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
891 |
this.$el.removeClass( 'iframe-ready' ).html( this.html( data ) ); |
5 | 892 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
893 |
currentPreviewDevice = this.$el.data( 'current-preview-device' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
894 |
if ( currentPreviewDevice ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
895 |
self.tooglePreviewDeviceButtons( currentPreviewDevice ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
896 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
897 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
898 |
themes.router.navigate( themes.router.baseUrl( themes.router.themePath + this.model.get( 'id' ) ), { replace: false } ); |
5 | 899 |
|
900 |
this.$el.fadeIn( 200, function() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
901 |
$body.addClass( 'theme-installer-active full-overlay-active' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
902 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
903 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
904 |
this.$el.find( 'iframe' ).one( 'load', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
905 |
self.iframeLoaded(); |
5 | 906 |
}); |
907 |
}, |
|
908 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
909 |
iframeLoaded: function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
910 |
this.$el.addClass( 'iframe-ready' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
911 |
$( document.body ).attr( 'aria-busy', 'false' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
912 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
913 |
|
5 | 914 |
close: function() { |
915 |
this.$el.fadeOut( 200, function() { |
|
916 |
$( 'body' ).removeClass( 'theme-installer-active full-overlay-active' ); |
|
917 |
||
918 |
// Return focus to the theme div |
|
919 |
if ( themes.focusedTheme ) { |
|
920 |
themes.focusedTheme.focus(); |
|
921 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
922 |
}).removeClass( 'iframe-ready' ); |
5 | 923 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
924 |
// Restore the previous browse tab if available. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
925 |
if ( themes.router.selectedTab ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
926 |
themes.router.navigate( themes.router.baseUrl( '?browse=' + themes.router.selectedTab ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
927 |
themes.router.selectedTab = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
928 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
929 |
themes.router.navigate( themes.router.baseUrl( '' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
930 |
} |
5 | 931 |
this.trigger( 'preview:close' ); |
932 |
this.undelegateEvents(); |
|
933 |
this.unbind(); |
|
934 |
return false; |
|
935 |
}, |
|
936 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
937 |
collapse: function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
938 |
var $button = $( event.currentTarget ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
939 |
if ( 'true' === $button.attr( 'aria-expanded' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
940 |
$button.attr({ 'aria-expanded': 'false', 'aria-label': l10n.expandSidebar }); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
941 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
942 |
$button.attr({ 'aria-expanded': 'true', 'aria-label': l10n.collapseSidebar }); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
943 |
} |
5 | 944 |
|
945 |
this.$el.toggleClass( 'collapsed' ).toggleClass( 'expanded' ); |
|
946 |
return false; |
|
947 |
}, |
|
948 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
949 |
previewDevice: function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
950 |
var device = $( event.currentTarget ).data( 'device' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
951 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
952 |
this.$el |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
953 |
.removeClass( 'preview-desktop preview-tablet preview-mobile' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
954 |
.addClass( 'preview-' + device ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
955 |
.data( 'current-preview-device', device ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
956 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
957 |
this.tooglePreviewDeviceButtons( device ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
958 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
959 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
960 |
tooglePreviewDeviceButtons: function( newDevice ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
961 |
var $devices = $( '.wp-full-overlay-footer .devices' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
962 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
963 |
$devices.find( 'button' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
964 |
.removeClass( 'active' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
965 |
.attr( 'aria-pressed', false ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
966 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
967 |
$devices.find( 'button.preview-' + newDevice ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
968 |
.addClass( 'active' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
969 |
.attr( 'aria-pressed', true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
970 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
971 |
|
5 | 972 |
keyEvent: function( event ) { |
973 |
// The escape key closes the preview |
|
974 |
if ( event.keyCode === 27 ) { |
|
975 |
this.undelegateEvents(); |
|
976 |
this.close(); |
|
977 |
} |
|
978 |
// The right arrow key, next theme |
|
979 |
if ( event.keyCode === 39 ) { |
|
980 |
_.once( this.nextTheme() ); |
|
0 | 981 |
} |
982 |
||
5 | 983 |
// The left arrow key, previous theme |
984 |
if ( event.keyCode === 37 ) { |
|
985 |
this.previousTheme(); |
|
986 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
987 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
988 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
989 |
installTheme: function( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
990 |
var _this = this, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
991 |
$target = $( event.target ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
992 |
event.preventDefault(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
993 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
994 |
if ( $target.hasClass( 'disabled' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
995 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
996 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
997 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
998 |
wp.updates.maybeRequestFilesystemCredentials( event ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
999 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1000 |
$( document ).on( 'wp-theme-install-success', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1001 |
_this.model.set( { 'installed': true } ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1002 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1003 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1004 |
wp.updates.installTheme( { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1005 |
slug: $target.data( 'slug' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1006 |
} ); |
5 | 1007 |
} |
1008 |
}); |
|
1009 |
||
1010 |
// Controls the rendering of div.themes, |
|
1011 |
// a wrapper that will hold all the theme elements |
|
1012 |
themes.view.Themes = wp.Backbone.View.extend({ |
|
1013 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1014 |
className: 'themes wp-clearfix', |
5 | 1015 |
$overlay: $( 'div.theme-overlay' ), |
1016 |
||
1017 |
// Number to keep track of scroll position |
|
1018 |
// while in theme-overlay mode |
|
1019 |
index: 0, |
|
1020 |
||
1021 |
// The theme count element |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1022 |
count: $( '.wrap .theme-count' ), |
5 | 1023 |
|
1024 |
// The live themes count |
|
1025 |
liveThemeCount: 0, |
|
1026 |
||
1027 |
initialize: function( options ) { |
|
1028 |
var self = this; |
|
1029 |
||
1030 |
// Set up parent |
|
1031 |
this.parent = options.parent; |
|
1032 |
||
1033 |
// Set current view to [grid] |
|
1034 |
this.setView( 'grid' ); |
|
1035 |
||
1036 |
// Move the active theme to the beginning of the collection |
|
1037 |
self.currentTheme(); |
|
1038 |
||
1039 |
// When the collection is updated by user input... |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1040 |
this.listenTo( self.collection, 'themes:update', function() { |
5 | 1041 |
self.parent.page = 0; |
1042 |
self.currentTheme(); |
|
1043 |
self.render( this ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1044 |
} ); |
5 | 1045 |
|
1046 |
// Update theme count to full result set when available. |
|
1047 |
this.listenTo( self.collection, 'query:success', function( count ) { |
|
1048 |
if ( _.isNumber( count ) ) { |
|
1049 |
self.count.text( count ); |
|
1050 |
self.announceSearchResults( count ); |
|
1051 |
} else { |
|
1052 |
self.count.text( self.collection.length ); |
|
1053 |
self.announceSearchResults( self.collection.length ); |
|
1054 |
} |
|
1055 |
}); |
|
1056 |
||
1057 |
this.listenTo( self.collection, 'query:empty', function() { |
|
1058 |
$( 'body' ).addClass( 'no-results' ); |
|
1059 |
}); |
|
1060 |
||
1061 |
this.listenTo( this.parent, 'theme:scroll', function() { |
|
1062 |
self.renderThemes( self.parent.page ); |
|
1063 |
}); |
|
1064 |
||
1065 |
this.listenTo( this.parent, 'theme:close', function() { |
|
1066 |
if ( self.overlay ) { |
|
1067 |
self.overlay.closeOverlay(); |
|
1068 |
} |
|
1069 |
} ); |
|
1070 |
||
1071 |
// Bind keyboard events. |
|
1072 |
$( 'body' ).on( 'keyup', function( event ) { |
|
1073 |
if ( ! self.overlay ) { |
|
1074 |
return; |
|
1075 |
} |
|
1076 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1077 |
// Bail if the filesystem credentials dialog is shown. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1078 |
if ( $( '#request-filesystem-credentials-dialog' ).is( ':visible' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1079 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1080 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1081 |
|
5 | 1082 |
// Pressing the right arrow key fires a theme:next event |
1083 |
if ( event.keyCode === 39 ) { |
|
1084 |
self.overlay.nextTheme(); |
|
1085 |
} |
|
1086 |
||
1087 |
// Pressing the left arrow key fires a theme:previous event |
|
1088 |
if ( event.keyCode === 37 ) { |
|
1089 |
self.overlay.previousTheme(); |
|
1090 |
} |
|
1091 |
||
1092 |
// Pressing the escape key fires a theme:collapse event |
|
1093 |
if ( event.keyCode === 27 ) { |
|
1094 |
self.overlay.collapse( event ); |
|
1095 |
} |
|
1096 |
}); |
|
1097 |
}, |
|
1098 |
||
1099 |
// Manages rendering of theme pages |
|
1100 |
// and keeping theme count in sync |
|
1101 |
render: function() { |
|
1102 |
// Clear the DOM, please |
|
1103 |
this.$el.empty(); |
|
1104 |
||
1105 |
// If the user doesn't have switch capabilities |
|
1106 |
// or there is only one theme in the collection |
|
1107 |
// render the detailed view of the active theme |
|
1108 |
if ( themes.data.themes.length === 1 ) { |
|
1109 |
||
1110 |
// Constructs the view |
|
1111 |
this.singleTheme = new themes.view.Details({ |
|
1112 |
model: this.collection.models[0] |
|
1113 |
}); |
|
1114 |
||
1115 |
// Render and apply a 'single-theme' class to our container |
|
1116 |
this.singleTheme.render(); |
|
1117 |
this.$el.addClass( 'single-theme' ); |
|
1118 |
this.$el.append( this.singleTheme.el ); |
|
1119 |
} |
|
1120 |
||
1121 |
// Generate the themes |
|
1122 |
// Using page instance |
|
1123 |
// While checking the collection has items |
|
1124 |
if ( this.options.collection.size() > 0 ) { |
|
1125 |
this.renderThemes( this.parent.page ); |
|
1126 |
} |
|
1127 |
||
1128 |
// Display a live theme count for the collection |
|
1129 |
this.liveThemeCount = this.collection.count ? this.collection.count : this.collection.length; |
|
1130 |
this.count.text( this.liveThemeCount ); |
|
1131 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1132 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1133 |
* In the theme installer the themes count is already announced |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1134 |
* because `announceSearchResults` is called on `query:success`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1135 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1136 |
if ( ! themes.isInstall ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1137 |
this.announceSearchResults( this.liveThemeCount ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1138 |
} |
5 | 1139 |
}, |
1140 |
||
1141 |
// Iterates through each instance of the collection |
|
1142 |
// and renders each theme module |
|
1143 |
renderThemes: function( page ) { |
|
1144 |
var self = this; |
|
1145 |
||
1146 |
self.instance = self.collection.paginate( page ); |
|
1147 |
||
1148 |
// If we have no more themes bail |
|
1149 |
if ( self.instance.size() === 0 ) { |
|
1150 |
// Fire a no-more-themes event. |
|
1151 |
this.parent.trigger( 'theme:end' ); |
|
1152 |
return; |
|
1153 |
} |
|
1154 |
||
1155 |
// Make sure the add-new stays at the end |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
if ( ! themes.isInstall && page >= 1 ) { |
5 | 1157 |
$( '.add-new-theme' ).remove(); |
1158 |
} |
|
1159 |
||
1160 |
// Loop through the themes and setup each theme view |
|
1161 |
self.instance.each( function( theme ) { |
|
1162 |
self.theme = new themes.view.Theme({ |
|
1163 |
model: theme, |
|
1164 |
parent: self |
|
1165 |
}); |
|
1166 |
||
1167 |
// Render the views... |
|
1168 |
self.theme.render(); |
|
1169 |
// and append them to div.themes |
|
1170 |
self.$el.append( self.theme.el ); |
|
1171 |
||
1172 |
// Binds to theme:expand to show the modal box |
|
1173 |
// with the theme details |
|
1174 |
self.listenTo( self.theme, 'theme:expand', self.expand, self ); |
|
1175 |
}); |
|
1176 |
||
1177 |
// 'Add new theme' element shown at the end of the grid |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1178 |
if ( ! themes.isInstall && themes.data.settings.canInstall ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1179 |
this.$el.append( '<div class="theme add-new-theme"><a href="' + themes.data.settings.installURI + '"><div class="theme-screenshot"><span></span></div><h2 class="theme-name">' + l10n.addNew + '</h2></a></div>' ); |
5 | 1180 |
} |
1181 |
||
1182 |
this.parent.page++; |
|
1183 |
}, |
|
1184 |
||
1185 |
// Grabs current theme and puts it at the beginning of the collection |
|
1186 |
currentTheme: function() { |
|
1187 |
var self = this, |
|
1188 |
current; |
|
1189 |
||
1190 |
current = self.collection.findWhere({ active: true }); |
|
1191 |
||
1192 |
// Move the active theme to the beginning of the collection |
|
1193 |
if ( current ) { |
|
1194 |
self.collection.remove( current ); |
|
1195 |
self.collection.add( current, { at:0 } ); |
|
1196 |
} |
|
1197 |
}, |
|
1198 |
||
1199 |
// Sets current view |
|
1200 |
setView: function( view ) { |
|
1201 |
return view; |
|
1202 |
}, |
|
1203 |
||
1204 |
// Renders the overlay with the ThemeDetails view |
|
1205 |
// Uses the current model data |
|
1206 |
expand: function( id ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1207 |
var self = this, $card, $modal; |
5 | 1208 |
|
1209 |
// Set the current theme model |
|
1210 |
this.model = self.collection.get( id ); |
|
1211 |
||
1212 |
// Trigger a route update for the current model |
|
1213 |
themes.router.navigate( themes.router.baseUrl( themes.router.themePath + this.model.id ) ); |
|
1214 |
||
1215 |
// Sets this.view to 'detail' |
|
1216 |
this.setView( 'detail' ); |
|
1217 |
$( 'body' ).addClass( 'modal-open' ); |
|
1218 |
||
1219 |
// Set up the theme details view |
|
1220 |
this.overlay = new themes.view.Details({ |
|
1221 |
model: self.model |
|
1222 |
}); |
|
1223 |
||
1224 |
this.overlay.render(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1225 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1226 |
if ( this.model.get( 'hasUpdate' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1227 |
$card = $( '[data-slug="' + this.model.id + '"]' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1228 |
$modal = $( this.overlay.el ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1229 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1230 |
if ( $card.find( '.updating-message' ).length ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1231 |
$modal.find( '.notice-warning h3' ).remove(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1232 |
$modal.find( '.notice-warning' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1233 |
.removeClass( 'notice-large' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1234 |
.addClass( 'updating-message' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1235 |
.find( 'p' ).text( wp.updates.l10n.updating ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1236 |
} else if ( $card.find( '.notice-error' ).length ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1237 |
$modal.find( '.notice-warning' ).remove(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1238 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1239 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1240 |
|
5 | 1241 |
this.$overlay.html( this.overlay.el ); |
1242 |
||
1243 |
// Bind to theme:next and theme:previous |
|
1244 |
// triggered by the arrow keys |
|
1245 |
// |
|
1246 |
// Keep track of the current model so we |
|
1247 |
// can infer an index position |
|
1248 |
this.listenTo( this.overlay, 'theme:next', function() { |
|
1249 |
// Renders the next theme on the overlay |
|
1250 |
self.next( [ self.model.cid ] ); |
|
1251 |
||
1252 |
}) |
|
1253 |
.listenTo( this.overlay, 'theme:previous', function() { |
|
1254 |
// Renders the previous theme on the overlay |
|
1255 |
self.previous( [ self.model.cid ] ); |
|
1256 |
}); |
|
1257 |
}, |
|
1258 |
||
1259 |
// This method renders the next theme on the overlay modal |
|
1260 |
// based on the current position in the collection |
|
1261 |
// @params [model cid] |
|
1262 |
next: function( args ) { |
|
1263 |
var self = this, |
|
1264 |
model, nextModel; |
|
1265 |
||
1266 |
// Get the current theme |
|
1267 |
model = self.collection.get( args[0] ); |
|
1268 |
// Find the next model within the collection |
|
1269 |
nextModel = self.collection.at( self.collection.indexOf( model ) + 1 ); |
|
1270 |
||
1271 |
// Sanity check which also serves as a boundary test |
|
1272 |
if ( nextModel !== undefined ) { |
|
1273 |
||
1274 |
// We have a new theme... |
|
1275 |
// Close the overlay |
|
1276 |
this.overlay.closeOverlay(); |
|
1277 |
||
1278 |
// Trigger a route update for the current model |
|
1279 |
self.theme.trigger( 'theme:expand', nextModel.cid ); |
|
1280 |
||
1281 |
} |
|
1282 |
}, |
|
1283 |
||
1284 |
// This method renders the previous theme on the overlay modal |
|
1285 |
// based on the current position in the collection |
|
1286 |
// @params [model cid] |
|
1287 |
previous: function( args ) { |
|
1288 |
var self = this, |
|
1289 |
model, previousModel; |
|
1290 |
||
1291 |
// Get the current theme |
|
1292 |
model = self.collection.get( args[0] ); |
|
1293 |
// Find the previous model within the collection |
|
1294 |
previousModel = self.collection.at( self.collection.indexOf( model ) - 1 ); |
|
1295 |
||
1296 |
if ( previousModel !== undefined ) { |
|
1297 |
||
1298 |
// We have a new theme... |
|
1299 |
// Close the overlay |
|
1300 |
this.overlay.closeOverlay(); |
|
1301 |
||
1302 |
// Trigger a route update for the current model |
|
1303 |
self.theme.trigger( 'theme:expand', previousModel.cid ); |
|
1304 |
||
1305 |
} |
|
1306 |
}, |
|
1307 |
||
1308 |
// Dispatch audible search results feedback message |
|
1309 |
announceSearchResults: function( count ) { |
|
1310 |
if ( 0 === count ) { |
|
1311 |
wp.a11y.speak( l10n.noThemesFound ); |
|
1312 |
} else { |
|
1313 |
wp.a11y.speak( l10n.themesFound.replace( '%d', count ) ); |
|
1314 |
} |
|
1315 |
} |
|
1316 |
}); |
|
1317 |
||
1318 |
// Search input view controller. |
|
1319 |
themes.view.Search = wp.Backbone.View.extend({ |
|
1320 |
||
1321 |
tagName: 'input', |
|
1322 |
className: 'wp-filter-search', |
|
1323 |
id: 'wp-filter-search-input', |
|
1324 |
searching: false, |
|
1325 |
||
1326 |
attributes: { |
|
1327 |
placeholder: l10n.searchPlaceholder, |
|
1328 |
type: 'search', |
|
1329 |
'aria-describedby': 'live-search-desc' |
|
1330 |
}, |
|
1331 |
||
1332 |
events: { |
|
1333 |
'input': 'search', |
|
1334 |
'keyup': 'search', |
|
1335 |
'blur': 'pushState' |
|
1336 |
}, |
|
1337 |
||
1338 |
initialize: function( options ) { |
|
1339 |
||
1340 |
this.parent = options.parent; |
|
1341 |
||
1342 |
this.listenTo( this.parent, 'theme:close', function() { |
|
1343 |
this.searching = false; |
|
1344 |
} ); |
|
1345 |
||
1346 |
}, |
|
1347 |
||
1348 |
search: function( event ) { |
|
1349 |
// Clear on escape. |
|
1350 |
if ( event.type === 'keyup' && event.which === 27 ) { |
|
1351 |
event.target.value = ''; |
|
1352 |
} |
|
1353 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1354 |
// Since doSearch is debounced, it will only run when user input comes to a rest. |
5 | 1355 |
this.doSearch( event ); |
1356 |
}, |
|
1357 |
||
1358 |
// Runs a search on the theme collection. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1359 |
doSearch: function( event ) { |
5 | 1360 |
var options = {}; |
1361 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1362 |
this.collection.doSearch( event.target.value.replace( /\+/g, ' ' ) ); |
5 | 1363 |
|
1364 |
// if search is initiated and key is not return |
|
1365 |
if ( this.searching && event.which !== 13 ) { |
|
1366 |
options.replace = true; |
|
1367 |
} else { |
|
1368 |
this.searching = true; |
|
1369 |
} |
|
1370 |
||
1371 |
// Update the URL hash |
|
1372 |
if ( event.target.value ) { |
|
1373 |
themes.router.navigate( themes.router.baseUrl( themes.router.searchPath + event.target.value ), options ); |
|
1374 |
} else { |
|
1375 |
themes.router.navigate( themes.router.baseUrl( '' ) ); |
|
1376 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1377 |
}, |
5 | 1378 |
|
1379 |
pushState: function( event ) { |
|
1380 |
var url = themes.router.baseUrl( '' ); |
|
1381 |
||
1382 |
if ( event.target.value ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1383 |
url = themes.router.baseUrl( themes.router.searchPath + encodeURIComponent( event.target.value ) ); |
5 | 1384 |
} |
1385 |
||
1386 |
this.searching = false; |
|
1387 |
themes.router.navigate( url ); |
|
1388 |
||
1389 |
} |
|
1390 |
}); |
|
1391 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1392 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1393 |
* Navigate router. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
* @param {string} url - URL to navigate to. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1398 |
* @param {object} state - State. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1399 |
* @returns {void} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1400 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1401 |
function navigateRouter( url, state ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1402 |
var router = this; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1403 |
if ( Backbone.history._hasPushState ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1404 |
Backbone.Router.prototype.navigate.call( router, url, state ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1405 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1406 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1407 |
|
5 | 1408 |
// Sets up the routes events for relevant url queries |
1409 |
// Listens to [theme] and [search] params |
|
1410 |
themes.Router = Backbone.Router.extend({ |
|
1411 |
||
1412 |
routes: { |
|
1413 |
'themes.php?theme=:slug': 'theme', |
|
1414 |
'themes.php?search=:query': 'search', |
|
1415 |
'themes.php?s=:query': 'search', |
|
1416 |
'themes.php': 'themes', |
|
1417 |
'': 'themes' |
|
1418 |
}, |
|
1419 |
||
1420 |
baseUrl: function( url ) { |
|
1421 |
return 'themes.php' + url; |
|
1422 |
}, |
|
1423 |
||
1424 |
themePath: '?theme=', |
|
1425 |
searchPath: '?search=', |
|
1426 |
||
1427 |
search: function( query ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1428 |
$( '.wp-filter-search' ).val( query.replace( /\+/g, ' ' ) ); |
5 | 1429 |
}, |
1430 |
||
1431 |
themes: function() { |
|
1432 |
$( '.wp-filter-search' ).val( '' ); |
|
1433 |
}, |
|
1434 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1435 |
navigate: navigateRouter |
5 | 1436 |
|
1437 |
}); |
|
1438 |
||
1439 |
// Execute and setup the application |
|
1440 |
themes.Run = { |
|
1441 |
init: function() { |
|
1442 |
// Initializes the blog's theme library view |
|
1443 |
// Create a new collection with data |
|
1444 |
this.themes = new themes.Collection( themes.data.themes ); |
|
1445 |
||
1446 |
// Set up the view |
|
1447 |
this.view = new themes.view.Appearance({ |
|
1448 |
collection: this.themes |
|
1449 |
}); |
|
1450 |
||
1451 |
this.render(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1452 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1453 |
// Start debouncing user searches after Backbone.history.start(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1454 |
this.view.SearchView.doSearch = _.debounce( this.view.SearchView.doSearch, 500 ); |
5 | 1455 |
}, |
1456 |
||
1457 |
render: function() { |
|
1458 |
||
1459 |
// Render results |
|
1460 |
this.view.render(); |
|
1461 |
this.routes(); |
|
1462 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1463 |
if ( Backbone.History.started ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1464 |
Backbone.history.stop(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1465 |
} |
5 | 1466 |
Backbone.history.start({ |
1467 |
root: themes.data.settings.adminUrl, |
|
1468 |
pushState: true, |
|
1469 |
hashChange: false |
|
1470 |
}); |
|
1471 |
}, |
|
1472 |
||
1473 |
routes: function() { |
|
1474 |
var self = this; |
|
1475 |
// Bind to our global thx object |
|
1476 |
// so that the object is available to sub-views |
|
1477 |
themes.router = new themes.Router(); |
|
1478 |
||
1479 |
// Handles theme details route event |
|
1480 |
themes.router.on( 'route:theme', function( slug ) { |
|
1481 |
self.view.view.expand( slug ); |
|
1482 |
}); |
|
1483 |
||
1484 |
themes.router.on( 'route:themes', function() { |
|
1485 |
self.themes.doSearch( '' ); |
|
1486 |
self.view.trigger( 'theme:close' ); |
|
1487 |
}); |
|
1488 |
||
1489 |
// Handles search route event |
|
1490 |
themes.router.on( 'route:search', function() { |
|
1491 |
$( '.wp-filter-search' ).trigger( 'keyup' ); |
|
1492 |
}); |
|
1493 |
||
1494 |
this.extraRoutes(); |
|
1495 |
}, |
|
1496 |
||
1497 |
extraRoutes: function() { |
|
1498 |
return false; |
|
1499 |
} |
|
1500 |
}; |
|
1501 |
||
1502 |
// Extend the main Search view |
|
1503 |
themes.view.InstallerSearch = themes.view.Search.extend({ |
|
1504 |
||
1505 |
events: { |
|
1506 |
'input': 'search', |
|
1507 |
'keyup': 'search' |
|
1508 |
}, |
|
1509 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1510 |
terms: '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1511 |
|
5 | 1512 |
// Handles Ajax request for searching through themes in public repo |
1513 |
search: function( event ) { |
|
1514 |
||
1515 |
// Tabbing or reverse tabbing into the search input shouldn't trigger a search |
|
1516 |
if ( event.type === 'keyup' && ( event.which === 9 || event.which === 16 ) ) { |
|
1517 |
return; |
|
1518 |
} |
|
1519 |
||
1520 |
this.collection = this.options.parent.view.collection; |
|
1521 |
||
1522 |
// Clear on escape. |
|
1523 |
if ( event.type === 'keyup' && event.which === 27 ) { |
|
1524 |
event.target.value = ''; |
|
1525 |
} |
|
1526 |
||
1527 |
this.doSearch( event.target.value ); |
|
1528 |
}, |
|
1529 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1530 |
doSearch: function( value ) { |
5 | 1531 |
var request = {}; |
1532 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1533 |
// Don't do anything if the search terms haven't changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1534 |
if ( this.terms === value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1535 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1536 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1537 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1538 |
// Updates terms with the value passed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1539 |
this.terms = value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1540 |
|
5 | 1541 |
request.search = value; |
1542 |
||
1543 |
// Intercept an [author] search. |
|
1544 |
// |
|
1545 |
// If input value starts with `author:` send a request |
|
1546 |
// for `author` instead of a regular `search` |
|
1547 |
if ( value.substring( 0, 7 ) === 'author:' ) { |
|
1548 |
request.search = ''; |
|
1549 |
request.author = value.slice( 7 ); |
|
1550 |
} |
|
1551 |
||
1552 |
// Intercept a [tag] search. |
|
1553 |
// |
|
1554 |
// If input value starts with `tag:` send a request |
|
1555 |
// for `tag` instead of a regular `search` |
|
1556 |
if ( value.substring( 0, 4 ) === 'tag:' ) { |
|
1557 |
request.search = ''; |
|
1558 |
request.tag = [ value.slice( 4 ) ]; |
|
1559 |
} |
|
1560 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1561 |
$( '.filter-links li > a.current' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1562 |
.removeClass( 'current' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1563 |
.removeAttr( 'aria-current' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1564 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1565 |
$( 'body' ).removeClass( 'show-filters filters-applied show-favorites-form' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1566 |
$( '.drawer-toggle' ).attr( 'aria-expanded', 'false' ); |
5 | 1567 |
|
1568 |
// Get the themes by sending Ajax POST request to api.wordpress.org/themes |
|
1569 |
// or searching the local cache |
|
1570 |
this.collection.query( request ); |
|
1571 |
||
1572 |
// Set route |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1573 |
themes.router.navigate( themes.router.baseUrl( themes.router.searchPath + encodeURIComponent( value ) ), { replace: true } ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1574 |
} |
5 | 1575 |
}); |
1576 |
||
1577 |
themes.view.Installer = themes.view.Appearance.extend({ |
|
1578 |
||
1579 |
el: '#wpbody-content .wrap', |
|
1580 |
||
1581 |
// Register events for sorting and filters in theme-navigation |
|
1582 |
events: { |
|
1583 |
'click .filter-links li > a': 'onSort', |
|
1584 |
'click .theme-filter': 'onFilter', |
|
1585 |
'click .drawer-toggle': 'moreFilters', |
|
1586 |
'click .filter-drawer .apply-filters': 'applyFilters', |
|
1587 |
'click .filter-group [type="checkbox"]': 'addFilter', |
|
1588 |
'click .filter-drawer .clear-filters': 'clearFilters', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1589 |
'click .edit-filters': 'backToFilters', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1590 |
'click .favorites-form-submit' : 'saveUsername', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1591 |
'keyup #wporg-username-input': 'saveUsername' |
5 | 1592 |
}, |
1593 |
||
1594 |
// Initial render method |
|
1595 |
render: function() { |
|
1596 |
var self = this; |
|
1597 |
||
1598 |
this.search(); |
|
1599 |
this.uploader(); |
|
1600 |
||
1601 |
this.collection = new themes.Collection(); |
|
1602 |
||
1603 |
// Bump `collection.currentQuery.page` and request more themes if we hit the end of the page. |
|
1604 |
this.listenTo( this, 'theme:end', function() { |
|
1605 |
||
1606 |
// Make sure we are not already loading |
|
1607 |
if ( self.collection.loadingThemes ) { |
|
1608 |
return; |
|
1609 |
} |
|
1610 |
||
1611 |
// Set loadingThemes to true and bump page instance of currentQuery. |
|
1612 |
self.collection.loadingThemes = true; |
|
1613 |
self.collection.currentQuery.page++; |
|
1614 |
||
1615 |
// Use currentQuery.page to build the themes request. |
|
1616 |
_.extend( self.collection.currentQuery.request, { page: self.collection.currentQuery.page } ); |
|
1617 |
self.collection.query( self.collection.currentQuery.request ); |
|
1618 |
}); |
|
1619 |
||
1620 |
this.listenTo( this.collection, 'query:success', function() { |
|
1621 |
$( 'body' ).removeClass( 'loading-content' ); |
|
1622 |
$( '.theme-browser' ).find( 'div.error' ).remove(); |
|
1623 |
}); |
|
1624 |
||
1625 |
this.listenTo( this.collection, 'query:fail', function() { |
|
1626 |
$( 'body' ).removeClass( 'loading-content' ); |
|
1627 |
$( '.theme-browser' ).find( 'div.error' ).remove(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1628 |
$( '.theme-browser' ).find( 'div.themes' ).before( '<div class="error"><p>' + l10n.error + '</p><p><button class="button try-again">' + l10n.tryAgain + '</button></p></div>' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1629 |
$( '.theme-browser .error .try-again' ).on( 'click', function( e ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1630 |
e.preventDefault(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1631 |
$( 'input.wp-filter-search' ).trigger( 'input' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1632 |
} ); |
5 | 1633 |
}); |
1634 |
||
1635 |
if ( this.view ) { |
|
1636 |
this.view.remove(); |
|
1637 |
} |
|
1638 |
||
1639 |
// Set ups the view and passes the section argument |
|
1640 |
this.view = new themes.view.Themes({ |
|
1641 |
collection: this.collection, |
|
1642 |
parent: this |
|
1643 |
}); |
|
1644 |
||
1645 |
// Reset pagination every time the install view handler is run |
|
1646 |
this.page = 0; |
|
1647 |
||
1648 |
// Render and append |
|
1649 |
this.$el.find( '.themes' ).remove(); |
|
1650 |
this.view.render(); |
|
1651 |
this.$el.find( '.theme-browser' ).append( this.view.el ).addClass( 'rendered' ); |
|
1652 |
}, |
|
1653 |
||
1654 |
// Handles all the rendering of the public theme directory |
|
1655 |
browse: function( section ) { |
|
1656 |
// Create a new collection with the proper theme data |
|
1657 |
// for each section |
|
1658 |
this.collection.query( { browse: section } ); |
|
1659 |
}, |
|
1660 |
||
1661 |
// Sorting navigation |
|
1662 |
onSort: function( event ) { |
|
1663 |
var $el = $( event.target ), |
|
1664 |
sort = $el.data( 'sort' ); |
|
1665 |
||
0 | 1666 |
event.preventDefault(); |
5 | 1667 |
|
1668 |
$( 'body' ).removeClass( 'filters-applied show-filters' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1669 |
$( '.drawer-toggle' ).attr( 'aria-expanded', 'false' ); |
5 | 1670 |
|
1671 |
// Bail if this is already active |
|
1672 |
if ( $el.hasClass( this.activeClass ) ) { |
|
1673 |
return; |
|
1674 |
} |
|
1675 |
||
1676 |
this.sort( sort ); |
|
1677 |
||
1678 |
// Trigger a router.naviagte update |
|
1679 |
themes.router.navigate( themes.router.baseUrl( themes.router.browsePath + sort ) ); |
|
1680 |
}, |
|
1681 |
||
1682 |
sort: function( sort ) { |
|
1683 |
this.clearSearch(); |
|
1684 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1685 |
// Track sorting so we can restore the correct tab when closing preview. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1686 |
themes.router.selectedTab = sort; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1687 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1688 |
$( '.filter-links li > a, .theme-filter' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1689 |
.removeClass( this.activeClass ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1690 |
.removeAttr( 'aria-current' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1691 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1692 |
$( '[data-sort="' + sort + '"]' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1693 |
.addClass( this.activeClass ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1694 |
.attr( 'aria-current', 'page' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1695 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1696 |
if ( 'favorites' === sort ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1697 |
$( 'body' ).addClass( 'show-favorites-form' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1698 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1699 |
$( 'body' ).removeClass( 'show-favorites-form' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1700 |
} |
5 | 1701 |
|
1702 |
this.browse( sort ); |
|
1703 |
}, |
|
1704 |
||
1705 |
// Filters and Tags |
|
1706 |
onFilter: function( event ) { |
|
1707 |
var request, |
|
1708 |
$el = $( event.target ), |
|
1709 |
filter = $el.data( 'filter' ); |
|
1710 |
||
1711 |
// Bail if this is already active |
|
1712 |
if ( $el.hasClass( this.activeClass ) ) { |
|
1713 |
return; |
|
1714 |
} |
|
1715 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1716 |
$( '.filter-links li > a, .theme-section' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1717 |
.removeClass( this.activeClass ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1718 |
.removeAttr( 'aria-current' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1719 |
$el |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1720 |
.addClass( this.activeClass ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1721 |
.attr( 'aria-current', 'page' ); |
5 | 1722 |
|
1723 |
if ( ! filter ) { |
|
1724 |
return; |
|
1725 |
} |
|
1726 |
||
1727 |
// Construct the filter request |
|
1728 |
// using the default values |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1729 |
filter = _.union( [ filter, this.filtersChecked() ] ); |
5 | 1730 |
request = { tag: [ filter ] }; |
1731 |
||
1732 |
// Get the themes by sending Ajax POST request to api.wordpress.org/themes |
|
1733 |
// or searching the local cache |
|
1734 |
this.collection.query( request ); |
|
1735 |
}, |
|
1736 |
||
1737 |
// Clicking on a checkbox to add another filter to the request |
|
1738 |
addFilter: function() { |
|
1739 |
this.filtersChecked(); |
|
1740 |
}, |
|
1741 |
||
1742 |
// Applying filters triggers a tag request |
|
1743 |
applyFilters: function( event ) { |
|
1744 |
var name, |
|
1745 |
tags = this.filtersChecked(), |
|
1746 |
request = { tag: tags }, |
|
1747 |
filteringBy = $( '.filtered-by .tags' ); |
|
1748 |
||
1749 |
if ( event ) { |
|
1750 |
event.preventDefault(); |
|
1751 |
} |
|
1752 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1753 |
if ( ! tags ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1754 |
wp.a11y.speak( l10n.selectFeatureFilter ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1755 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1756 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1757 |
|
5 | 1758 |
$( 'body' ).addClass( 'filters-applied' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1759 |
$( '.filter-links li > a.current' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1760 |
.removeClass( 'current' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1761 |
.removeAttr( 'aria-current' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1762 |
|
5 | 1763 |
filteringBy.empty(); |
1764 |
||
1765 |
_.each( tags, function( tag ) { |
|
1766 |
name = $( 'label[for="filter-id-' + tag + '"]' ).text(); |
|
1767 |
filteringBy.append( '<span class="tag">' + name + '</span>' ); |
|
1768 |
}); |
|
1769 |
||
1770 |
// Get the themes by sending Ajax POST request to api.wordpress.org/themes |
|
1771 |
// or searching the local cache |
|
1772 |
this.collection.query( request ); |
|
1773 |
}, |
|
1774 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1775 |
// Save the user's WordPress.org username and get his favorite themes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1776 |
saveUsername: function ( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1777 |
var username = $( '#wporg-username-input' ).val(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1778 |
nonce = $( '#wporg-username-nonce' ).val(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1779 |
request = { browse: 'favorites', user: username }, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1780 |
that = this; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1781 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1782 |
if ( event ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1783 |
event.preventDefault(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1784 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1785 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1786 |
// save username on enter |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1787 |
if ( event.type === 'keyup' && event.which !== 13 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1788 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1789 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1790 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1791 |
return wp.ajax.send( 'save-wporg-username', { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1792 |
data: { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1793 |
_wpnonce: nonce, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1794 |
username: username |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1795 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1796 |
success: function () { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1797 |
// Get the themes by sending Ajax POST request to api.wordpress.org/themes |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1798 |
// or searching the local cache |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1799 |
that.collection.query( request ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1800 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1801 |
} ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1802 |
}, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1803 |
|
5 | 1804 |
// Get the checked filters |
1805 |
// @return {array} of tags or false |
|
1806 |
filtersChecked: function() { |
|
1807 |
var items = $( '.filter-group' ).find( ':checkbox' ), |
|
1808 |
tags = []; |
|
1809 |
||
1810 |
_.each( items.filter( ':checked' ), function( item ) { |
|
1811 |
tags.push( $( item ).prop( 'value' ) ); |
|
1812 |
}); |
|
1813 |
||
1814 |
// When no filters are checked, restore initial state and return |
|
1815 |
if ( tags.length === 0 ) { |
|
1816 |
$( '.filter-drawer .apply-filters' ).find( 'span' ).text( '' ); |
|
1817 |
$( '.filter-drawer .clear-filters' ).hide(); |
|
1818 |
$( 'body' ).removeClass( 'filters-applied' ); |
|
1819 |
return false; |
|
1820 |
} |
|
1821 |
||
1822 |
$( '.filter-drawer .apply-filters' ).find( 'span' ).text( tags.length ); |
|
1823 |
$( '.filter-drawer .clear-filters' ).css( 'display', 'inline-block' ); |
|
1824 |
||
1825 |
return tags; |
|
1826 |
}, |
|
1827 |
||
1828 |
activeClass: 'current', |
|
1829 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1830 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1831 |
* When users press the "Upload Theme" button, show the upload form in place. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1832 |
*/ |
5 | 1833 |
uploader: function() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1834 |
var uploadViewToggle = $( '.upload-view-toggle' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1835 |
$body = $( document.body ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1836 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1837 |
uploadViewToggle.on( 'click', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1838 |
// Toggle the upload view. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1839 |
$body.toggleClass( 'show-upload-view' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1840 |
// Toggle the `aria-expanded` button attribute. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1841 |
uploadViewToggle.attr( 'aria-expanded', $body.hasClass( 'show-upload-view' ) ); |
5 | 1842 |
}); |
1843 |
}, |
|
1844 |
||
1845 |
// Toggle the full filters navigation |
|
1846 |
moreFilters: function( event ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1847 |
var $body = $( 'body' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1848 |
$toggleButton = $( '.drawer-toggle' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1849 |
|
5 | 1850 |
event.preventDefault(); |
1851 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1852 |
if ( $body.hasClass( 'filters-applied' ) ) { |
5 | 1853 |
return this.backToFilters(); |
1854 |
} |
|
1855 |
||
1856 |
this.clearSearch(); |
|
1857 |
||
1858 |
themes.router.navigate( themes.router.baseUrl( '' ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1859 |
// Toggle the feature filters view. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1860 |
$body.toggleClass( 'show-filters' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1861 |
// Toggle the `aria-expanded` button attribute. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1862 |
$toggleButton.attr( 'aria-expanded', $body.hasClass( 'show-filters' ) ); |
5 | 1863 |
}, |
1864 |
||
1865 |
// Clears all the checked filters |
|
1866 |
// @uses filtersChecked() |
|
1867 |
clearFilters: function( event ) { |
|
1868 |
var items = $( '.filter-group' ).find( ':checkbox' ), |
|
1869 |
self = this; |
|
1870 |
||
1871 |
event.preventDefault(); |
|
1872 |
||
1873 |
_.each( items.filter( ':checked' ), function( item ) { |
|
1874 |
$( item ).prop( 'checked', false ); |
|
1875 |
return self.filtersChecked(); |
|
1876 |
}); |
|
1877 |
}, |
|
1878 |
||
1879 |
backToFilters: function( event ) { |
|
1880 |
if ( event ) { |
|
1881 |
event.preventDefault(); |
|
1882 |
} |
|
1883 |
||
1884 |
$( 'body' ).removeClass( 'filters-applied' ); |
|
1885 |
}, |
|
1886 |
||
1887 |
clearSearch: function() { |
|
1888 |
$( '#wp-filter-search-input').val( '' ); |
|
1889 |
} |
|
1890 |
}); |
|
1891 |
||
1892 |
themes.InstallerRouter = Backbone.Router.extend({ |
|
1893 |
routes: { |
|
1894 |
'theme-install.php?theme=:slug': 'preview', |
|
1895 |
'theme-install.php?browse=:sort': 'sort', |
|
1896 |
'theme-install.php?search=:query': 'search', |
|
1897 |
'theme-install.php': 'sort' |
|
1898 |
}, |
|
1899 |
||
1900 |
baseUrl: function( url ) { |
|
1901 |
return 'theme-install.php' + url; |
|
1902 |
}, |
|
1903 |
||
1904 |
themePath: '?theme=', |
|
1905 |
browsePath: '?browse=', |
|
1906 |
searchPath: '?search=', |
|
1907 |
||
1908 |
search: function( query ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1909 |
$( '.wp-filter-search' ).val( query.replace( /\+/g, ' ' ) ); |
5 | 1910 |
}, |
1911 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1912 |
navigate: navigateRouter |
5 | 1913 |
}); |
1914 |
||
1915 |
||
1916 |
themes.RunInstaller = { |
|
1917 |
||
1918 |
init: function() { |
|
1919 |
// Set up the view |
|
1920 |
// Passes the default 'section' as an option |
|
1921 |
this.view = new themes.view.Installer({ |
|
1922 |
section: 'featured', |
|
1923 |
SearchView: themes.view.InstallerSearch |
|
1924 |
}); |
|
1925 |
||
1926 |
// Render results |
|
1927 |
this.render(); |
|
1928 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1929 |
// Start debouncing user searches after Backbone.history.start(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1930 |
this.view.SearchView.doSearch = _.debounce( this.view.SearchView.doSearch, 500 ); |
5 | 1931 |
}, |
1932 |
||
1933 |
render: function() { |
|
1934 |
||
1935 |
// Render results |
|
1936 |
this.view.render(); |
|
1937 |
this.routes(); |
|
1938 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1939 |
if ( Backbone.History.started ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1940 |
Backbone.history.stop(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1941 |
} |
5 | 1942 |
Backbone.history.start({ |
1943 |
root: themes.data.settings.adminUrl, |
|
1944 |
pushState: true, |
|
1945 |
hashChange: false |
|
1946 |
}); |
|
1947 |
}, |
|
1948 |
||
1949 |
routes: function() { |
|
1950 |
var self = this, |
|
1951 |
request = {}; |
|
1952 |
||
1953 |
// Bind to our global `wp.themes` object |
|
1954 |
// so that the router is available to sub-views |
|
1955 |
themes.router = new themes.InstallerRouter(); |
|
1956 |
||
1957 |
// Handles `theme` route event |
|
1958 |
// Queries the API for the passed theme slug |
|
1959 |
themes.router.on( 'route:preview', function( slug ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1960 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1961 |
// Remove existing handlers. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1962 |
if ( themes.preview ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1963 |
themes.preview.undelegateEvents(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1964 |
themes.preview.unbind(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1965 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1966 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1967 |
// If the theme preview is active, set the current theme. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1968 |
if ( self.view.view.theme && self.view.view.theme.preview ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1969 |
self.view.view.theme.model = self.view.collection.findWhere( { 'slug': slug } ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1970 |
self.view.view.theme.preview(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1971 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1972 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1973 |
// Select the theme by slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1974 |
request.theme = slug; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1975 |
self.view.collection.query( request ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1976 |
self.view.collection.trigger( 'update' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1977 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1978 |
// Open the theme preview. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1979 |
self.view.collection.once( 'query:success', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1980 |
$( 'div[data-slug="' + slug + '"]' ).trigger( 'click' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1981 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1982 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1983 |
} |
5 | 1984 |
}); |
1985 |
||
1986 |
// Handles sorting / browsing routes |
|
1987 |
// Also handles the root URL triggering a sort request |
|
1988 |
// for `featured`, the default view |
|
1989 |
themes.router.on( 'route:sort', function( sort ) { |
|
1990 |
if ( ! sort ) { |
|
1991 |
sort = 'featured'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1992 |
themes.router.navigate( themes.router.baseUrl( '?browse=featured' ), { replace: true } ); |
5 | 1993 |
} |
1994 |
self.view.sort( sort ); |
|
1995 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1996 |
// Close the preview if open. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1997 |
if ( themes.preview ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1998 |
themes.preview.close(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1999 |
} |
5 | 2000 |
}); |
2001 |
||
2002 |
// The `search` route event. The router populates the input field. |
|
2003 |
themes.router.on( 'route:search', function() { |
|
2004 |
$( '.wp-filter-search' ).focus().trigger( 'keyup' ); |
|
2005 |
}); |
|
2006 |
||
2007 |
this.extraRoutes(); |
|
2008 |
}, |
|
2009 |
||
2010 |
extraRoutes: function() { |
|
2011 |
return false; |
|
2012 |
} |
|
2013 |
}; |
|
2014 |
||
2015 |
// Ready... |
|
2016 |
$( document ).ready(function() { |
|
2017 |
if ( themes.isInstall ) { |
|
2018 |
themes.RunInstaller.init(); |
|
2019 |
} else { |
|
2020 |
themes.Run.init(); |
|
2021 |
} |
|
2022 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2023 |
// Update the return param just in time. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2024 |
$( document.body ).on( 'click', '.load-customize', function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2025 |
var link = $( this ), urlParser = document.createElement( 'a' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2026 |
urlParser.href = link.prop( 'href' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2027 |
urlParser.search = $.param( _.extend( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2028 |
wp.customize.utils.parseQueryString( urlParser.search.substr( 1 ) ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2029 |
{ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2030 |
'return': window.location.href |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2031 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2032 |
) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2033 |
link.prop( 'href', urlParser.href ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2034 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2035 |
|
5 | 2036 |
$( '.broken-themes .delete-theme' ).on( 'click', function() { |
2037 |
return confirm( _wpThemeSettings.settings.confirmDelete ); |
|
0 | 2038 |
}); |
2039 |
}); |
|
2040 |
||
5 | 2041 |
})( jQuery ); |
2042 |
||
2043 |
// Align theme browser thickbox |
|
0 | 2044 |
jQuery(document).ready( function($) { |
9 | 2045 |
window.tb_position = function() { |
5 | 2046 |
var tbWindow = $('#TB_window'), |
2047 |
width = $(window).width(), |
|
2048 |
H = $(window).height(), |
|
2049 |
W = ( 1040 < width ) ? 1040 : width, |
|
2050 |
adminbar_height = 0; |
|
0 | 2051 |
|
5 | 2052 |
if ( $('#wpadminbar').length ) { |
2053 |
adminbar_height = parseInt( $('#wpadminbar').css('height'), 10 ); |
|
2054 |
} |
|
0 | 2055 |
|
2056 |
if ( tbWindow.size() ) { |
|
2057 |
tbWindow.width( W - 50 ).height( H - 45 - adminbar_height ); |
|
2058 |
$('#TB_iframeContent').width( W - 50 ).height( H - 75 - adminbar_height ); |
|
2059 |
tbWindow.css({'margin-left': '-' + parseInt( ( ( W - 50 ) / 2 ), 10 ) + 'px'}); |
|
5 | 2060 |
if ( typeof document.body.style.maxWidth !== 'undefined' ) { |
2061 |
tbWindow.css({'top': 20 + adminbar_height + 'px', 'margin-top': '0'}); |
|
2062 |
} |
|
2063 |
} |
|
0 | 2064 |
}; |
2065 |
||
2066 |
$(window).resize(function(){ tb_position(); }); |
|
2067 |
}); |