Making WordPress.org

Changeset 7262


Ignore:
Timestamp:
06/01/2018 05:35:03 PM (8 years ago)
Author:
iandunn
Message:

WordCamp Post Types: Provide shareable link for favorited sessions.

Props egmanekki.
Fixes #2733.

Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/css/shortcodes.css

    r7261 r7262  
    130130        bottom: 50px;
    131131        right: 100px;
    132         width: 200px;
     132        width: 350px;
    133133        background: #dcdcdc;
    134134        font-size: 12px;
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/js/favourite-sessions.js

    r7261 r7262  
    11jQuery( document ).ready( function ( $ ) {
     2        var favSessionsUrlSlug = 'fav-sessions=';
     3
     4        function getUrlParams() {
     5                var url       = decodeURIComponent( window.location.search ),
     6                    urlParams = {};
     7
     8                url.replace( /[?&]+([^=&]+)=([^&]*)/gi, function( str, key, value ) {
     9                        urlParams[ key ] = value;
     10                } );
     11
     12                return urlParams;
     13        }
     14
    215        var FavSessions = {
    316                favSessKey: 'favourite_sessions',
    4 
    5                 get: function () {
    6                         var favSessions = JSON.parse( localStorage.getItem( this.favSessKey ) );
    7 
    8                         if ( ! favSessions ) {
    9                                 favSessions = {};
     17                useLocalStorage: 'local_storage',
     18                useUrlSessions:  'URL',
     19
     20                get: function() {
     21                        if ( this.primarySource == this.useLocalStorage ) {
     22                                var favSessions = JSON.parse( localStorage.getItem( this.favSessKey ) );
     23
     24                                if ( ! favSessions ) {
     25                                        favSessions = {};
     26                                }
     27                        } else {
     28                                favSessions = this.favSessionsFromUrl();
    1029                        }
    1130
     
    1433
    1534                toggleSession: function ( sessionId ) {
     35                        if ( this.primarySource !== this.useLocalStorage ) {
     36                                return;
     37                        }
     38
    1639                        var favSessions = this.get();
    1740
     
    2447                        localStorage.setItem( this.favSessKey, JSON.stringify( favSessions ) );
    2548                },
     49
     50                getSessionsForLink: function() {
     51                        var favSessions = this.get();
     52
     53                        return Object.keys( favSessions ).join();
     54                },
     55
     56                favSessionsFromUrl: function() {
     57                        var urlParams       = getUrlParams(),
     58                            urlSlugPosition = favSessionsUrlSlug.slice( 0, favSessionsUrlSlug.length - 1 ),
     59                            favSessionIds   = urlParams[ urlSlugPosition ].split( ',' ),
     60                            favSessions     = {};
     61
     62                        for ( var i = 0; i < favSessionIds.length; i++ ) {
     63                                favSessions[ favSessionIds[ i ] ] = true;
     64                        }
     65
     66                        return favSessions;
     67                },
     68
     69                updateBasedOnLink: function() {
     70                        favSessions = this.favSessionsFromUrl();
     71
     72                        localStorage.setItem( this.favSessKey, JSON.stringify( favSessions ) );
     73
     74                        return this.get();
     75                },
    2676        };
     77
     78        // Use local storage for session source for fetching & target for saving by default.
     79        FavSessions.primarySource = FavSessions.useLocalStorage;
    2780
    2881        function switchCellAppearance( sessionId ) {
     
    50103        }
    51104
     105        function updateShareLink() {
     106                var favSessionIds   = FavSessions.getSessionsForLink(),
     107                    urlParams       = getUrlParams(),
     108                    baseURL         = window.location.href,
     109                    paramsPosition  = baseURL.indexOf( '?' ),
     110                    urlSlugPosition = favSessionsUrlSlug.slice( 0, favSessionsUrlSlug.length - 1 );
     111
     112                if ( -1 !== paramsPosition ) {
     113                        baseURL = baseURL.slice( 0, paramsPosition );
     114                }
     115
     116                urlParams[ urlSlugPosition ] = favSessionIds;
     117
     118                // Don't include empty URL parameter.
     119                if ( '' === favSessionIds ) {
     120                        delete urlParams[ urlSlugPosition ];
     121                }
     122
     123                var favSessionsLink = baseURL + '?' + $.param( urlParams );
     124
     125                $( '#fav-sessions-link' ).text( favSessionsLink );
     126                $( '#fav-sessions-link' ).prop( 'href', favSessionsLink );
     127        }
     128
    52129        function switchSessionFavourite( sessionId ) {
    53130                FavSessions.toggleSession( sessionId );
    54131                switchCellAppearance( sessionId );
    55132                switchEmailFavButton();
     133                updateShareLink();
    56134        }
    57135
     
    63141                }
    64142
     143                /*
     144                 * The user has already saved some sessions in local storage, but is now
     145                 * loading a shared link. We need to determine whether they intend to overwrite
     146                 * their saved sessions with those in the link, or if they just want to view
     147                 * the link's sessions and then discard them, so that their saved sessions remain
     148                 * in tact.
     149                 */
     150                var currentUrl = window.location.href;
     151
     152                if ( currentUrl.indexOf( favSessionsUrlSlug ) > -1 ) {
     153                        var overwrite = confirm( favSessionsPhpObject.i18n.overwriteFavSessions );
     154
     155                        if ( true === overwrite ) {
     156                                FavSessions.primarySource = FavSessions.useLocalStorage;
     157                                favSessions               = FavSessions.updateBasedOnLink( currentUrl );
     158
     159                                $( '.fav-session-button' ).attr( 'title', '' );
     160                                $( '.fav-session-button' ).fadeTo( 0, 1 );
     161                        } else {
     162                                FavSessions.primarySource = FavSessions.useUrlSessions;
     163                                favSessions               = FavSessions.get();
     164
     165                                /*
     166                                 * Deactivate interaction with favourite session buttons,
     167                                 * since the use chose to not overwrite their saved sessions.
     168                                 */
     169                                $( '.fav-session-button' ).attr( 'title', favSessionsPhpObject.i18n.buttonDisabledNote );
     170                                $( '.fav-session-button' ).fadeTo( 0, 0.5 );
     171                                $( '.fav-session-button' ).css( 'color', '#e7e7e7' );
     172                        }
     173                }
     174
     175                if ( {} === favSessions ) {
     176                        return;
     177                }
     178
    65179                // Highlight favourite sessions in table.
    66180                var sessionIds = Object.keys( favSessions );
     
    75189
    76190                switchEmailFavButton();
     191                updateShareLink();
    77192        }
    78193
     
    119234                event.preventDefault();
    120235
    121                 var elem = $( this );
    122                 var sessionId = elem.parent().parent().data( 'session-id' );
    123                 switchSessionFavourite( sessionId );
     236                if ( FavSessions.primarySource == FavSessions.useLocalStorage ) {
     237                        var elem      = $( this ),
     238                            sessionId = elem.parent().parent().data( 'session-id' );
     239
     240                        switchSessionFavourite( sessionId );
     241                } else {
     242                        alert( favSessionsPhpObject.i18n.buttonDisabledAlert );
     243                }
    124244
    125245                return false;
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/wc-post-types.php

    r7261 r7262  
    732732                                        'reqTimeOut' => esc_html__( 'Sorry, the email request timed out.', 'wordcamporg' ),
    733733                                        'otherError' => esc_html__( 'Sorry, the email request failed.',    'wordcamporg' ),
     734                                        'overwriteFavSessions' => esc_html__( 'You already have some sessions saved. Would you like to overwrite those with the shared sessions that you are viewing?', 'wordcamporg' ),
     735                                        'buttonDisabledAlert'  => esc_html__( 'Interaction with favorite sessions disabled in share sessions view. Please click on schedule menu link to pick sessions.', 'wordcamporg' ),
     736                                        'buttonDisabledNote'   => esc_html__( 'Button disabled.', 'wordcamporg' ),
    734737                                ),
    735738                        )
     
    765768                                <?php endif; ?>
    766769
     770                                <div class="fav-session-tablinks" id="fav-session-btn-link">
     771                                        <?php esc_html_e( 'Link', 'wordcamporg' ); ?>
     772                                </div>
     773
    767774                                <div class="fav-session-tablinks" id="fav-session-btn-print">
    768775                                        <?php esc_html_e( 'Print', 'wordcamporg' ); ?>
     
    786793                                </div>
    787794                        <?php endif; ?>
     795
     796                        <div id="fav-session-tab-link" class="fav-session-share-tabcontent">
     797                                <?php esc_html_e( 'Shareable link:', 'wordcamporg' ); ?><br />
     798                                <a id="fav-sessions-link" href=""></a>
     799                        </div>
    788800
    789801                        <div id="fav-session-tab-print" class="fav-session-share-tabcontent">
Note: See TracChangeset for help on using the changeset viewer.