res/scite.page.creator.js   A
last analyzed

Complexity

Total Complexity 11
Complexity/F 1.83

Size

Lines of Code 75
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 37
c 0
b 0
f 0
dl 0
loc 75
rs 10
mnd 5
bc 5
fnc 6
bpm 0.8333
cpm 1.8333
noi 3
1
/**
2
 * MW Api handler to create article content from data-content-selector
3
 */
4
5
/*global jQuery, mediaWiki */
6
/*global confirm */
7
8
( function ( $, mw ) {
9
10
	'use strict';
11
12
	$( function ( $ ) {
13
14
		var page = {
15
16
			/**
17
			 * @since  1.0
18
			 *
19
			 * MW Api handler
20
			 */
21
			api: new mw.Api(),
22
23
			/**
24
			 * @since  1.0
25
			 *
26
			 * Create article
27
			 */
28
			create: function ( title, content ) {
29
30
				this.api.postWithToken( "csrf", {
31
					action: "edit",
32
					title: title,
33
					section: 0, // Existing content will be replaced
34
					// summary: section, // no need for a section heading
35
					text: content
36
				} ).done( function( result, jqXHR ) {
0 ignored issues
show
Unused Code introduced by
The parameter result is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter jqXHR is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
37
					location.reload();
38
					// $( '#scite-status' ).append( 'Added: ' + title ); // not sure we need an update not
39
				} ).fail( function( xhr, status, error ) {
0 ignored issues
show
Unused Code introduced by
The parameter error is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
40
41
					var apiErrorText = '';
42
43
					if ( xhr === "http" ) {
44
						apiErrorText = "HTTP error: " + status.textStatus; // status.xhr contains the jqXHR object
45
					} else if ( xhr === "ok-but-empty" ) {
46
						apiErrorText = "Got an empty response from the server";
47
					} else {
48
						apiErrorText = "API error: " + xhr;
49
					}
50
51
					if ( status.hasOwnProperty( 'xhr' ) ) {
52
						apiErrorText = status.xhr.responseText.replace(/\<br \/\>/g," ");
53
						var xhr = status.xhr;
54
55
						if ( xhr.hasOwnProperty( 'responseText' ) ) {
56
							apiErrorText = xhr.responseText.replace(/\<br \/\>/g," " );
57
						};
58
59
						if ( xhr.hasOwnProperty( 'statusText' ) ) {
60
							apiErrorText = 'The API returned with: ' + xhr.statusText.replace(/\<br \/\>/g," " );
61
						};
62
					}
63
64
					$( '#scite-status' ).append( apiErrorText );
65
				} );
66
			}
67
		}
68
69
		$( '.scite-create' ).on( 'click', function( event ) {
70
71
			// Dynamically select element that contains the content
72
			// to be copied
73
			page.create(
74
				$( this ).data( 'title' ),
75
				$( $( this ).data( 'content-selector' ) ).text()
76
			);
77
78
			event.preventDefault();
79
		} );
80
81
	} );
82
}( jQuery, mediaWiki ) );
83