Make WordPress Core

Changeset 63458


Ignore:
Timestamp:
09/04/2026 12:01:14 AM (2 months ago)
Author:
westonruter
Message:

Customize: Document the header image models and views.

Neither file documented a single method: only the classes themselves carried a docblock. Every method across the two now says what it does, in the third person, along with the arguments of the function wrapping each file.

One behavior is changed: ChoiceListView.render() returned nothing, unlike the sibling views beside it, so it now returns this as the Backbone convention expects. Nothing reads the value, since the view is only ever constructed, never chained, and render is otherwise reached as a listenTo handler, whose return is discarded.

Developed as subset of ​https://github.com/WordPress/wordpress-develop/pull/10743.
Follow-up to r63454, r63455, r63456, r63457.

Props westonruter, afercia, grapplerulrich.
See #39671, #64662, #66033.
Fixes #40831.

Location:
trunk/src/js/_enqueues/wp/customize
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/wp/customize/models.js

    r48168 r63458  
    44
    55/* global _wpCustomizeHeader */
     6
     7/**
     8 * @param {JQueryStatic} $  The jQuery object.
     9 * @param {Object}       wp The WordPress global object.
     10 */
    611(function( $, wp ) {
    712        var api = wp.customize;
    … …  
    2227         * @alias wp.customize.HeaderTool.ImageModel
    2328         *
    24          * @constructor
     29         * @class
    2530         * @augments Backbone.Model
    2631         */
    2732        api.HeaderTool.ImageModel = Backbone.Model.extend(/** @lends wp.customize.HeaderTool.ImageModel.prototype */{
     33                /**
     34                 * Returns the default attributes for a header image.
     35                 *
     36                 * @return {Object} Default attributes.
     37                 */
    2838                defaults: function() {
    2939                        return {
    … …  
    4050                },
    4151
     52                /**
     53                 * Initializes the model, hiding the header when asked to.
     54                 */
    4255                initialize: function() {
    4356                        this.on('hide', this.hide, this);
    4457                },
    4558
     59                /**
     60                 * Hides the header image, removing it from the site.
     61                 */
    4662                hide: function() {
    4763                        this.set('choice', '');
    … …  
    5066                },
    5167
     68                /**
     69                 * Removes the image, hiding the header first if it is the current one.
     70                 */
    5271                destroy: function() {
    5372                        var data = this.get('header'),
    … …  
    7089                },
    7190
     91                /**
     92                 * Makes this image the current header image.
     93                 */
    7294                save: function() {
    7395                        if (this.get('random')) {
    … …  
    87109                },
    88110
     111                /**
     112                 * Adds the image to the theme's uploaded headers.
     113                 */
    89114                importImage: function() {
    90115                        var data = this.get('header');
    … …  
    101126                },
    102127
     128                /**
     129                 * Returns whether the image needs cropping for this theme.
     130                 *
     131                 * @return {boolean} Whether the image should be cropped.
     132                 */
    103133                shouldBeCropped: function() {
    104134                        if (this.get('themeFlexWidth') === true &&
    … …  
    137167         * @alias wp.customize.HeaderTool.ChoiceList
    138168         *
    139          * @constructor
     169         * @class
    140170         * @augments Backbone.Collection
    141171         */
    … …  
    143173                model: api.HeaderTool.ImageModel,
    144174
    145                 // Ordered from most recently used to least.
     175                /**
     176                 * Orders the collection from most recently used to least.
     177                 *
     178                 * @param {Backbone.Model} model The model to sort.
     179                 * @return {number} The sort order.
     180                 */
    146181                comparator: function(model) {
    147182                        return -model.get('header').timestamp;
    148183                },
    149184
     185                /**
     186                 * Initializes the collection from the uploaded header images.
     187                 */
    150188                initialize: function() {
    151189                        var current = api.HeaderTool.currentHeader.get('choice').replace(/^https?:\/\//, ''),
    … …  
    193231                },
    194232
     233                /**
     234                 * Removes an image's previous crop once it has been replaced.
     235                 *
     236                 * @param {Backbone.Model} model Model.
     237                 */
    195238                maybeRemoveOldCrop: function( model ) {
    196239                        var newID = model.get( 'header' ).attachment_id || false,
    … …  
    212255                },
    213256
     257                /**
     258                 * Adds the random choice once a single image is left.
     259                 */
    214260                maybeAddRandomChoice: function() {
    215261                        if (this.size() === 1) {
    … …  
    218264                },
    219265
     266                /**
     267                 * Adds the random image choice to the collection.
     268                 *
     269                 * @param {string} initialChoice Initial choice.
     270                 */
    220271                addRandomChoice: function(initialChoice) {
    221272                        var isRandomSameType = RegExp(this.type).test(initialChoice),
    … …  
    235286                },
    236287
     288                /**
     289                 * Returns whether a choice is one of the random image choices.
     290                 *
     291                 * @param {string} choice Choice.
     292                 * @return {boolean} Whether the choice is random.
     293                 */
    237294                isRandomChoice: function(choice) {
    238295                        return (/^random-(uploaded|default)-image$/).test(choice);
    239296                },
    240297
     298                /**
     299                 * Returns whether the list title should be hidden.
     300                 *
     301                 * @return {boolean} Whether the title should be hidden.
     302                 */
    241303                shouldHideTitle: function() {
    242304                        return this.size() < 2;
    243305                },
    244306
     307                /**
     308                 * Marks a single image as the selected one.
     309                 *
     310                 * @param {Backbone.Model} model Model.
     311                 */
    245312                setImage: function(model) {
    246313                        this.each(function(m) {
    … …  
    253320                },
    254321
     322                /**
     323                 * Clears the selection.
     324                 */
    255325                removeImage: function() {
    256326                        this.each(function(m) {
    … …  
    267337         * @alias wp.customize.HeaderTool.DefaultsList
    268338         *
    269          * @constructor
     339         * @class
    270340         * @augments wp.customize.HeaderTool.ChoiceList
    271341         * @augments Backbone.Collection
    272342         */
    273343        api.HeaderTool.DefaultsList = api.HeaderTool.ChoiceList.extend({
     344                /**
     345                 * Initializes the collection from the theme's default header images.
     346                 */
    274347                initialize: function() {
    275348                        this.type = 'default';
  • trunk/src/js/_enqueues/wp/customize/views.js

    r60645 r63458  
    33 */
    44
     5/**
     6 * @param {JQueryStatic}       $  The jQuery object.
     7 * @param {Object}             wp The WordPress global object.
     8 * @param {_.UnderscoreStatic} _  The Underscore.js object.
     9 */
    510(function( $, wp, _ ) {
    611
    … …  
    1924         * @alias wp.customize.HeaderTool.CurrentView
    2025         *
    21          * @constructor
     26         * @class
    2227         * @augments wp.Backbone.View
    2328         */
    … …  
    2530                template: wp.template('header-current'),
    2631
     32                /**
     33                 * Initializes the view, rendering it whenever the model changes.
     34                 */
    2735                initialize: function() {
    2836                        this.listenTo(this.model, 'change', this.render);
    … …  
    3038                },
    3139
     40                /**
     41                 * Renders the currently selected header image.
     42                 *
     43                 * @return {wp.customize.HeaderTool.CurrentView} Current view.
     44                 */
    3245                render: function() {
    3346                        this.$el.html(this.template(this.model.toJSON()));
    … …  
    3649                },
    3750
     51                /**
     52                 * Shows or hides the header's action buttons.
     53                 */
    3854                setButtons: function() {
    3955                        var elements = $('#customize-control-header_image .actions .remove');
    … …  
    6581         * @alias wp.customize.HeaderTool.ChoiceView
    6682         *
    67          * @constructor
     83         * @class
    6884         * @augments wp.Backbone.View
    6985         */
    … …  
    7894                },
    7995
     96                /**
     97                 * Initializes the view, marking it current when it matches the header image.
     98                 */
    8099                initialize: function() {
    81100                        var properties = [
    … …  
    91110                },
    92111
     112                /**
     113                 * Renders the choice.
     114                 *
     115                 * @return {wp.customize.HeaderTool.ChoiceView} Choice view.
     116                 */
    93117                render: function() {
    94118                        this.$el.html(this.template(this.extendedModel()));
    … …  
    98122                },
    99123
     124                /**
     125                 * Toggles the selected class on the choice.
     126                 */
    100127                toggleSelected: function() {
    101128                        this.$el.toggleClass('selected', this.model.get('selected'));
    102129                },
    103130
     131                /**
     132                 * Returns the model's attributes together with its collection type.
     133                 *
     134                 * @return {Object} Extended model.
     135                 */
    104136                extendedModel: function() {
    105137                        var c = this.model.get('collection');
    … …  
    109141                },
    110142
     143                /**
     144                 * Selects the choice as the header image.
     145                 */
    111146                select: function() {
    112147                        this.preventJump();
    … …  
    115150                },
    116151
     152                /**
     153                 * Keeps the sidebar at its current scroll position.
     154                 */
    117155                preventJump: function() {
    118156                        var container = $('.wp-full-overlay-sidebar-content'),
    … …  
    124162                },
    125163
     164                /**
     165                 * Removes the image from the collection.
     166                 *
     167                 * @param {JQuery.Event} e Event.
     168                 */
    126169                removeImage: function(e) {
    127170                        e.stopPropagation();
    … …  
    143186         * @alias wp.customize.HeaderTool.ChoiceListView
    144187         *
    145          * @constructor
     188         * @class
    146189         * @augments wp.Backbone.View
    147190         */
    148191        api.HeaderTool.ChoiceListView = wp.Backbone.View.extend(/** @lends wp.customize.HeaderTool.ChoiceListView.prototype */{
     192                /**
     193                 * Initializes the view, rendering it as the collection changes.
     194                 */
    149195                initialize: function() {
    150196                        this.listenTo(this.collection, 'add', this.addOne);
    … …  
    155201                },
    156202
     203                /**
     204                 * Renders each choice in the collection.
     205                 *
     206                 * @return {wp.customize.HeaderTool.ChoiceListView} Choice list view.
     207                 */
    157208                render: function() {
    158209                        this.$el.empty();
    159210                        this.collection.each(this.addOne, this);
    160211                        this.toggleList();
    161                 },
    162 
     212                        return this;
     213                },
     214
     215                /**
     216                 * Renders a single choice into the list.
     217                 *
     218                 * @param {Backbone.Model} choice Choice.
     219                 */
    163220                addOne: function(choice) {
    164221                        var view;
    … …  
    168225                },
    169226
     227                /**
     228                 * Shows or hides the list title and the random button.
     229                 */
    170230                toggleList: function() {
    171231                        var title = this.$el.parents().prev('.customize-control-title'),
    … …  
    189249         * @alias wp.customize.HeaderTool.CombinedList
    190250         *
    191          * @constructor
     251         * @class
    192252         * @augments wp.Backbone.View
    193253         */
    194254        api.HeaderTool.CombinedList = wp.Backbone.View.extend(/** @lends wp.customize.HeaderTool.CombinedList.prototype */{
     255                /**
     256                 * Initializes the view with the collections to combine.
     257                 *
     258                 * @param {Backbone.Collection[]} collections Collections.
     259                 */
    195260                initialize: function(collections) {
    196261                        this.collections = collections;
    197262                        this.on('all', this.propagate, this);
    198263                },
     264
     265                /**
     266                 * Passes an event on to each of the combined collections.
     267                 *
     268                 * @param {string} event Event.
     269                 * @param {*}      arg   Argument.
     270                 */
    199271                propagate: function(event, arg) {
    200272                        _.each(this.collections, function(collection) {
Note: See TracChangeset for help on using the changeset viewer.