User:Enterprisey/edit-wizard-early-prototype.js

// Early prototype, doesn't do anything interesting. // ( function {    var STRINGS = {        portlet_lbl: 'Suggest',        portlet_tooltip: 'Suggest an edit using the Edit Wizard',        main_menu_add_btn: 'Add Fact',        main_menu_tag_btn: 'Tag an Issue',        main_menu_update_btn: 'Update Text',        main_menu_delete_btn: 'Delete Text',        choose_source_title: 'Choose Source',        choose_source_instructions: 'Choose a source that states your fact.',        choose_quote_title: 'Choose Quote',        choose_quote_instructions: 'Copy and paste the text from the source that states your fact.',    };

var workflow = ''; // 'add', 'tag', 'update', or 'delete' var currCard = 'main-menu'; var editRequestData = {};

$.when(       $.ready,        mw.loader.using( [ 'mediawiki.util' ] )    ).then( function  {        var link = mw.util.addPortletLink( 'p-views', '#', STRINGS.portlet_lbl, 'ca-suggest', STRINGS.portlet_tooltip );        // we use querySelector because we nest the whole panel in the         // (which is what addPortletLink returns), so if we registered the click        // handler on the  we'd run the event listener every time there was        // a click on the panel.        link.querySelector( 'a' ).addEventListener( 'click', togglePanel );

goForDebugging; } );

function goForDebugging { togglePanel; showCard( 'source' ); }

function togglePanel { if( !$( '#edwz-panel' ).length ) { setupPanel; }       $( '#edwz-panel' ).toggle; }

function setupPanel { mw.loader.load('http://localhost:8001/style.css?' + new Date.getTime,'text/css');

$( '#ca-suggest > a' ).wrap( $( ' ', { 'id': 'edwz-ca-wrapper' } )               .css( { 'position': 'relative'} ) ); $( '#edwz-ca-wrapper' ).append(           $( ' ', { 'id': 'edwz-panel' } ).toggle            .css( 'top', ( $( '#ca-suggest a' ).outerHeight + 2 ) + 'px' ) );

showCard( 'main-menu' ); }

function showCard( cardName ) { if( !cardConstructors[cardName] ) { throw new Error( 'bad card name: ' + cardName ); }

var id = 'edwz-card-' + cardName; $( '#edwz-card-' + currCard ).toggle; if( !$( '#' + id ).length ) { $( '#edwz-panel' ).append(               $( ' ' )                    .append( cardConstructors[cardName] )            ); } else { $( '#' + id ).toggle; }       currCard = cardName; }

///////////////   //    // Card creation

var cardConstructors = { 'main-menu': makeMainMenuCard, 'source': makeSourceCard, 'quote': makeQuoteCard, };

function makeMainMenuCard { function makeButtonHtml( id ) { return ' ' + STRINGS[id] + ' '; }       var contents = $( ' Edit Wizard ' ); const FIRST_STEP = { 'add': 'source', 'tag': 'select', 'update': 'select', 'delete': 'select', };       [ 'add', 'tag', 'update', 'delete' ].forEach( function ( id ) {            contents.find( '#edwz-main_menu_' + id + '_btn' ).click( function  { workflow = id; showCard( FIRST_STEP[workflow] ); return false; } );       } );        return contents; }

function makeSourceCard { var contents = $( ' ' + STRINGS.choose_source_title + ' ' +           ' ' + STRINGS.choose_source_instructions + ' ' +            ' ' +            '' +            'Website ' +            '' +            'Book ' +            '' +            'Journal article ' +            '   ' +            ' '+            ' Back ' +            'Next '        ); function updateFieldLabel { var labelText = 'URL'; switch( contents.find( 'input[name="edwz-source"]:checked' ).val ) { case 'web': labelText = 'URL'; break; case 'book': labelText = 'ISBN'; break; case 'journal': labelText = 'DOI'; break; }           contents.find( '#edwz-source-field-label' ).text( labelText + ':' ); }       updateFieldLabel; contents.find( "input[type=radio]" ).click( updateFieldLabel ); function doCheckUrl { var result = checkUrl( contents.find( '#edwz-source-field' ).val ); contents.find( '#edwz-next' ).prop( 'disabled', !result.allowed ); }       doCheckUrl; contents.find( '#edwz-source-field' ).on( 'input', doCheckUrl ); contents.find( '#edwz-back' ).click( function {            showCard( 'main' );        } ); contents.find( '#edwz-next' ).click( function {            doCheckUrl;            if( !$( this ).prop( 'disabled' ) ) {                editRequestData.sourceType = contents.find( 'input[name="edwz-source"]:checked' ).val;                editRequestData.sourceDescriptor = contents.find( '#edwz-source-field' ).val;                showCard( 'quote' );            }        } ); return contents; }

function makeQuoteCard { var contents = $( ' ' + STRINGS.choose_quote_title + ' ' +           ' ' + STRINGS.choose_quote_instructions + ' ' +            '  ' +            ' Back ' +            '<button id="edwz-next">Next '        ); function doCheckQuote { var quote = contents.find( 'textarea' ).val; switch( editRequestData.sourceType ) { case 'web': return checkQuote( editRequestData.sourceDescriptor, quote ).then( function ( result ) {                       contents.find( '#edwz-next' ).prop( 'disabled', !result.allowed );                    } ); case 'book': case 'journal': // TODO return { allowed: true }; default: throw new Error( 'bad source type: ' + editRequestData.sourceType ); }       }        doCheckQuote; contents.find( '#edwz-quote-area' ).on( 'input', doCheckQuote ); contents.find( '#edwz-back' ).click( function {            showCard( 'source' );        } ); contents.find( '#edwz-next' ).prop( 'disabled', true ); contents.find( '#edwz-next' ).click( function {            doCheckQuote.then( function  { if( !$( this ).prop( 'disabled' ) ) { editRequestData.quote = contents.find( '#edwz-quote-area' ).val; showCard( 'rephrase' ); }           } );        } );        return contents; }

///////////////   //    // Source checking function checkUrl( url ) { // TODO... return { allowed: !!url && url !== 'google.com', };   }

///////////////   //    // Quote checking function checkQuote( url, quote ) { // TODO this is as crude as can be       if ( !quote ) { return $.when( { allowed: false } ); }       return $.get( url, function ( data ) {            var parser = new DOMParser;            var doc = parser.parseFromString( data, 'text/html' );            var text = doc.textContent;            return {                allowed: text.indexOf( quote ) >= 0,            };        } ); } } ); //