User:CWii/JB2script.js

This is an old revision of this page, as edited by CWii (talk | contribs) at 14:30, 19 June 2008 (update and pray). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function jb2script () {
    if( wgNamespaceNumber == 6 ) {
        addPortletLink( 'p-cactions', "javascript:jb2script.callback()", "JB2", "jb2script", "Tag for moval by John Bot II", "");
    }
}
addOnloadHook(jb2script);
 
jb2script.callback = function jb2scriptCallback() {
    var Window = new SimpleWindow( 600, 300 );
    Window.setTitle( "John Bot II image moval b" );
    var form = new QuickForm( jb2script.callback.evaluate );
    var categories = form.append( {
            type: 'select',
            name: 'movable',
            label: 'Is the image movable? ',
            tooltip: 'meh',
            event: jb2script.callback.change_category
        } );
    categories.append( {
            type: 'option',
            label: 'Yes - It can be moved by John Bot II',
            selected: true,
            value: 'yes'
        } );
    categories.append( {
            type: 'option',
            label: "No - It can't be moved by John Bot II",
            selected: false,
            value: 'no'
        } );
    categories.append( {
            type: 'option',
            label: "Not by a bot - It can't be moved by a robot",
            selected: false,
            value: 'notBot'
        } );
    form.append( {
            type: 'field',
            label:'Work area',
            name: 'work_area'
        } );
    var result = form.render();
    Window.setContent( result );
    Window.display();
 
    // We must init the
    var evt = document.createEvent( "Event" );
    evt.initEvent( 'change', true, true );
    result.movable.dispatchEvent( evt );
}
 
jb2script.callback.change_category = function jb2scriptCallbackChangeCategory(e) {
    var value = e.target.value;
    var root = e.target.form;
    var old_area;
    var childNodes = root.childNodes;
    for( var i = 0; i < childNodes.length; ++i ) {
        var node = childNodes[i];
        if( 
            node instanceof Element &&
            node.getAttribute( 'name' ) == 'work_area' 
        ) {
            old_area = node;
            break;
        }
    }
    var work_area = null;
 
    switch( value ) {
        case 'yes':
            work_area = new QuickForm.element( { 
                type: 'field',
                label: 'Movable',
                name: 'work_area'
            } );
            work_area.append( {
                type: 'input',
                name: 'newname',
                label: 'New name: '
            } );
			work_area.append( {
                type: 'input',
                name: 'comCat1',
                label: 'Commons Cat #1: '
            } );
			work_area.append( {
                type: 'input',
                name: 'comCat2',
                label: 'Commons Cat #2: '
            } );
			work_area.append( {
                type: 'input',
                name: 'comCat3',
                label: 'Commons Cat #3: '
            } );
			work_area.append( {
                type: 'input',
                name: 'comCat4',
                label: 'Commons Cat #4: '
            } );
            work_area.append( { type:'submit' } );
            work_area = work_area.render();
            old_area.parentNode.replaceChild( work_area, old_area );
            break;
        default:
            work_area = new QuickForm.element( { 
                    type: 'field',
                    label: 'Nothing for anything',
                    name: 'work_area'
                } );
            work_area = work_area.render();
            old_area.parentNode.replaceChild( work_area, old_area );
            break;
 
    }
}
 
jb2script.callbacks = {
	yes: function ( self ) {
		newname = self.params.newname;
		
		cc1 = self.params.cc1;
		cc2 = self.params.cc2;
		cc3 = self.params.cc3;
		cc4 = self.params.cc4;
		
		template = '\{\{jb2move|canMove=yes'
		template += '|user=' + wgUserName;
		
		if ( cc1 != '' ) {
			template += '|commCat1=' + cc1;
		}
		if ( cc2 != '' ) {
			template += '|commCat2=' + cc2;
		}
		if ( cc3 != '' ) {
			template += '|commCat3=' + cc3;
		}
		if ( cc4 != '' ) {
			template += '|commCat4=' + cc4;
		}
		
		template += '\}\}';
		
		var form = self.responseXML.getElementById('editorm');
		
		if( !form ) {
			self.statelem.error( 'Failed to get edit Form' );
		}
		
		var text = form.wpTextbox1.value;
		
		var postData = {
			'wpMinoredit': TwinkleConfig.markAIVReportAsMinor ? '' : form.wpMinoredit.checked ? '' : undefined, 
			'wpWatchthis': form.wpWatchthis.checked ? '' : undefined,
			'wpStarttime': form.wpStarttime.value,
			'wpEdittime': form.wpEdittime.value,
			'wpAutoSummary': form.wpAutoSummary.value,
			'wpEditToken': form.wpEditToken.value,
			'wpSummary': '(TESTING) Reviewing image for [[User:John Bot|JB-II]] (Image OK)',
			'wpTextbox1': template + text
		};
		
		self.post( postData );

		
	}
}

jb2script.callback.evaluate = function(e) {
	var form = e.target;
	var reason = "";
	switch( form.movable.value ) {
	default:
	case 'yes':
		var query = {
				'title': wgPageName,
				'action': 'submit',
		};
		var newname = form.newname.value;
		var cc1 = form.comCat1.value;
		var cc2 = form.comCat2.value;
		var cc3 = form.comCat3.value;
		var cc4 = form.comCat4.value;
		
		Status.init( form );
		
		wikipedia_wiki = new Wikipedia.wiki( 'Tagging Image', query, jb2script.callbacks.yes );
		wikipedia_wiki.params = { newname:newname, cc1:cc1, cc2:cc2, cc3:cc3, cc4:cc4 }
		wikipedia_wiki.get();
		break;
	}
}