Making WordPress.org

Changeset 2327


Ignore:
Timestamp:
01/19/2016 08:44:53 PM (9 years ago)
Author:
iandunn
Message:

WordCamp Budgets: Centralize common JavaScript functions.

Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/payment-request.php

    r2309 r2327  
    182182            'payment-requests',
    183183            plugins_url( 'javascript/payment-requests.js', __DIR__ ),
    184             array( 'jquery', 'jquery-ui-datepicker', 'media-upload', 'media-views' ),
     184            array( 'wordcamp-budgets', 'wcb-attached-files', 'jquery' ),
    185185            WordCamp_Budgets::VERSION,
    186186            true
    187187        );
    188188
    189         wp_register_script(
    190             'wcp-attached-files',
    191             plugins_url( 'javascript/attached-files.js', __DIR__ ),
    192             array( 'payment-requests', 'backbone', 'wp-util' ),
    193             WordCamp_Budgets::VERSION,
    194             true
    195         );
    196 
    197189        // Enqueue our assets if they're needed on the current screen
    198190        $current_screen = get_current_screen();
     
    206198        if ( isset( $post->ID ) ) {
    207199            wp_enqueue_media( array( 'post' => $post->ID ) );
    208             wp_enqueue_script( 'wcp-attached-files' );
     200            wp_enqueue_script( 'wcb-attached-files' );
    209201        }
    210202
    211203        wp_localize_script(
    212204            'payment-requests',
    213             'wcpLocalizedStrings',      // todo merge into paymentRequests var
     205            'wcpLocalizedStrings',      // todo merge into WordCampBudgets var
    214206            array(
    215207                'uploadModalTitle'  => __( 'Attach Supporting Documentation', 'wordcamporg' ),
    216                 'uploadModalButton' => __( 'Attach Files', 'wordcamporg' ),
    217208            )
    218209        );
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/sponsor-invoice.php

    r2309 r2327  
    147147        'sponsor-invoices',
    148148        plugins_url( 'javascript/sponsor-invoices.js', __DIR__ ),
    149         array( 'jquery', 'jquery-ui-datepicker', 'underscore', 'wp-util' ),
     149        array( 'wordcamp-budgets', 'jquery', 'underscore', 'wp-util' ),
    150150        \WordCamp_Budgets::VERSION,
    151151        true
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/includes/wordcamp-budgets.php

    r2309 r2327  
    3838     */
    3939    public function enqueue_common_assets() {
    40         // todo setup grunt to concat/minify js and css?
     40        // todo setup grunt to concat/minify js
     41
     42        wp_enqueue_script(
     43            'wordcamp-budgets',
     44            plugins_url( 'javascript/wordcamp-budgets.js', __DIR__ ),
     45            array( 'jquery', 'jquery-ui-datepicker', 'media-upload', 'media-views' ),
     46            self::VERSION,
     47            true
     48        );
     49
     50        wp_register_script(
     51            'wcb-attached-files',
     52            plugins_url( 'javascript/attached-files.js', __DIR__ ),
     53            array( 'wordcamp-budgets', 'backbone', 'wp-util' ),
     54            WordCamp_Budgets::VERSION,
     55            true
     56        );
     57
     58        wp_localize_script(
     59            'wordcamp-budgets',
     60            'wcbLocalizedStrings',      // todo merge into WordCampBudgets var
     61            array(
     62                'uploadModalButton' => __( 'Attach Files', 'wordcamporg' ),
     63            )
     64        );
    4165
    4266        // Let's still include our .css file even if these are unavailable.
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/javascript/attached-files.js

    r2276 r2327  
    11jQuery( document ).ready( function( $ ) {
     2    // todo strict mode
     3
     4    var app = window.WordCampBudgets;
    25
    36    /*
    47     * Model for an attached file
    58     */
    6     $.paymentRequests.AttachedFile = Backbone.Model.extend( {
     9    app.AttachedFile = Backbone.Model.extend( {
    710        defaults: {
    811            'ID':          0,
     
    1619     * Collection of attached files
    1720     */
    18     $.paymentRequests.AttachedFiles = Backbone.Collection.extend( {
    19         model: $.paymentRequests.AttachedFile
     21    app.AttachedFiles = Backbone.Collection.extend( {
     22        model: app.AttachedFile
    2023    } );
    2124
     
    2326     * View for a single attached file
    2427     */
    25     $.paymentRequests.AttachedFileView = Backbone.View.extend( {
     28    app.AttachedFileView = Backbone.View.extend( {
    2629        tagName: 'li',
    2730        template: wp.template( 'wcp-attached-file' ),
     
    4043     * View for a collection of attached files
    4144     */
    42     $.paymentRequests.AttachedFilesView = Backbone.View.extend( {
     45    app.AttachedFilesView = Backbone.View.extend( {
    4346        el: $( '#wcp_files' ),
    4447
     
    4649            _.bindAll( this, 'render', 'appendFile' );
    4750
    48             this.collection = new $.paymentRequests.AttachedFiles( wcpAttachedFiles );
     51            this.collection = new app.AttachedFiles( wcbAttachedFiles );
    4952            this.collection.bind( 'add', this.appendFile );
    5053
     
    6265        appendFile: function( file ) {
    6366            var noFilesUploaded  = $( '.wcp_no_files_uploaded' );
    64             var attachedFileView = new $.paymentRequests.AttachedFileView( { model: file } );
     67            var attachedFileView = new app.AttachedFileView( { model: file } );
    6568
    6669            $( '.wcp_files_list' ).append( attachedFileView.render().el );
     
    7982         * Files that are already attached to other posts are ignored.
    8083         *
    81          * @param {paymentRequests.AttachedFile} file
     84         * @param {app.AttachedFile} file
    8285         */
    8386        attachExistingFile: function( file ) {
     
    98101    } );
    99102
    100     $.paymentRequests.attachedFilesView = new $.paymentRequests.AttachedFilesView();
    101 
    102103} );
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/javascript/payment-requests.js

    r2276 r2327  
    11jQuery( document ).ready( function( $ ) {
    22
    3     $.paymentRequests = {
     3    // todo add try/catch and log
    44
     5    var wcb = window.WordCampBudgets;
     6    var app = wcb.PaymentRequests = {
     7       
    58        /**
    69         * Main entry point
    710         */
    811        init: function () {
    9             $.paymentRequests.registerEventHandlers();
    10             $.paymentRequests.setupDatePicker();
     12            app.registerEventHandlers();
     13            wcb.attachedFilesView = new wcb.AttachedFilesView();
     14            wcb.setupDatePicker( '#wcp_general_info' );
    1115        },
    1216
     
    1519         */
    1620        registerEventHandlers : function() {
    17             $( '#wcp_payment_details' ).find( 'input[name=payment_method]' ).change( $.paymentRequests.togglePaymentMethodFields );
    18             $( '#payment_category' ).change( $.paymentRequests.toggleOtherCategoryDescription );
    19             $( '#wcp_files' ).find( 'a.wcp-insert-media' ).click( $.paymentRequests.showUploadModal );
    20             $( '#wcp_mark_incomplete_checkbox' ).click( $.paymentRequests.requireNotes );
     21            $( '#wcp_payment_details' ).find( 'input[name=payment_method]' ).change( wcb.togglePaymentMethodFields );
     22            $( '#payment_category' ).change( app.toggleOtherCategoryDescription );
     23                // todo this needs to fire onLoad too, otherwise the field is hidden
     24            $( '#wcp_files' ).find( 'a.wcp-insert-media' ).click( { title : wcpLocalizedStrings.uploadModalTitle }, wcb.showUploadModal );
     25            $( '#wcp_mark_incomplete_checkbox' ).click( app.requireNotes );
    2126        },
    2227
    2328        /**
    24          * Toggle the payment method fields based on which method is selected
    25          *
    26          * @param {object} event
    27          */
    28         togglePaymentMethodFields : function( event ) {
    29             var active_fields_container = '#' + $( this ).attr( 'id' ) + '_fields';
    30             var payment_method_fields   = '.payment_method_fields';
    31 
    32             $( payment_method_fields   ).removeClass( 'active' );
    33             $( payment_method_fields   ).addClass( 'hidden' );
    34             $( active_fields_container ).removeClass( 'hidden' );
    35             $( active_fields_container ).addClass( 'active' );
    36 
    37             // todo make the transition smoother
    38         },
    39 
    40         /**
    41          * Example event handler
     29         * Toggle the extra input field when the user selects the Other category
    4230         *
    4331         * @param {object} event
     
    5644
    5745        /**
    58          * Initialize Core's Media Picker
    59          *
    60          * @param {object} event
    61          */
    62         showUploadModal : function( event ) {
    63             if ( 'undefined' == typeof $.paymentRequests.fileUploadFrame ) {
    64                 // Create the frame
    65                 $.paymentRequests.fileUploadFrame = wp.media( {
    66                     title: wcpLocalizedStrings.uploadModalTitle,
    67                     multiple: true,
    68                     button: {
    69                         text: wcpLocalizedStrings.uploadModalButton
    70                     }
    71                 } );
    72 
    73                 // Add models to the collection for each selected attachment
    74                 $.paymentRequests.fileUploadFrame.on( 'select', $.paymentRequests.addSelectedFilesToCollection );
    75             }
    76 
    77             $.paymentRequests.fileUploadFrame.open();
    78             return false;
    79         },
    80 
    81         /**
    82          * Add files selected from the Media Picker to the current collection of files
    83          */
    84         addSelectedFilesToCollection : function() {
    85             var attachments = $.paymentRequests.fileUploadFrame.state().get( 'selection' ).toJSON();
    86 
    87             $.each( attachments, function( index, attachment ) {
    88                 var newFile = new $.paymentRequests.AttachedFile( {
    89                     'ID':          attachment.id,
    90                     'post_parent': attachment.uploadedTo,
    91                     'filename':    attachment.filename,
    92                     'url':         attachment.url
    93                 } );
    94 
    95                 $.paymentRequests.attachedFilesView.collection.add( newFile );
    96             } );
    97         },
    98 
    99         /**
    100          * Fallback to the jQueryUI datepicker if the browser doesn't support <input type="date">
    101          */
    102         setupDatePicker : function() {
    103             var browserTest = document.createElement( 'input' );
    104             browserTest.setAttribute( 'type', 'date' );
    105 
    106             if ( 'text' === browserTest.type ) {
    107                 $( '#wcp_general_info' ).find( 'input[type=date]' ).not( '[readonly="readonly"]' ).datepicker( {
    108                     dateFormat : 'yy-mm-dd',
    109                     changeMonth: true,
    110                     changeYear : true
    111                 } );
    112             }
    113         },
    114 
    115         /**
    11646         * Require notes when the request is being marked as incomplete
    11747         */
     
    12757    };
    12858
    129     $.paymentRequests.init();
     59    app.init();
    13060} );
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/javascript/sponsor-invoices.js

    r2303 r2327  
    22    'use strict';
    33
    4     $.sponsorInvoices = {
     4    var wcb = window.WordCampBudgets;
     5    var app = wcb.SponsorInvoices = {
    56
    67        /**
     
    910        init: function() {
    1011            try {
    11                 $.sponsorInvoices.registerEventHandlers();
     12                app.registerEventHandlers();
    1213                $( '#_wcbsi_sponsor_id' ).trigger( 'change' );  // Populate the initial sponsor information
    13                 $.sponsorInvoices.setupDatePicker();
     14                wcb.setupDatePicker( '#wcbsi_sponsor_invoice' );
    1415            } catch ( exception ) {
    15                 $.sponsorInvoices.log( exception );
     16                wcb.log( exception );
    1617            }
    1718        },
     
    2122         */
    2223        registerEventHandlers : function() {
    23             $( '#_wcbsi_sponsor_id' ).change( $.sponsorInvoices.populateSponsorInformation );
    24 
    25             // todo detect when invoice fields incomplete, disable submit button (but not draft), and show notice
     24            $( '#_wcbsi_sponsor_id' ).change( app.populateSponsorInformation );
    2625        },
    2726
     
    4241                    sendInvoiceButton.prop( 'disabled', true );
    4342                    return;
     43
    4444                } else if ( info.requiredFieldsComplete ) {
     45                    // todo add info.hasOwnProperty() check
    4546                    infoTemplate = wp.template( 'wcbsi-sponsor-information' );
    4647                    sendInvoiceButton.prop( 'disabled', false );
     48
    4749                } else {
    4850                    infoTemplate = wp.template( 'wcbsi-required-fields-incomplete' );
     
    5254                infoContainer.html( infoTemplate( info ) );
    5355            } catch ( exception ) {
    54                 $.sponsorInvoices.log( exception );
    55             }
    56         },
    57 
    58         /**
    59          * Fallback to the jQueryUI datepicker if the browser doesn't support <input type="date">
    60          *
    61          * todo this is mostly duplicate of same function in payment-requests.js. should make DRY
    62          */
    63         setupDatePicker : function() {
    64             var browserTest = document.createElement( 'input' );
    65             browserTest.setAttribute( 'type', 'date' );
    66 
    67             if ( 'text' === browserTest.type ) {
    68                 $( '#wcbsi_sponsor_invoice' ).find( 'input[type=date]' ).not( '[readonly="readonly"]' ).datepicker( {
    69                     dateFormat : 'yy-mm-dd',
    70                     changeMonth: true,
    71                     changeYear : true
    72                 } );
    73             }
    74         },
    75 
    76         /**
    77          * Log a message to the console
    78          *
    79          * @todo centralize this for all modules to use
    80          *
    81          * @param {*} error
    82          */
    83         log : function( error ) {
    84             if ( ! window.console ) {
    85                 return;
    86             }
    87 
    88             if ( 'string' === typeof error ) {
    89                 console.log( 'WordCamp Sponsor Invoices: ' + error );
    90             } else {
    91                 console.log( 'WordCamp Sponsor Invoices: ', error );
     56                wcb.log( exception );
    9257            }
    9358        }
    9459    };
    9560
    96     $.sponsorInvoices.init();
     61    app.init();
    9762} );
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments/views/payment-request/input-files.php

    r2272 r2327  
    99        <?php endif; ?>
    1010
     11        <?php // todo move to centralized view file ?>
    1112        <p>
    1213            <a class="button wcp-insert-media" role="button">
     
    2728        </script>
    2829
    29         <?php wp_localize_script( 'wcp-attached-files', 'wcpAttachedFiles', $files ); // todo merge into wordcampBudgets var ?>
     30        <?php wp_localize_script( 'wcb-attached-files', 'wcbAttachedFiles', $files ); // todo merge into wordcampBudgets var ?>
    3031    </td>
    3132</tr>
Note: See TracChangeset for help on using the changeset viewer.