Index: trunk/extensions/MarkAsHelpful/modules/ext.markAsHelpful/ext.markAsHelpful.js |
— | — | @@ -4,91 +4,107 @@ |
5 | 5 | * @author Rob Moen, 2011 |
6 | 6 | */ |
7 | 7 | |
8 | | -(function( $ ) { |
| 8 | +( function ( $, mw ) { |
9 | 9 | |
10 | | - var mah = mw.mah = { |
| 10 | + var mah = mw.markAsHelpful = { |
11 | 11 | loadedItems: [], |
12 | | - selector: '[class^=markashelpful]', //class of element(s) to apply MarkAsHelpful to. |
| 12 | + // Class of element(s) to apply MarkAsHelpful to. |
| 13 | + selector: '[class^="markashelpful"]', |
13 | 14 | |
14 | | - init: function() { |
| 15 | + init: function () { |
15 | 16 | var props, thisItem; |
16 | | - $( mah.selector ).each( function ( i, e ) { |
17 | | - props = mah.getItemProperties( $(this) ); |
18 | | - thisItem = props.type + props.item; //create an item reference to place in the loaded items array. |
| 17 | + |
| 18 | + $( mah.selector ).each( function ( i, el ) { |
| 19 | + props = mah.getItemProperties( $(el) ); |
| 20 | + // Create an item reference to place in the loaded items array. |
| 21 | + thisItem = props.type + props.item; |
19 | 22 | |
20 | | - //load once per type+id because user can copy / paste element on the talk page and load the same item many times. |
| 23 | + // Load once per type+id because user can copy / paste element on the talk page |
| 24 | + // and load the same item many times. |
21 | 25 | if( $.inArray( thisItem, mah.loadedItems ) === -1 ) { |
22 | 26 | mah.loadedItems.push( thisItem ); |
23 | | - mah.loadItem( $( this ) ); |
| 27 | + mah.loadItem( $( el ) ); |
24 | 28 | } |
25 | 29 | }); |
26 | 30 | }, |
27 | | - /* |
| 31 | + |
| 32 | + /** |
28 | 33 | * Return object of item properties |
| 34 | + * @param $item |
29 | 35 | */ |
30 | | - getItemProperties: function( $item ) { |
31 | | - var tag = $item.attr( 'class' ), |
32 | | - //item properties are stored in classname to prevent parser from stripping out non html 5 objects. |
33 | | - //(eg data-markashelpful-item) |
34 | | - properties = { |
35 | | - 'item': tag.split( '-' )[2], // item id |
36 | | - 'type': tag.split( '-' )[1] // item type (eg, mbresponse) |
37 | | - }; |
38 | | - return properties; |
| 36 | + getItemProperties: function ( $item ) { |
| 37 | + var tag, props; |
| 38 | + |
| 39 | + tag = $item.attr( 'class' ); |
| 40 | + // Item properties are stored in classname to prevent parser from stripping |
| 41 | + // out non html 5 objects. (eg. data-markashelpful-item) |
| 42 | + props = { |
| 43 | + // item id |
| 44 | + item: tag.split( '-' )[2], |
| 45 | + // item type (eg, mbresponse) |
| 46 | + type: tag.split( '-' )[1] |
| 47 | + }; |
| 48 | + return props; |
39 | 49 | }, |
40 | | - /* |
| 50 | + |
| 51 | + /** |
41 | 52 | * Load the current state of the MarkAsHelpful item |
| 53 | + * @param $item |
42 | 54 | */ |
43 | | - loadItem: function( $item ) { |
44 | | - var props = mah.getItemProperties( $item ), |
45 | | - request = { |
46 | | - 'action': 'getmarkashelpfulitem', |
47 | | - 'item': props.item, |
48 | | - 'type': props.type, |
49 | | - 'page': mw.config.get( 'wgPageName' ), |
50 | | - 'format': 'json' |
51 | | - }; |
| 55 | + loadItem: function ( $item ) { |
| 56 | + var props, request; |
52 | 57 | |
| 58 | + props = mah.getItemProperties( $item ); |
| 59 | + request = { |
| 60 | + format: 'json', |
| 61 | + action: 'getmarkashelpfulitem', |
| 62 | + item: props.item, |
| 63 | + type: props.type, |
| 64 | + page: mw.config.get( 'wgPageName' ) |
| 65 | + }; |
| 66 | + |
53 | 67 | $.ajax({ |
54 | 68 | type: 'POST', |
55 | 69 | url: mw.util.wikiScript('api'), |
56 | 70 | cache: false, |
57 | 71 | data: request, |
58 | | - success: function( data ) { |
| 72 | + success: function ( data ) { |
| 73 | + var $content; |
59 | 74 | |
60 | 75 | if ( data.getmarkashelpfulitem && |
61 | | - data.getmarkashelpfulitem.result == 'success' && |
| 76 | + data.getmarkashelpfulitem.result === 'success' && |
62 | 77 | data.getmarkashelpfulitem.formatted |
63 | 78 | ) { |
64 | | - var $content = $( data.getmarkashelpfulitem.formatted ); |
| 79 | + $content = $( data.getmarkashelpfulitem.formatted ); |
65 | 80 | $item.html( $content ); |
66 | | - } else { |
67 | | - // Failure, do nothing to the item for now |
68 | 81 | } |
| 82 | + // else: Failure, do nothing to the item for now else |
69 | 83 | }, |
70 | | - error: function ( data ) { |
| 84 | + error: function () { |
71 | 85 | // Failure, do nothing to the item for now |
72 | 86 | }, |
73 | 87 | dataType: 'json' |
74 | 88 | }); |
75 | 89 | |
76 | 90 | }, |
77 | | - /* |
| 91 | + |
| 92 | + /** |
78 | 93 | * API call to mark or unmark an item as helpful. |
79 | 94 | */ |
80 | | - markItem: function( $clicked, action ) { |
81 | | - var $item = $clicked.parent().parent(), |
82 | | - props = mah.getItemProperties( $item ), |
83 | | - clientData = $.client.profile(), |
84 | | - request; |
| 95 | + markItem: function ( $clicked, action ) { |
| 96 | + var $item, props, clientData, request; |
| 97 | + |
| 98 | + $item = $clicked.parent().parent(); |
| 99 | + props = mah.getItemProperties( $item ); |
| 100 | + clientData = $.client.profile(); |
85 | 101 | props.mahaction = action; |
86 | 102 | request = $.extend( { |
87 | | - 'action': 'markashelpful', |
88 | | - 'page': mw.config.get( 'wgPageName' ), |
89 | | - 'useragent': clientData.name + '/' + clientData.versionNumber, |
90 | | - 'system': clientData.platform, |
91 | | - 'token': mw.user.tokens.get( 'editToken' ), |
92 | | - 'format': 'json' |
| 103 | + action: 'markashelpful', |
| 104 | + format: 'json', |
| 105 | + page: mw.config.get( 'wgPageName' ), |
| 106 | + useragent: clientData.name + '/' + clientData.versionNumber, |
| 107 | + system: clientData.platform, |
| 108 | + token: mw.user.tokens.get( 'editToken' ) |
93 | 109 | }, props ); |
94 | 110 | |
95 | 111 | $.ajax( { |
— | — | @@ -105,22 +121,24 @@ |
106 | 122 | |
107 | 123 | // Some live events for the different modes |
108 | 124 | |
109 | | - $(document).ready( function() { |
110 | | - /* |
| 125 | + $(document).ready( function () { |
| 126 | + |
| 127 | + /** |
111 | 128 | * Click Event for marking an item as helpful. |
112 | 129 | */ |
113 | | - $( '.markashelpful-mark' ).live( 'click', function() { |
| 130 | + $( '.markashelpful-mark' ).live( 'click', function () { |
114 | 131 | mah.markItem( $(this), 'mark' ); |
115 | 132 | } ); |
116 | 133 | |
117 | | - /* |
| 134 | + /** |
118 | 135 | * Click Event for removing helpful status from an item. |
119 | 136 | */ |
120 | | - $( '.markashelpful-undo' ).live( 'click', function() { |
| 137 | + $( '.markashelpful-undo' ).live( 'click', function () { |
121 | 138 | mah.markItem( $(this), 'unmark' ); |
122 | 139 | } ); |
123 | 140 | |
124 | 141 | // Initialize MarkAsHelpful |
125 | 142 | mah.init(); |
126 | 143 | } ); |
127 | | -} ) ( jQuery ); |
| 144 | + |
| 145 | +}( jQuery, mediaWiki ) ); |