PageRenderTime 65ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/js/theme.js

https://gitlab.com/savaf/WordPress
JavaScript | 1707 lines | 1022 code | 358 blank | 327 comment | 124 complexity | 390ff3932274834922bdd0aff9b09257 MD5 | raw file
  1. /* global _wpThemeSettings, confirm */
  2. window.wp = window.wp || {};
  3. ( function($) {
  4. // Set up our namespace...
  5. var themes, l10n;
  6. themes = wp.themes = wp.themes || {};
  7. // Store the theme data and settings for organized and quick access
  8. // themes.data.settings, themes.data.themes, themes.data.l10n
  9. themes.data = _wpThemeSettings;
  10. l10n = themes.data.l10n;
  11. // Shortcut for isInstall check
  12. themes.isInstall = !! themes.data.settings.isInstall;
  13. // Setup app structure
  14. _.extend( themes, { model: {}, view: {}, routes: {}, router: {}, template: wp.template });
  15. themes.Model = Backbone.Model.extend({
  16. // Adds attributes to the default data coming through the .org themes api
  17. // Map `id` to `slug` for shared code
  18. initialize: function() {
  19. var description;
  20. // If theme is already installed, set an attribute.
  21. if ( _.indexOf( themes.data.installedThemes, this.get( 'slug' ) ) !== -1 ) {
  22. this.set({ installed: true });
  23. }
  24. // Set the attributes
  25. this.set({
  26. // slug is for installation, id is for existing.
  27. id: this.get( 'slug' ) || this.get( 'id' )
  28. });
  29. // Map `section.description` to `description`
  30. // as the API sometimes returns it differently
  31. if ( this.has( 'sections' ) ) {
  32. description = this.get( 'sections' ).description;
  33. this.set({ description: description });
  34. }
  35. }
  36. });
  37. // Main view controller for themes.php
  38. // Unifies and renders all available views
  39. themes.view.Appearance = wp.Backbone.View.extend({
  40. el: '#wpbody-content .wrap .theme-browser',
  41. window: $( window ),
  42. // Pagination instance
  43. page: 0,
  44. // Sets up a throttler for binding to 'scroll'
  45. initialize: function( options ) {
  46. // Scroller checks how far the scroll position is
  47. _.bindAll( this, 'scroller' );
  48. this.SearchView = options.SearchView ? options.SearchView : themes.view.Search;
  49. // Bind to the scroll event and throttle
  50. // the results from this.scroller
  51. this.window.bind( 'scroll', _.throttle( this.scroller, 300 ) );
  52. },
  53. // Main render control
  54. render: function() {
  55. // Setup the main theme view
  56. // with the current theme collection
  57. this.view = new themes.view.Themes({
  58. collection: this.collection,
  59. parent: this
  60. });
  61. // Render search form.
  62. this.search();
  63. // Render and append
  64. this.view.render();
  65. this.$el.find( '.themes' ).remove();
  66. this.$el.append( this.view.el ).addClass( 'rendered' );
  67. this.$el.append( '<br class="clear"/>' );
  68. },
  69. // Defines search element container
  70. searchContainer: $( '#wpbody h2:first' ),
  71. // Search input and view
  72. // for current theme collection
  73. search: function() {
  74. var view,
  75. self = this;
  76. // Don't render the search if there is only one theme
  77. if ( themes.data.themes.length === 1 ) {
  78. return;
  79. }
  80. view = new this.SearchView({
  81. collection: self.collection,
  82. parent: this
  83. });
  84. // Render and append after screen title
  85. view.render();
  86. this.searchContainer
  87. .append( $.parseHTML( '<label class="screen-reader-text" for="wp-filter-search-input">' + l10n.search + '</label>' ) )
  88. .append( view.el );
  89. },
  90. // Checks when the user gets close to the bottom
  91. // of the mage and triggers a theme:scroll event
  92. scroller: function() {
  93. var self = this,
  94. bottom, threshold;
  95. bottom = this.window.scrollTop() + self.window.height();
  96. threshold = self.$el.offset().top + self.$el.outerHeight( false ) - self.window.height();
  97. threshold = Math.round( threshold * 0.9 );
  98. if ( bottom > threshold ) {
  99. this.trigger( 'theme:scroll' );
  100. }
  101. }
  102. });
  103. // Set up the Collection for our theme data
  104. // @has 'id' 'name' 'screenshot' 'author' 'authorURI' 'version' 'active' ...
  105. themes.Collection = Backbone.Collection.extend({
  106. model: themes.Model,
  107. // Search terms
  108. terms: '',
  109. // Controls searching on the current theme collection
  110. // and triggers an update event
  111. doSearch: function( value ) {
  112. // Don't do anything if we've already done this search
  113. // Useful because the Search handler fires multiple times per keystroke
  114. if ( this.terms === value ) {
  115. return;
  116. }
  117. // Updates terms with the value passed
  118. this.terms = value;
  119. // If we have terms, run a search...
  120. if ( this.terms.length > 0 ) {
  121. this.search( this.terms );
  122. }
  123. // If search is blank, show all themes
  124. // Useful for resetting the views when you clean the input
  125. if ( this.terms === '' ) {
  126. this.reset( themes.data.themes );
  127. }
  128. // Trigger an 'update' event
  129. this.trigger( 'update' );
  130. },
  131. // Performs a search within the collection
  132. // @uses RegExp
  133. search: function( term ) {
  134. var match, results, haystack, name, description, author;
  135. // Start with a full collection
  136. this.reset( themes.data.themes, { silent: true } );
  137. // Escape the term string for RegExp meta characters
  138. term = term.replace( /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' );
  139. // Consider spaces as word delimiters and match the whole string
  140. // so matching terms can be combined
  141. term = term.replace( / /g, ')(?=.*' );
  142. match = new RegExp( '^(?=.*' + term + ').+', 'i' );
  143. // Find results
  144. // _.filter and .test
  145. results = this.filter( function( data ) {
  146. name = data.get( 'name' ).replace( /(<([^>]+)>)/ig, '' );
  147. description = data.get( 'description' ).replace( /(<([^>]+)>)/ig, '' );
  148. author = data.get( 'author' ).replace( /(<([^>]+)>)/ig, '' );
  149. haystack = _.union( name, data.get( 'id' ), description, author, data.get( 'tags' ) );
  150. if ( match.test( data.get( 'author' ) ) && term.length > 2 ) {
  151. data.set( 'displayAuthor', true );
  152. }
  153. return match.test( haystack );
  154. });
  155. if ( results.length === 0 ) {
  156. this.trigger( 'query:empty' );
  157. } else {
  158. $( 'body' ).removeClass( 'no-results' );
  159. }
  160. this.reset( results );
  161. },
  162. // Paginates the collection with a helper method
  163. // that slices the collection
  164. paginate: function( instance ) {
  165. var collection = this;
  166. instance = instance || 0;
  167. // Themes per instance are set at 20
  168. collection = _( collection.rest( 20 * instance ) );
  169. collection = _( collection.first( 20 ) );
  170. return collection;
  171. },
  172. count: false,
  173. // Handles requests for more themes
  174. // and caches results
  175. //
  176. // When we are missing a cache object we fire an apiCall()
  177. // which triggers events of `query:success` or `query:fail`
  178. query: function( request ) {
  179. /**
  180. * @static
  181. * @type Array
  182. */
  183. var queries = this.queries,
  184. self = this,
  185. query, isPaginated, count;
  186. // Store current query request args
  187. // for later use with the event `theme:end`
  188. this.currentQuery.request = request;
  189. // Search the query cache for matches.
  190. query = _.find( queries, function( query ) {
  191. return _.isEqual( query.request, request );
  192. });
  193. // If the request matches the stored currentQuery.request
  194. // it means we have a paginated request.
  195. isPaginated = _.has( request, 'page' );
  196. // Reset the internal api page counter for non paginated queries.
  197. if ( ! isPaginated ) {
  198. this.currentQuery.page = 1;
  199. }
  200. // Otherwise, send a new API call and add it to the cache.
  201. if ( ! query && ! isPaginated ) {
  202. query = this.apiCall( request ).done( function( data ) {
  203. // Update the collection with the queried data.
  204. if ( data.themes ) {
  205. self.reset( data.themes );
  206. count = data.info.results;
  207. // Store the results and the query request
  208. queries.push( { themes: data.themes, request: request, total: count } );
  209. }
  210. // Trigger a collection refresh event
  211. // and a `query:success` event with a `count` argument.
  212. self.trigger( 'update' );
  213. self.trigger( 'query:success', count );
  214. if ( data.themes && data.themes.length === 0 ) {
  215. self.trigger( 'query:empty' );
  216. }
  217. }).fail( function() {
  218. self.trigger( 'query:fail' );
  219. });
  220. } else {
  221. // If it's a paginated request we need to fetch more themes...
  222. if ( isPaginated ) {
  223. return this.apiCall( request, isPaginated ).done( function( data ) {
  224. // Add the new themes to the current collection
  225. // @todo update counter
  226. self.add( data.themes );
  227. self.trigger( 'query:success' );
  228. // We are done loading themes for now.
  229. self.loadingThemes = false;
  230. }).fail( function() {
  231. self.trigger( 'query:fail' );
  232. });
  233. }
  234. if ( query.themes.length === 0 ) {
  235. self.trigger( 'query:empty' );
  236. } else {
  237. $( 'body' ).removeClass( 'no-results' );
  238. }
  239. // Only trigger an update event since we already have the themes
  240. // on our cached object
  241. if ( _.isNumber( query.total ) ) {
  242. this.count = query.total;
  243. }
  244. this.reset( query.themes );
  245. if ( ! query.total ) {
  246. this.count = this.length;
  247. }
  248. this.trigger( 'update' );
  249. this.trigger( 'query:success', this.count );
  250. }
  251. },
  252. // Local cache array for API queries
  253. queries: [],
  254. // Keep track of current query so we can handle pagination
  255. currentQuery: {
  256. page: 1,
  257. request: {}
  258. },
  259. // Send request to api.wordpress.org/themes
  260. apiCall: function( request, paginated ) {
  261. return wp.ajax.send( 'query-themes', {
  262. data: {
  263. // Request data
  264. request: _.extend({
  265. per_page: 100,
  266. fields: {
  267. description: true,
  268. tested: true,
  269. requires: true,
  270. rating: true,
  271. downloaded: true,
  272. downloadLink: true,
  273. last_updated: true,
  274. homepage: true,
  275. num_ratings: true
  276. }
  277. }, request)
  278. },
  279. beforeSend: function() {
  280. if ( ! paginated ) {
  281. // Spin it
  282. $( 'body' ).addClass( 'loading-content' ).removeClass( 'no-results' );
  283. }
  284. }
  285. });
  286. },
  287. // Static status controller for when we are loading themes.
  288. loadingThemes: false
  289. });
  290. // This is the view that controls each theme item
  291. // that will be displayed on the screen
  292. themes.view.Theme = wp.Backbone.View.extend({
  293. // Wrap theme data on a div.theme element
  294. className: 'theme',
  295. // Reflects which theme view we have
  296. // 'grid' (default) or 'detail'
  297. state: 'grid',
  298. // The HTML template for each element to be rendered
  299. html: themes.template( 'theme' ),
  300. events: {
  301. 'click': themes.isInstall ? 'preview': 'expand',
  302. 'keydown': themes.isInstall ? 'preview': 'expand',
  303. 'touchend': themes.isInstall ? 'preview': 'expand',
  304. 'keyup': 'addFocus',
  305. 'touchmove': 'preventExpand'
  306. },
  307. touchDrag: false,
  308. render: function() {
  309. var data = this.model.toJSON();
  310. // Render themes using the html template
  311. this.$el.html( this.html( data ) ).attr({
  312. tabindex: 0,
  313. 'aria-describedby' : data.id + '-action ' + data.id + '-name'
  314. });
  315. // Renders active theme styles
  316. this.activeTheme();
  317. if ( this.model.get( 'displayAuthor' ) ) {
  318. this.$el.addClass( 'display-author' );
  319. }
  320. if ( this.model.get( 'installed' ) ) {
  321. this.$el.addClass( 'is-installed' );
  322. }
  323. },
  324. // Adds a class to the currently active theme
  325. // and to the overlay in detailed view mode
  326. activeTheme: function() {
  327. if ( this.model.get( 'active' ) ) {
  328. this.$el.addClass( 'active' );
  329. }
  330. },
  331. // Add class of focus to the theme we are focused on.
  332. addFocus: function() {
  333. var $themeToFocus = ( $( ':focus' ).hasClass( 'theme' ) ) ? $( ':focus' ) : $(':focus').parents('.theme');
  334. $('.theme.focus').removeClass('focus');
  335. $themeToFocus.addClass('focus');
  336. },
  337. // Single theme overlay screen
  338. // It's shown when clicking a theme
  339. expand: function( event ) {
  340. var self = this;
  341. event = event || window.event;
  342. // 'enter' and 'space' keys expand the details view when a theme is :focused
  343. if ( event.type === 'keydown' && ( event.which !== 13 && event.which !== 32 ) ) {
  344. return;
  345. }
  346. // Bail if the user scrolled on a touch device
  347. if ( this.touchDrag === true ) {
  348. return this.touchDrag = false;
  349. }
  350. // Prevent the modal from showing when the user clicks
  351. // one of the direct action buttons
  352. if ( $( event.target ).is( '.theme-actions a' ) ) {
  353. return;
  354. }
  355. // Set focused theme to current element
  356. themes.focusedTheme = this.$el;
  357. this.trigger( 'theme:expand', self.model.cid );
  358. },
  359. preventExpand: function() {
  360. this.touchDrag = true;
  361. },
  362. preview: function( event ) {
  363. var self = this,
  364. current, preview;
  365. // Bail if the user scrolled on a touch device
  366. if ( this.touchDrag === true ) {
  367. return this.touchDrag = false;
  368. }
  369. // Allow direct link path to installing a theme.
  370. if ( $( event.target ).hasClass( 'button-primary' ) ) {
  371. return;
  372. }
  373. // 'enter' and 'space' keys expand the details view when a theme is :focused
  374. if ( event.type === 'keydown' && ( event.which !== 13 && event.which !== 32 ) ) {
  375. return;
  376. }
  377. // pressing enter while focused on the buttons shouldn't open the preview
  378. if ( event.type === 'keydown' && event.which !== 13 && $( ':focus' ).hasClass( 'button' ) ) {
  379. return;
  380. }
  381. event.preventDefault();
  382. event = event || window.event;
  383. // Set focus to current theme.
  384. themes.focusedTheme = this.$el;
  385. // Construct a new Preview view.
  386. preview = new themes.view.Preview({
  387. model: this.model
  388. });
  389. // Render the view and append it.
  390. preview.render();
  391. this.setNavButtonsState();
  392. // Hide previous/next navigation if there is only one theme
  393. if ( this.model.collection.length === 1 ) {
  394. preview.$el.addClass( 'no-navigation' );
  395. } else {
  396. preview.$el.removeClass( 'no-navigation' );
  397. }
  398. // Append preview
  399. $( 'div.wrap' ).append( preview.el );
  400. // Listen to our preview object
  401. // for `theme:next` and `theme:previous` events.
  402. this.listenTo( preview, 'theme:next', function() {
  403. // Keep local track of current theme model.
  404. current = self.model;
  405. // If we have ventured away from current model update the current model position.
  406. if ( ! _.isUndefined( self.current ) ) {
  407. current = self.current;
  408. }
  409. // Get next theme model.
  410. self.current = self.model.collection.at( self.model.collection.indexOf( current ) + 1 );
  411. // If we have no more themes, bail.
  412. if ( _.isUndefined( self.current ) ) {
  413. self.options.parent.parent.trigger( 'theme:end' );
  414. return self.current = current;
  415. }
  416. preview.model = self.current;
  417. // Render and append.
  418. preview.render();
  419. this.setNavButtonsState();
  420. $( '.next-theme' ).focus();
  421. })
  422. .listenTo( preview, 'theme:previous', function() {
  423. // Keep track of current theme model.
  424. current = self.model;
  425. // Bail early if we are at the beginning of the collection
  426. if ( self.model.collection.indexOf( self.current ) === 0 ) {
  427. return;
  428. }
  429. // If we have ventured away from current model update the current model position.
  430. if ( ! _.isUndefined( self.current ) ) {
  431. current = self.current;
  432. }
  433. // Get previous theme model.
  434. self.current = self.model.collection.at( self.model.collection.indexOf( current ) - 1 );
  435. // If we have no more themes, bail.
  436. if ( _.isUndefined( self.current ) ) {
  437. return;
  438. }
  439. preview.model = self.current;
  440. // Render and append.
  441. preview.render();
  442. this.setNavButtonsState();
  443. $( '.previous-theme' ).focus();
  444. });
  445. this.listenTo( preview, 'preview:close', function() {
  446. self.current = self.model;
  447. });
  448. },
  449. // Handles .disabled classes for previous/next buttons in theme installer preview
  450. setNavButtonsState: function() {
  451. var $themeInstaller = $( '.theme-install-overlay' ),
  452. current = _.isUndefined( this.current ) ? this.model : this.current;
  453. // Disable previous at the zero position
  454. if ( 0 === this.model.collection.indexOf( current ) ) {
  455. $themeInstaller.find( '.previous-theme' ).addClass( 'disabled' );
  456. }
  457. // Disable next if the next model is undefined
  458. if ( _.isUndefined( this.model.collection.at( this.model.collection.indexOf( current ) + 1 ) ) ) {
  459. $themeInstaller.find( '.next-theme' ).addClass( 'disabled' );
  460. }
  461. }
  462. });
  463. // Theme Details view
  464. // Set ups a modal overlay with the expanded theme data
  465. themes.view.Details = wp.Backbone.View.extend({
  466. // Wrap theme data on a div.theme element
  467. className: 'theme-overlay',
  468. events: {
  469. 'click': 'collapse',
  470. 'click .delete-theme': 'deleteTheme',
  471. 'click .left': 'previousTheme',
  472. 'click .right': 'nextTheme'
  473. },
  474. // The HTML template for the theme overlay
  475. html: themes.template( 'theme-single' ),
  476. render: function() {
  477. var data = this.model.toJSON();
  478. this.$el.html( this.html( data ) );
  479. // Renders active theme styles
  480. this.activeTheme();
  481. // Set up navigation events
  482. this.navigation();
  483. // Checks screenshot size
  484. this.screenshotCheck( this.$el );
  485. // Contain "tabbing" inside the overlay
  486. this.containFocus( this.$el );
  487. },
  488. // Adds a class to the currently active theme
  489. // and to the overlay in detailed view mode
  490. activeTheme: function() {
  491. // Check the model has the active property
  492. this.$el.toggleClass( 'active', this.model.get( 'active' ) );
  493. },
  494. // Keeps :focus within the theme details elements
  495. containFocus: function( $el ) {
  496. var $target;
  497. // Move focus to the primary action
  498. _.delay( function() {
  499. $( '.theme-wrap a.button-primary:visible' ).focus();
  500. }, 500 );
  501. $el.on( 'keydown.wp-themes', function( event ) {
  502. // Tab key
  503. if ( event.which === 9 ) {
  504. $target = $( event.target );
  505. // Keep focus within the overlay by making the last link on theme actions
  506. // switch focus to button.left on tabbing and vice versa
  507. if ( $target.is( 'button.left' ) && event.shiftKey ) {
  508. $el.find( '.theme-actions a:last-child' ).focus();
  509. event.preventDefault();
  510. } else if ( $target.is( '.theme-actions a:last-child' ) ) {
  511. $el.find( 'button.left' ).focus();
  512. event.preventDefault();
  513. }
  514. }
  515. });
  516. },
  517. // Single theme overlay screen
  518. // It's shown when clicking a theme
  519. collapse: function( event ) {
  520. var self = this,
  521. scroll;
  522. event = event || window.event;
  523. // Prevent collapsing detailed view when there is only one theme available
  524. if ( themes.data.themes.length === 1 ) {
  525. return;
  526. }
  527. // Detect if the click is inside the overlay
  528. // and don't close it unless the target was
  529. // the div.back button
  530. if ( $( event.target ).is( '.theme-backdrop' ) || $( event.target ).is( '.close' ) || event.keyCode === 27 ) {
  531. // Add a temporary closing class while overlay fades out
  532. $( 'body' ).addClass( 'closing-overlay' );
  533. // With a quick fade out animation
  534. this.$el.fadeOut( 130, function() {
  535. // Clicking outside the modal box closes the overlay
  536. $( 'body' ).removeClass( 'closing-overlay' );
  537. // Handle event cleanup
  538. self.closeOverlay();
  539. // Get scroll position to avoid jumping to the top
  540. scroll = document.body.scrollTop;
  541. // Clean the url structure
  542. themes.router.navigate( themes.router.baseUrl( '' ) );
  543. // Restore scroll position
  544. document.body.scrollTop = scroll;
  545. // Return focus to the theme div
  546. if ( themes.focusedTheme ) {
  547. themes.focusedTheme.focus();
  548. }
  549. });
  550. }
  551. },
  552. // Handles .disabled classes for next/previous buttons
  553. navigation: function() {
  554. // Disable Left/Right when at the start or end of the collection
  555. if ( this.model.cid === this.model.collection.at(0).cid ) {
  556. this.$el.find( '.left' ).addClass( 'disabled' );
  557. }
  558. if ( this.model.cid === this.model.collection.at( this.model.collection.length - 1 ).cid ) {
  559. this.$el.find( '.right' ).addClass( 'disabled' );
  560. }
  561. },
  562. // Performs the actions to effectively close
  563. // the theme details overlay
  564. closeOverlay: function() {
  565. $( 'body' ).removeClass( 'modal-open' );
  566. this.remove();
  567. this.unbind();
  568. this.trigger( 'theme:collapse' );
  569. },
  570. // Confirmation dialog for deleting a theme
  571. deleteTheme: function() {
  572. return confirm( themes.data.settings.confirmDelete );
  573. },
  574. nextTheme: function() {
  575. var self = this;
  576. self.trigger( 'theme:next', self.model.cid );
  577. return false;
  578. },
  579. previousTheme: function() {
  580. var self = this;
  581. self.trigger( 'theme:previous', self.model.cid );
  582. return false;
  583. },
  584. // Checks if the theme screenshot is the old 300px width version
  585. // and adds a corresponding class if it's true
  586. screenshotCheck: function( el ) {
  587. var screenshot, image;
  588. screenshot = el.find( '.screenshot img' );
  589. image = new Image();
  590. image.src = screenshot.attr( 'src' );
  591. // Width check
  592. if ( image.width && image.width <= 300 ) {
  593. el.addClass( 'small-screenshot' );
  594. }
  595. }
  596. });
  597. // Theme Preview view
  598. // Set ups a modal overlay with the expanded theme data
  599. themes.view.Preview = themes.view.Details.extend({
  600. className: 'wp-full-overlay expanded',
  601. el: '.theme-install-overlay',
  602. events: {
  603. 'click .close-full-overlay': 'close',
  604. 'click .collapse-sidebar': 'collapse',
  605. 'click .previous-theme': 'previousTheme',
  606. 'click .next-theme': 'nextTheme',
  607. 'keyup': 'keyEvent'
  608. },
  609. // The HTML template for the theme preview
  610. html: themes.template( 'theme-preview' ),
  611. render: function() {
  612. var data = this.model.toJSON();
  613. this.$el.html( this.html( data ) );
  614. themes.router.navigate( themes.router.baseUrl( themes.router.themePath + this.model.get( 'id' ) ), { replace: true } );
  615. this.$el.fadeIn( 200, function() {
  616. $( 'body' ).addClass( 'theme-installer-active full-overlay-active' );
  617. $( '.close-full-overlay' ).focus();
  618. });
  619. },
  620. close: function() {
  621. this.$el.fadeOut( 200, function() {
  622. $( 'body' ).removeClass( 'theme-installer-active full-overlay-active' );
  623. // Return focus to the theme div
  624. if ( themes.focusedTheme ) {
  625. themes.focusedTheme.focus();
  626. }
  627. });
  628. themes.router.navigate( themes.router.baseUrl( '' ) );
  629. this.trigger( 'preview:close' );
  630. this.undelegateEvents();
  631. this.unbind();
  632. return false;
  633. },
  634. collapse: function() {
  635. this.$el.toggleClass( 'collapsed' ).toggleClass( 'expanded' );
  636. return false;
  637. },
  638. keyEvent: function( event ) {
  639. // The escape key closes the preview
  640. if ( event.keyCode === 27 ) {
  641. this.undelegateEvents();
  642. this.close();
  643. }
  644. // The right arrow key, next theme
  645. if ( event.keyCode === 39 ) {
  646. _.once( this.nextTheme() );
  647. }
  648. // The left arrow key, previous theme
  649. if ( event.keyCode === 37 ) {
  650. this.previousTheme();
  651. }
  652. }
  653. });
  654. // Controls the rendering of div.themes,
  655. // a wrapper that will hold all the theme elements
  656. themes.view.Themes = wp.Backbone.View.extend({
  657. className: 'themes',
  658. $overlay: $( 'div.theme-overlay' ),
  659. // Number to keep track of scroll position
  660. // while in theme-overlay mode
  661. index: 0,
  662. // The theme count element
  663. count: $( '.wp-core-ui .theme-count' ),
  664. initialize: function( options ) {
  665. var self = this;
  666. // Set up parent
  667. this.parent = options.parent;
  668. // Set current view to [grid]
  669. this.setView( 'grid' );
  670. // Move the active theme to the beginning of the collection
  671. self.currentTheme();
  672. // When the collection is updated by user input...
  673. this.listenTo( self.collection, 'update', function() {
  674. self.parent.page = 0;
  675. self.currentTheme();
  676. self.render( this );
  677. });
  678. // Update theme count to full result set when available.
  679. this.listenTo( self.collection, 'query:success', function( count ) {
  680. if ( _.isNumber( count ) ) {
  681. self.count.text( count );
  682. } else {
  683. self.count.text( self.collection.length );
  684. }
  685. });
  686. this.listenTo( self.collection, 'query:empty', function() {
  687. $( 'body' ).addClass( 'no-results' );
  688. });
  689. this.listenTo( this.parent, 'theme:scroll', function() {
  690. self.renderThemes( self.parent.page );
  691. });
  692. this.listenTo( this.parent, 'theme:close', function() {
  693. if ( self.overlay ) {
  694. self.overlay.closeOverlay();
  695. }
  696. } );
  697. // Bind keyboard events.
  698. $( 'body' ).on( 'keyup', function( event ) {
  699. if ( ! self.overlay ) {
  700. return;
  701. }
  702. // Pressing the right arrow key fires a theme:next event
  703. if ( event.keyCode === 39 ) {
  704. self.overlay.nextTheme();
  705. }
  706. // Pressing the left arrow key fires a theme:previous event
  707. if ( event.keyCode === 37 ) {
  708. self.overlay.previousTheme();
  709. }
  710. // Pressing the escape key fires a theme:collapse event
  711. if ( event.keyCode === 27 ) {
  712. self.overlay.collapse( event );
  713. }
  714. });
  715. },
  716. // Manages rendering of theme pages
  717. // and keeping theme count in sync
  718. render: function() {
  719. // Clear the DOM, please
  720. this.$el.html( '' );
  721. // If the user doesn't have switch capabilities
  722. // or there is only one theme in the collection
  723. // render the detailed view of the active theme
  724. if ( themes.data.themes.length === 1 ) {
  725. // Constructs the view
  726. this.singleTheme = new themes.view.Details({
  727. model: this.collection.models[0]
  728. });
  729. // Render and apply a 'single-theme' class to our container
  730. this.singleTheme.render();
  731. this.$el.addClass( 'single-theme' );
  732. this.$el.append( this.singleTheme.el );
  733. }
  734. // Generate the themes
  735. // Using page instance
  736. // While checking the collection has items
  737. if ( this.options.collection.size() > 0 ) {
  738. this.renderThemes( this.parent.page );
  739. }
  740. // Display a live theme count for the collection
  741. this.count.text( this.collection.count ? this.collection.count : this.collection.length );
  742. },
  743. // Iterates through each instance of the collection
  744. // and renders each theme module
  745. renderThemes: function( page ) {
  746. var self = this;
  747. self.instance = self.collection.paginate( page );
  748. // If we have no more themes bail
  749. if ( self.instance.size() === 0 ) {
  750. // Fire a no-more-themes event.
  751. this.parent.trigger( 'theme:end' );
  752. return;
  753. }
  754. // Make sure the add-new stays at the end
  755. if ( page >= 1 ) {
  756. $( '.add-new-theme' ).remove();
  757. }
  758. // Loop through the themes and setup each theme view
  759. self.instance.each( function( theme ) {
  760. self.theme = new themes.view.Theme({
  761. model: theme,
  762. parent: self
  763. });
  764. // Render the views...
  765. self.theme.render();
  766. // and append them to div.themes
  767. self.$el.append( self.theme.el );
  768. // Binds to theme:expand to show the modal box
  769. // with the theme details
  770. self.listenTo( self.theme, 'theme:expand', self.expand, self );
  771. });
  772. // 'Add new theme' element shown at the end of the grid
  773. if ( themes.data.settings.canInstall ) {
  774. this.$el.append( '<div class="theme add-new-theme"><a href="' + themes.data.settings.installURI + '"><div class="theme-screenshot"><span></span></div><h3 class="theme-name">' + l10n.addNew + '</h3></a></div>' );
  775. }
  776. this.parent.page++;
  777. },
  778. // Grabs current theme and puts it at the beginning of the collection
  779. currentTheme: function() {
  780. var self = this,
  781. current;
  782. current = self.collection.findWhere({ active: true });
  783. // Move the active theme to the beginning of the collection
  784. if ( current ) {
  785. self.collection.remove( current );
  786. self.collection.add( current, { at:0 } );
  787. }
  788. },
  789. // Sets current view
  790. setView: function( view ) {
  791. return view;
  792. },
  793. // Renders the overlay with the ThemeDetails view
  794. // Uses the current model data
  795. expand: function( id ) {
  796. var self = this;
  797. // Set the current theme model
  798. this.model = self.collection.get( id );
  799. // Trigger a route update for the current model
  800. themes.router.navigate( themes.router.baseUrl( themes.router.themePath + this.model.id ) );
  801. // Sets this.view to 'detail'
  802. this.setView( 'detail' );
  803. $( 'body' ).addClass( 'modal-open' );
  804. // Set up the theme details view
  805. this.overlay = new themes.view.Details({
  806. model: self.model
  807. });
  808. this.overlay.render();
  809. this.$overlay.html( this.overlay.el );
  810. // Bind to theme:next and theme:previous
  811. // triggered by the arrow keys
  812. //
  813. // Keep track of the current model so we
  814. // can infer an index position
  815. this.listenTo( this.overlay, 'theme:next', function() {
  816. // Renders the next theme on the overlay
  817. self.next( [ self.model.cid ] );
  818. })
  819. .listenTo( this.overlay, 'theme:previous', function() {
  820. // Renders the previous theme on the overlay
  821. self.previous( [ self.model.cid ] );
  822. });
  823. },
  824. // This method renders the next theme on the overlay modal
  825. // based on the current position in the collection
  826. // @params [model cid]
  827. next: function( args ) {
  828. var self = this,
  829. model, nextModel;
  830. // Get the current theme
  831. model = self.collection.get( args[0] );
  832. // Find the next model within the collection
  833. nextModel = self.collection.at( self.collection.indexOf( model ) + 1 );
  834. // Sanity check which also serves as a boundary test
  835. if ( nextModel !== undefined ) {
  836. // We have a new theme...
  837. // Close the overlay
  838. this.overlay.closeOverlay();
  839. // Trigger a route update for the current model
  840. self.theme.trigger( 'theme:expand', nextModel.cid );
  841. }
  842. },
  843. // This method renders the previous theme on the overlay modal
  844. // based on the current position in the collection
  845. // @params [model cid]
  846. previous: function( args ) {
  847. var self = this,
  848. model, previousModel;
  849. // Get the current theme
  850. model = self.collection.get( args[0] );
  851. // Find the previous model within the collection
  852. previousModel = self.collection.at( self.collection.indexOf( model ) - 1 );
  853. if ( previousModel !== undefined ) {
  854. // We have a new theme...
  855. // Close the overlay
  856. this.overlay.closeOverlay();
  857. // Trigger a route update for the current model
  858. self.theme.trigger( 'theme:expand', previousModel.cid );
  859. }
  860. }
  861. });
  862. // Search input view controller.
  863. themes.view.Search = wp.Backbone.View.extend({
  864. tagName: 'input',
  865. className: 'wp-filter-search',
  866. id: 'wp-filter-search-input',
  867. searching: false,
  868. attributes: {
  869. placeholder: l10n.searchPlaceholder,
  870. type: 'search'
  871. },
  872. events: {
  873. 'input': 'search',
  874. 'keyup': 'search',
  875. 'change': 'search',
  876. 'search': 'search',
  877. 'blur': 'pushState'
  878. },
  879. initialize: function( options ) {
  880. this.parent = options.parent;
  881. this.listenTo( this.parent, 'theme:close', function() {
  882. this.searching = false;
  883. } );
  884. },
  885. // Runs a search on the theme collection.
  886. search: function( event ) {
  887. var options = {};
  888. // Clear on escape.
  889. if ( event.type === 'keyup' && event.which === 27 ) {
  890. event.target.value = '';
  891. }
  892. // Lose input focus when pressing enter
  893. if ( event.which === 13 ) {
  894. this.$el.trigger( 'blur' );
  895. }
  896. this.collection.doSearch( event.target.value );
  897. // if search is initiated and key is not return
  898. if ( this.searching && event.which !== 13 ) {
  899. options.replace = true;
  900. } else {
  901. this.searching = true;
  902. }
  903. // Update the URL hash
  904. if ( event.target.value ) {
  905. themes.router.navigate( themes.router.baseUrl( themes.router.searchPath + event.target.value ), options );
  906. } else {
  907. themes.router.navigate( themes.router.baseUrl( '' ) );
  908. }
  909. },
  910. pushState: function( event ) {
  911. var url = themes.router.baseUrl( '' );
  912. if ( event.target.value ) {
  913. url = themes.router.baseUrl( themes.router.searchPath + event.target.value );
  914. }
  915. this.searching = false;
  916. themes.router.navigate( url );
  917. }
  918. });
  919. // Sets up the routes events for relevant url queries
  920. // Listens to [theme] and [search] params
  921. themes.Router = Backbone.Router.extend({
  922. routes: {
  923. 'themes.php?theme=:slug': 'theme',
  924. 'themes.php?search=:query': 'search',
  925. 'themes.php?s=:query': 'search',
  926. 'themes.php': 'themes',
  927. '': 'themes'
  928. },
  929. baseUrl: function( url ) {
  930. return 'themes.php' + url;
  931. },
  932. themePath: '?theme=',
  933. searchPath: '?search=',
  934. search: function( query ) {
  935. $( '.wp-filter-search' ).val( query );
  936. },
  937. themes: function() {
  938. $( '.wp-filter-search' ).val( '' );
  939. },
  940. navigate: function() {
  941. if ( Backbone.history._hasPushState ) {
  942. Backbone.Router.prototype.navigate.apply( this, arguments );
  943. }
  944. }
  945. });
  946. // Execute and setup the application
  947. themes.Run = {
  948. init: function() {
  949. // Initializes the blog's theme library view
  950. // Create a new collection with data
  951. this.themes = new themes.Collection( themes.data.themes );
  952. // Set up the view
  953. this.view = new themes.view.Appearance({
  954. collection: this.themes
  955. });
  956. this.render();
  957. },
  958. render: function() {
  959. // Render results
  960. this.view.render();
  961. this.routes();
  962. Backbone.history.start({
  963. root: themes.data.settings.adminUrl,
  964. pushState: true,
  965. hashChange: false
  966. });
  967. },
  968. routes: function() {
  969. var self = this;
  970. // Bind to our global thx object
  971. // so that the object is available to sub-views
  972. themes.router = new themes.Router();
  973. // Handles theme details route event
  974. themes.router.on( 'route:theme', function( slug ) {
  975. self.view.view.expand( slug );
  976. });
  977. themes.router.on( 'route:themes', function() {
  978. self.themes.doSearch( '' );
  979. self.view.trigger( 'theme:close' );
  980. });
  981. // Handles search route event
  982. themes.router.on( 'route:search', function() {
  983. $( '.wp-filter-search' ).trigger( 'keyup' );
  984. });
  985. this.extraRoutes();
  986. },
  987. extraRoutes: function() {
  988. return false;
  989. }
  990. };
  991. // Extend the main Search view
  992. themes.view.InstallerSearch = themes.view.Search.extend({
  993. events: {
  994. 'keyup': 'search'
  995. },
  996. // Handles Ajax request for searching through themes in public repo
  997. search: function( event ) {
  998. // Tabbing or reverse tabbing into the search input shouldn't trigger a search
  999. if ( event.type === 'keyup' && ( event.which === 9 || event.which === 16 ) ) {
  1000. return;
  1001. }
  1002. this.collection = this.options.parent.view.collection;
  1003. // Clear on escape.
  1004. if ( event.type === 'keyup' && event.which === 27 ) {
  1005. event.target.value = '';
  1006. }
  1007. _.debounce( _.bind( this.doSearch, this ), 300 )( event.target.value );
  1008. },
  1009. doSearch: _.debounce( function( value ) {
  1010. var request = {};
  1011. request.search = value;
  1012. // Intercept an [author] search.
  1013. //
  1014. // If input value starts with `author:` send a request
  1015. // for `author` instead of a regular `search`
  1016. if ( value.substring( 0, 7 ) === 'author:' ) {
  1017. request.search = '';
  1018. request.author = value.slice( 7 );
  1019. }
  1020. // Intercept a [tag] search.
  1021. //
  1022. // If input value starts with `tag:` send a request
  1023. // for `tag` instead of a regular `search`
  1024. if ( value.substring( 0, 4 ) === 'tag:' ) {
  1025. request.search = '';
  1026. request.tag = [ value.slice( 4 ) ];
  1027. }
  1028. $( '.filter-links li > a.current' ).removeClass( 'current' );
  1029. $( 'body' ).removeClass( 'show-filters filters-applied' );
  1030. // Get the themes by sending Ajax POST request to api.wordpress.org/themes
  1031. // or searching the local cache
  1032. this.collection.query( request );
  1033. // Set route
  1034. themes.router.navigate( themes.router.baseUrl( themes.router.searchPath + value ), { replace: true } );
  1035. }, 300 )
  1036. });
  1037. themes.view.Installer = themes.view.Appearance.extend({
  1038. el: '#wpbody-content .wrap',
  1039. // Register events for sorting and filters in theme-navigation
  1040. events: {
  1041. 'click .filter-links li > a': 'onSort',
  1042. 'click .theme-filter': 'onFilter',
  1043. 'click .drawer-toggle': 'moreFilters',
  1044. 'click .filter-drawer .apply-filters': 'applyFilters',
  1045. 'click .filter-group [type="checkbox"]': 'addFilter',
  1046. 'click .filter-drawer .clear-filters': 'clearFilters',
  1047. 'click .filtered-by': 'backToFilters'
  1048. },
  1049. // Initial render method
  1050. render: function() {
  1051. var self = this;
  1052. this.search();
  1053. this.uploader();
  1054. this.collection = new themes.Collection();
  1055. // Bump `collection.currentQuery.page` and request more themes if we hit the end of the page.
  1056. this.listenTo( this, 'theme:end', function() {
  1057. // Make sure we are not already loading
  1058. if ( self.collection.loadingThemes ) {
  1059. return;
  1060. }
  1061. // Set loadingThemes to true and bump page instance of currentQuery.
  1062. self.collection.loadingThemes = true;
  1063. self.collection.currentQuery.page++;
  1064. // Use currentQuery.page to build the themes request.
  1065. _.extend( self.collection.currentQuery.request, { page: self.collection.currentQuery.page } );
  1066. self.collection.query( self.collection.currentQuery.request );
  1067. });
  1068. this.listenTo( this.collection, 'query:success', function() {
  1069. $( 'body' ).removeClass( 'loading-content' );
  1070. $( '.theme-browser' ).find( 'div.error' ).remove();
  1071. });
  1072. this.listenTo( this.collection, 'query:fail', function() {
  1073. $( 'body' ).removeClass( 'loading-content' );
  1074. $( '.theme-browser' ).find( 'div.error' ).remove();
  1075. $( '.theme-browser' ).find( 'div.themes' ).before( '<div class="error"><p>' + l10n.error + '</p></div>' );
  1076. });
  1077. if ( this.view ) {
  1078. this.view.remove();
  1079. }
  1080. // Set ups the view and passes the section argument
  1081. this.view = new themes.view.Themes({
  1082. collection: this.collection,
  1083. parent: this
  1084. });
  1085. // Reset pagination every time the install view handler is run
  1086. this.page = 0;
  1087. // Render and append
  1088. this.$el.find( '.themes' ).remove();
  1089. this.view.render();
  1090. this.$el.find( '.theme-browser' ).append( this.view.el ).addClass( 'rendered' );
  1091. },
  1092. // Handles all the rendering of the public theme directory
  1093. browse: function( section ) {
  1094. // Create a new collection with the proper theme data
  1095. // for each section
  1096. this.collection.query( { browse: section } );
  1097. },
  1098. // Sorting navigation
  1099. onSort: function( event ) {
  1100. var $el = $( event.target ),
  1101. sort = $el.data( 'sort' );
  1102. event.preventDefault();
  1103. $( 'body' ).removeClass( 'filters-applied show-filters' );
  1104. // Bail if this is already active
  1105. if ( $el.hasClass( this.activeClass ) ) {
  1106. return;
  1107. }
  1108. this.sort( sort );
  1109. // Trigger a router.naviagte update
  1110. themes.router.navigate( themes.router.baseUrl( themes.router.browsePath + sort ) );
  1111. },
  1112. sort: function( sort ) {
  1113. this.clearSearch();
  1114. $( '.filter-links li > a, .theme-filter' ).removeClass( this.activeClass );
  1115. $( '[data-sort="' + sort + '"]' ).addClass( this.activeClass );
  1116. this.browse( sort );
  1117. },
  1118. // Filters and Tags
  1119. onFilter: function( event ) {
  1120. var request,
  1121. $el = $( event.target ),
  1122. filter = $el.data( 'filter' );
  1123. // Bail if this is already active
  1124. if ( $el.hasClass( this.activeClass ) ) {
  1125. return;
  1126. }
  1127. $( '.filter-links li > a, .theme-section' ).removeClass( this.activeClass );
  1128. $el.addClass( this.activeClass );
  1129. if ( ! filter ) {
  1130. return;
  1131. }
  1132. // Construct the filter request
  1133. // using the default values
  1134. filter = _.union( filter, this.filtersChecked() );
  1135. request = { tag: [ filter ] };
  1136. // Get the themes by sending Ajax POST request to api.wordpress.org/themes
  1137. // or searching the local cache
  1138. this.collection.query( request );
  1139. },
  1140. // Clicking on a checkbox to add another filter to the request
  1141. addFilter: function() {
  1142. this.filtersChecked();
  1143. },
  1144. // Applying filters triggers a tag request
  1145. applyFilters: function( event ) {
  1146. var name,
  1147. tags = this.filtersChecked(),
  1148. request = { tag: tags },
  1149. filteringBy = $( '.filtered-by .tags' );
  1150. if ( event ) {
  1151. event.preventDefault();
  1152. }
  1153. $( 'body' ).addClass( 'filters-applied' );
  1154. $( '.filter-links li > a.current' ).removeClass( 'current' );
  1155. filteringBy.empty();
  1156. _.each( tags, function( tag ) {
  1157. name = $( 'label[for="filter-id-' + tag + '"]' ).text();
  1158. filteringBy.append( '<span class="tag">' + name + '</span>' );
  1159. });
  1160. // Get the themes by sending Ajax POST request to api.wordpress.org/themes
  1161. // or searching the local cache
  1162. this.collection.query( request );
  1163. },
  1164. // Get the checked filters
  1165. // @return {array} of tags or false
  1166. filtersChecked: function() {
  1167. var items = $( '.filter-group' ).find( ':checkbox' ),
  1168. tags = [];
  1169. _.each( items.filter( ':checked' ), function( item ) {
  1170. tags.push( $( item ).prop( 'value' ) );
  1171. });
  1172. // When no filters are checked, restore initial state and return
  1173. if ( tags.length === 0 ) {
  1174. $( '.filter-drawer .apply-filters' ).find( 'span' ).text( '' );
  1175. $( '.filter-drawer .clear-filters' ).hide();
  1176. $( 'body' ).removeClass( 'filters-applied' );
  1177. return false;
  1178. }
  1179. $( '.filter-drawer .apply-filters' ).find( 'span' ).text( tags.length );
  1180. $( '.filter-drawer .clear-filters' ).css( 'display', 'inline-block' );
  1181. return tags;
  1182. },
  1183. activeClass: 'current',
  1184. // Overwrite search container class to append search
  1185. // in new location
  1186. searchContainer: $( '.wp-filter .search-form' ),
  1187. uploader: function() {
  1188. $( 'a.upload' ).on( 'click', function( event ) {
  1189. event.preventDefault();
  1190. $( 'body' ).addClass( 'show-upload-theme' );
  1191. themes.router.navigate( themes.router.baseUrl( '?upload' ), { replace: true } );
  1192. });
  1193. $( 'a.browse-themes' ).on( 'click', function( event ) {
  1194. event.preventDefault();
  1195. $( 'body' ).removeClass( 'show-upload-theme' );
  1196. themes.router.navigate( themes.router.baseUrl( '' ), { replace: true } );
  1197. });
  1198. },
  1199. // Toggle the full filters navigation
  1200. moreFilters: function( event ) {
  1201. event.preventDefault();
  1202. if ( $( 'body' ).hasClass( 'filters-applied' ) ) {
  1203. return this.backToFilters();
  1204. }
  1205. // If the filters section is opened and filters are checked
  1206. // run the relevant query collapsing to filtered-by state
  1207. if ( $( 'body' ).hasClass( 'show-filters' ) && this.filtersChecked() ) {
  1208. return this.addFilter();
  1209. }
  1210. this.clearSearch();
  1211. themes.router.navigate( themes.router.baseUrl( '' ) );
  1212. $( 'body' ).toggleClass( 'show-filters' );
  1213. },
  1214. // Clears all the checked filters
  1215. // @uses filtersChecked()
  1216. clearFilters: function( event ) {
  1217. var items = $( '.filter-group' ).find( ':checkbox' ),
  1218. self = this;
  1219. event.preventDefault();
  1220. _.each( items.filter( ':checked' ), function( item ) {
  1221. $( item ).prop( 'checked', false );
  1222. return self.filtersChecked();
  1223. });
  1224. },
  1225. backToFilters: function( event ) {
  1226. if ( event ) {
  1227. event.preventDefault();
  1228. }
  1229. $( 'body' ).removeClass( 'filters-applied' );
  1230. },
  1231. clearSearch: function() {
  1232. $( '#wp-filter-search-input').val( '' );
  1233. }
  1234. });
  1235. themes.InstallerRouter = Backbone.Router.extend({
  1236. routes: {
  1237. 'theme-install.php?theme=:slug': 'preview',
  1238. 'theme-install.php?browse=:sort': 'sort',
  1239. 'theme-install.php?upload': 'upload',
  1240. 'theme-install.php?search=:query': 'search',
  1241. 'theme-install.php': 'sort'
  1242. },
  1243. baseUrl: function( url ) {
  1244. return 'theme-install.php' + url;
  1245. },
  1246. themePath: '?theme=',
  1247. browsePath: '?browse=',
  1248. searchPath: '?search=',
  1249. search: function( query ) {
  1250. $( '.wp-filter-search' ).val( query );
  1251. },
  1252. navigate: function() {
  1253. if ( Backbone.history._hasPushState ) {
  1254. Backbone.Router.prototype.navigate.apply( this, arguments );
  1255. }
  1256. }
  1257. });
  1258. themes.RunInstaller = {
  1259. init: function() {
  1260. // Set up the view
  1261. // Passes the default 'section' as an option
  1262. this.view = new themes.view.Installer({
  1263. section: 'featured',
  1264. SearchView: themes.view.InstallerSearch
  1265. });
  1266. // Render results
  1267. this.render();
  1268. },
  1269. render: function() {
  1270. // Render results
  1271. this.view.render();
  1272. this.routes();
  1273. Backbone.history.start({
  1274. root: themes.data.settings.adminUrl,
  1275. pushState: true,
  1276. hashChange: false
  1277. });
  1278. },
  1279. routes: function() {
  1280. var self = this,
  1281. request = {};
  1282. // Bind to our global `wp.themes` object
  1283. // so that the router is available to sub-views
  1284. themes.router = new themes.InstallerRouter();
  1285. // Handles `theme` route event
  1286. // Queries the API for the passed theme slug
  1287. themes.router.on( 'route:preview', function( slug ) {
  1288. request.theme = slug;
  1289. self.view.collection.query( request );
  1290. });
  1291. // Handles sorting / browsing routes
  1292. // Also handles the root URL triggering a sort request
  1293. // for `featured`, the default view
  1294. themes.router.on( 'route:sort', function( sort ) {
  1295. if ( ! sort ) {
  1296. sort = 'featured';
  1297. }
  1298. self.view.sort( sort );
  1299. self.view.trigger( 'theme:close' );
  1300. });
  1301. // Support the `upload` route by going straight to upload section
  1302. themes.router.on( 'route:upload', function() {
  1303. $( 'a.upload' ).trigger( 'click' );
  1304. });
  1305. // The `search` route event. The router populates the input field.
  1306. themes.router.on( 'route:search', function() {
  1307. $( '.wp-filter-search' ).focus().trigger( 'keyup' );
  1308. });
  1309. this.extraRoutes();
  1310. },
  1311. extraRoutes: function() {
  1312. return false;
  1313. }
  1314. };
  1315. // Ready...
  1316. $( document ).ready(function() {
  1317. if ( themes.isInstall ) {
  1318. themes.RunInstaller.init();
  1319. } else {
  1320. themes.Run.init();
  1321. }
  1322. $( '.broken-themes .delete-theme' ).on( 'click', function() {
  1323. return confirm( _wpThemeSettings.settings.confirmDelete );
  1324. });
  1325. });
  1326. })( jQuery );
  1327. // Align theme browser thickbox
  1328. var tb_position;
  1329. jQuery(document).ready( function($) {
  1330. tb_position = function() {
  1331. var tbWindow = $('#TB_window'),
  1332. width = $(window).width(),
  1333. H = $(window).height(),
  1334. W = ( 1040 < width ) ? 1040 : width,
  1335. adminbar_height = 0;
  1336. if ( $('#wpadminbar').length ) {
  1337. adminbar_height = parseInt( $('#wpadminbar').css('height'), 10 );
  1338. }
  1339. if ( tbWindow.size() ) {
  1340. tbWindow.width( W - 50 ).height( H - 45 - adminbar_height );
  1341. $('#TB_iframeContent').width( W - 50 ).height( H - 75 - adminbar_height );
  1342. tbWindow.css({'margin-left': '-' + parseInt( ( ( W - 50 ) / 2 ), 10 ) + 'px'});
  1343. if ( typeof document.body.style.maxWidth !== 'undefined' ) {
  1344. tbWindow.css({'top': 20 + adminbar_height + 'px', 'margin-top': '0'});
  1345. }
  1346. }
  1347. };
  1348. $(window).resize(function(){ tb_position(); });
  1349. });