Dan-nl has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/90041


Change subject: assembla/169 ------------ re: #169/assembla - Second source 
field mapping dissapears
......................................................................

assembla/169
------------
re: #169/assembla - Second source field mapping dissapears

* includes/Config.php
  ** added resource jquery.json

* includes/Forms/MetadataMappingForm.php
  ** added ids to form tables

* resources/js/ext.gwtoolset.js
  ** added logic to handle cookies in order to store/restore input/select 
fields added with js

Change-Id: I7afe3d2fb01becccd586afed2ead490dbcd01784
---
M includes/Config.php
M includes/Forms/MetadataMappingForm.php
M resources/js/ext.gwtoolset.js
3 files changed, 188 insertions(+), 35 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GWToolset 
refs/changes/41/90041/1

diff --git a/includes/Config.php b/includes/Config.php
index f33fa61..f536109 100644
--- a/includes/Config.php
+++ b/includes/Config.php
@@ -246,6 +246,7 @@
                        'gwtoolset-cancel'
                ),
                'dependencies' => array(
+                       'jquery.json',
                        'jquery.spinner',
                        'jquery.ui.widget',
                        'jquery.ui.button',
diff --git a/includes/Forms/MetadataMappingForm.php 
b/includes/Forms/MetadataMappingForm.php
index 220fad9..65a3ba8 100644
--- a/includes/Forms/MetadataMappingForm.php
+++ b/includes/Forms/MetadataMappingForm.php
@@ -320,7 +320,7 @@
 
                        Html::rawElement(
                                'table',
-                               array(),
+                               array( 'id' => 'global-categories-table' ),
                                Html::rawElement(
                                        'tbody',
                                        array(),
@@ -370,7 +370,7 @@
 
                        Html::rawElement(
                                'table',
-                               array(),
+                               array( 'id' => 'item-specific-categories-table' 
),
                                Html::rawElement(
                                        'thead',
                                        array(),
diff --git a/resources/js/ext.gwtoolset.js b/resources/js/ext.gwtoolset.js
index 9b06547..8d0fa50 100644
--- a/resources/js/ext.gwtoolset.js
+++ b/resources/js/ext.gwtoolset.js
@@ -1,6 +1,6 @@
-/*global jQuery, mw */
-/*jslint browser: true, white: true, devel: true, regexp: true */
-(function ( $ ) {
+/*global jQuery, mediaWiki */
+/*jslint browser: true, plusplus: true, regexp: true, white: true */
+(function ( mw, $ ) {
        'use strict';
 
        var gwtoolset,
@@ -86,6 +86,26 @@
 
        gwtoolset = {
 
+               $ajaxLoader: $( '<div>' )
+                       .attr( 'id', 'gwtoolset-loader' )
+                       .html(
+                               $( '<p>' )
+                                       .text( mw.message( 'gwtoolset-loading' 
).text() )
+                                       .append( $.createSpinner( { size: 
'large', type: 'block' } ) )
+                       ),
+               $backText: $( '#back-text' ),
+               $buttons: {
+                       $add: $( '<img>' )
+                               .attr( 'src',
+                                       mw.config.get('wgExtensionAssetsPath') 
+ '/GWToolset/resources/images/b_snewtbl.png'
+                               )
+                               .addClass( 'gwtoolset-metadata-button' ),
+                       $subtract: $( '<img>' )
+                               .attr( 'src',
+                                       mw.config.get('wgExtensionAssetsPath') 
+ '/GWToolset/resources/images/b_drop.png'
+                               )
+                               .addClass( 'gwtoolset-metadata-button' )
+               },
                $dialog: $( '<div>' )
                        .attr( 'id', 'dialog' )
                        .dialog( {
@@ -97,14 +117,9 @@
                                }
                        } ),
                $form: $( '#gwtoolset-form' ),
-               $ajaxLoader: $( '<div>' )
-                       .attr( 'id', 'gwtoolset-loader' )
-                       .html(
-                               $( '<p>' )
-                                       .text( mw.message( 'gwtoolset-loading' 
).text() )
-                                       .append( $.createSpinner( { size: 
'large', type: 'block' } ) )
-                       ),
-               $templateTableTbody: $( '#template-table > tbody' ),
+               formName: $('input[name=gwtoolset-form]').val(),
+               $globalCategoriesTableTbody: $( '#global-categories-table > 
tbody' ),
+               $itemSpecificCategoriesTableTbody: $( 
'#item-specific-categories-table > tbody' ),
                $saveMappingButton: $( '<tr>' )
                        .html(
                                $( '<td>' )
@@ -119,16 +134,8 @@
                                                        .text( mw.message( 
'gwtoolset-save-mapping' ).text() )
                                        )
                        ),
-               $buttons: {
-                       $add: $( '<img>' )
-                               .attr( 'src', 
mw.config.get('wgExtensionAssetsPath') + 
'/GWToolset/resources/images/b_snewtbl.png' )
-                               .addClass( 'gwtoolset-metadata-button' ),
-                       $subtract: $( '<img>' )
-                               .attr( 'src', 
mw.config.get('wgExtensionAssetsPath') + 
'/GWToolset/resources/images/b_drop.png' )
-                               .addClass( 'gwtoolset-metadata-button' )
-               },
-               $backText: $( '#back-text' ),
                $step2Link: $( '#step2-link' ),
+               $templateTableTbody: $( '#template-table > tbody' ),
 
                addAjaxLoader: function () {
                        this.$ajaxLoader.hide();
@@ -136,13 +143,21 @@
                },
 
                addBackLinks: function () {
-                       this.$backText.replaceWith( this.createBackLink( { 
title: mw.message( 'gwtoolset-back-text-link' ).text() } ) );
-                       this.$step2Link.replaceWith( this.createBackLink( { 
title: mw.message( 'gwtoolset-step-2-heading' ).text() } ) );
+                       this.$backText
+                               .replaceWith(
+                                       this.createBackLink( { title: 
mw.message( 'gwtoolset-back-text-link' ).text() } )
+                               );
+                       this.$step2Link
+                               .replaceWith(
+                                       this.createBackLink( { title: 
mw.message( 'gwtoolset-step-2-heading' ).text() } )
+                               );
                },
 
                addButtons: function () {
-                       $( '.button-add' ).html( this.$buttons.$add.clone().on( 
'click', this.handleButtonAddClick ) );
-                       $( '.button-subtract' ).html( 
this.$buttons.$subtract.clone().on( 'click', this.handleButtonSubtractClick ) );
+                       $( '.button-add' )
+                               .html( this.$buttons.$add.clone().on( 'click', 
this.handleButtonAddClick ) );
+                       $( '.button-subtract' )
+                               .html( this.$buttons.$subtract.clone().on( 
'click', this.handleButtonSubtractClick ) );
                },
 
                addFormListener: function () {
@@ -188,6 +203,38 @@
                                } );
                },
 
+               /**
+                * @param {Object} options
+                *   {Boolean} options.deparam
+                *   call $.String.deparam on the options.value
+                *
+                *   {String} options.name
+                *   name of the cookie
+                *
+                *   {Object} options.options
+                *   cookie options
+                *
+                *   {Boolean} options.toJSON
+                *   convert the value to a JSON
+                *
+                *   {String} options.value
+                */
+               createCookie: function ( options ) {
+                       if ( options.deparam === true ) {
+                               options.value = $.String.deparam( options.value 
);
+                       }
+
+                       if ( options.toJSON === true ) {
+                               options.value = $.toJSON( options.value );
+                       }
+
+                       $.cookie(
+                               options.name,
+                               options.value,
+                               options.options
+                       );
+               },
+
                handleAjaxError: function () {
                        gwtoolset.openDialog( { msg: mw.message( 
'gwtoolset-developer-issue' ).text() } );
                        mw.log( arguments );
@@ -209,11 +256,13 @@
                /**
                 * @param {Event} evt
                 */
-               handleButtonAddClick: function ( evt ) {
+               handleButtonAddClick: function ( evt, data ) {
                        var $target = $( this ).closest( 'tr' ),
                                $tdButton = $( '<td>' )
                                        .addClass( 'button-subtract' )
-                                       .html( 
gwtoolset.$buttons.$subtract.clone().on( 'click', 
gwtoolset.handleButtonSubtractClick ) ),
+                                       .html( 
gwtoolset.$buttons.$subtract.clone()
+                                               .on( 'click', 
gwtoolset.handleButtonSubtractClick )
+                                       ),
                                $row = $( '<tr>' );
 
                        evt.preventDefault();
@@ -228,8 +277,16 @@
                                        $row.append( $tdButton );
                                } else {
                                        $tdElm = $elm.clone();
-                                       $tdElm.find( 'input' ).val( '' );
-                                       $tdElm.find( 'option' ).prop( 
'selected', false );
+                                       if ( data && data.value ) {
+                                               $tdElm.find( 'input' ).val( 
data.value );
+                                       } else {
+                                               $tdElm.find( 'input' ).val( '' 
);
+                                       }
+                                       if ( data && data.option ) {
+                                               $tdElm.find( 'option:contains(' 
+ data.option + ')' ).prop( 'selected', 'selected' );
+                                       } else {
+                                               $tdElm.find( 'option' ).prop( 
'selected', false );
+                                       }
                                        $row.append( $tdElm );
                                }
                        });
@@ -245,7 +302,17 @@
                        $( this ).closest( 'tr' ).remove();
                },
 
+               /**
+                * remove cookies on step-1 submit, we don’t want previously 
stored values to be used.
+                * store cookies on step-2 submit
+                */
                handleFormSubmit: function () {
+                       if ( gwtoolset.formName === 'metadata-detect' ) {
+                               gwtoolset.removeCookies();
+                       } else if ( gwtoolset.formName === 'metadata-mapping' ) 
{
+                               gwtoolset.storeJsFormFields();
+                       }
+
                        gwtoolset.$ajaxLoader.fadeIn();
                },
 
@@ -288,10 +355,95 @@
                },
 
                init: function () {
-                       gwtoolset.addBackLinks();
-                       gwtoolset.addFormListener();
-                       gwtoolset.addSaveMappingButton();
-                       gwtoolset.addButtons();
+                       this.addBackLinks();
+                       this.addFormListener();
+                       this.addSaveMappingButton();
+                       this.addButtons();
+                       this.restoreJsFormFields();
+               },
+
+               removeCookies: function () {
+                       $.cookie( 'gwtoolset.global.categories', null );
+                       $.cookie( 'gwtoolset.item.specific.categories', null );
+                       $.cookie( 'gwtoolset.metadata.mappings', null );
+               },
+
+               /**
+                * restores js added input and select fields from JSONs within 
document.cookie
+                */
+               restoreJsFormFields: function () {
+                       var buttonAdd,
+                       fields,
+                       i,
+                       x;
+
+                       fields = $.secureEvalJSON( $.cookie( 
'gwtoolset.global.categories' ) );
+
+                       if ( fields && fields.category ) {
+                               buttonAdd = 
this.$globalCategoriesTableTbody.find( '.button-add img' );
+                               for ( i = 1; i < fields.category.length; i ++ ) 
{
+                                       buttonAdd.trigger( 'click', { value: 
[fields.category[i]] } );
+                               }
+                       }
+
+                       fields = $.secureEvalJSON( $.cookie( 
'gwtoolset.metadata.mappings' ) );
+
+                       if ( fields ) {
+                               for ( i in fields ) {
+                                       if ( fields.hasOwnProperty( i ) ) {
+                                               buttonAdd = $( '#' + i.replace( 
' ', '_' ) ).closest( 'tr' ).find('.button-add img');
+
+                                               for ( x = 1; x < 
fields[i].length ; x++ ) {
+                                                       buttonAdd.trigger( 
'click', { option: fields[i][x] } );
+                                               }
+                                       }
+                               }
+                       }
+
+                       fields = $.secureEvalJSON( $.cookie( 
'gwtoolset.item.specific.categories' ) );
+
+                       if ( fields && fields['category-metadata'] ) {
+                               buttonAdd = 
this.$itemSpecificCategoriesTableTbody.find( '.button-add img' );
+
+                               for ( i = 1; i < 
fields['category-metadata'].length; i ++ ) {
+                                       buttonAdd.trigger(
+                                               'click',
+                                               {
+                                                       option: 
[fields['category-metadata'][i]],
+                                                       value: 
[fields['category-phrase'][i]]
+                                               }
+                                       );
+                               }
+                       }
+               },
+
+               /**
+                * stores the js added input and select fields as JSONs 
document.cookie
+                */
+               storeJsFormFields: function () {
+                       this.createCookie( {
+                               deparam: true,
+                               name: 'gwtoolset.global.categories',
+                               options: { path: '/' },
+                               toJSON: true,
+                               value: this.$globalCategoriesTableTbody.find( 
'input' ).serialize()
+                       } );
+
+                       this.createCookie( {
+                               deparam: true,
+                               name: 'gwtoolset.item.specific.categories',
+                               options: { path: '/' },
+                               toJSON: true,
+                               value: 
this.$itemSpecificCategoriesTableTbody.find( 'input, select').serialize()
+                       } );
+
+                       this.createCookie( {
+                               deparam: true,
+                               name: 'gwtoolset.metadata.mappings',
+                               options: { path: '/' },
+                               toJSON: true,
+                               value: this.$templateTableTbody.find( 'select' 
).serialize()
+                       } );
                },
 
                /**
@@ -350,4 +502,4 @@
 
        gwtoolset.init();
 
-}( jQuery ));
+}( mediaWiki, jQuery ));

-- 
To view, visit https://gerrit.wikimedia.org/r/90041
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7afe3d2fb01becccd586afed2ead490dbcd01784
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/GWToolset
Gerrit-Branch: master
Gerrit-Owner: Dan-nl <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to