PageRenderTime 63ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/js/theme.js

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