PageRenderTime 23ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/MoodBar/modules/ext.moodBar/ext.moodBar.core.js

https://github.com/ChuguluGames/mediawiki-svn
JavaScript | 272 lines | 237 code | 22 blank | 13 comment | 11 complexity | 206b746d1cfa1536c288e99e53b0eb71 MD5 | raw file
  1. /**
  2. * Front-end scripting core for the MoodBar MediaWiki extension
  3. *
  4. * @author Timo Tijhof, 2011
  5. */
  6. ( function( $ ) {
  7. var mb = mw.moodBar;
  8. $.extend( mb, {
  9. tpl: {
  10. overlayBase: '\
  11. <div id="mw-moodBar-overlayWrap"><div id="mw-moodBar-overlay">\
  12. <div id="mw-moodBar-pokey"></div>\
  13. <span class="mw-moodBar-overlayClose"><a href="#"><html:msg key="moodbar-close" /></a></span>\
  14. <div class="mw-moodBar-overlayContent"></div>\
  15. </div></div>',
  16. userinput: '\
  17. <div class="mw-moodBar-overlayTitle"><html:msg key="INTROTITLE" /></div>\
  18. <div class="mw-moodBar-types"></div>\
  19. <div class="mw-moodBar-form">\
  20. <div class="mw-moodBar-formTitle">\
  21. <span class="mw-moodBar-formNote"><html:msg key="moodbar-form-note" /></span>\
  22. <html:msg key="moodbar-form-title" />\
  23. </div>\
  24. <div class="mw-moodBar-formInputs">\
  25. <textarea rows="3" maxlength="140" class="mw-moodBar-formInput" /></textarea>\
  26. <div class="mw-moodBar-privacy"></div>\
  27. <input type="button" class="mw-moodBar-formSubmit" disabled="disabled" />\
  28. </div>\
  29. </div>\
  30. <span class="mw-moodBar-overlayWhat">\
  31. <a href="#" title-msg="tooltip-moodbar-what">\
  32. <span class="mw-moodBar-overlayWhatTrigger"></span>\
  33. <span class="mw-moodBar-overlayWhatLabel"><html:msg key="moodbar-what-label" /></span>\
  34. </a>\
  35. <div class="mw-moodBar-overlayWhatContent"></div>\
  36. </span>',
  37. type: '\
  38. <div class="mw-moodBar-type mw-moodBar-type-$1" rel="$1">\
  39. <span class="mw-moodBar-typeTitle"><html:msg key="moodbar-type-$1-title" /></span>\
  40. </div>',
  41. loading: '\
  42. <div class="mw-moodBar-state mw-moodBar-state-loading">\
  43. <div class="mw-moodBar-state-title"><html:msg key="moodbar-loading-title" /></div>\
  44. <div class="mw-moodBar-state-subtitle"><html:msg key="moodbar-loading-subtitle" /></div>\
  45. </div>',
  46. success: '\
  47. <div class="mw-moodBar-state mw-moodBar-state-success">\
  48. <div class="mw-moodBar-state-title"><html:msg key="moodbar-success-title" /></div>\
  49. <div class="mw-moodBar-state-subtitle"><html:msg key="moodbar-success-subtitle" /></div>\
  50. </div>',
  51. error: '\
  52. <div class="mw-moodBar-state mw-moodBar-state-error">\
  53. <div class="mw-moodBar-state-title"><html:msg key="moodbar-error-title" /></div>\
  54. <div class="mw-moodBar-state-subtitle"><html:msg key="moodbar-error-subtitle" /></div>\
  55. </div>'
  56. },
  57. event: {
  58. trigger: function( e ) {
  59. e.preventDefault();
  60. if ( mb.ui.overlay.is( ':hidden' ) ) {
  61. mb.swapContent( mb.tpl.userinput );
  62. mb.ui.overlay.show();
  63. } else {
  64. mb.ui.overlay.hide();
  65. }
  66. },
  67. disable: function( e ) {
  68. e.preventDefault();
  69. $.cookie(
  70. mb.cookiePrefix() + 'disabled',
  71. '1',
  72. { 'path': '/', 'expires': Number( mb.conf.disableExpiration ) }
  73. );
  74. mb.ui.overlay.fadeOut( 'fast' );
  75. mb.ui.trigger.fadeOut( 'fast' );
  76. }
  77. },
  78. feedbackItem: {
  79. comment: '',
  80. bucket: mb.conf.bucketKey,
  81. type: 'unknown',
  82. callback: function( data ) {
  83. if ( data && data.moodbar && data.moodbar.result === 'success' ) {
  84. mb.swapContent( mb.tpl.success );
  85. setTimeout( function() {
  86. mb.ui.overlay.fadeOut();
  87. }, 3000 );
  88. } else {
  89. mb.swapContent( mb.tpl.error );
  90. }
  91. }
  92. },
  93. prepareUserinputContent: function( overlay ) {
  94. overlay
  95. // Populate type selector
  96. .find( '.mw-moodBar-types' )
  97. .append( function() {
  98. var $mwMoodBarTypes = $( this ),
  99. elems = [];
  100. $.each( mb.conf.validTypes, function( id, type ) {
  101. elems.push(
  102. $( mb.tpl.type.replace( /\$1/g, type ) )
  103. .localize()
  104. .click( function( e ) {
  105. var $el = $( this );
  106. mb.ui.overlay.find( '.mw-moodBar-formSubmit').removeAttr('disabled');
  107. mb.ui.overlay.find( '.mw-moodBar-formInput' ).focus();
  108. $mwMoodBarTypes.addClass( 'mw-moodBar-types-select' );
  109. mb.feedbackItem.type = $el.attr( 'rel' );
  110. $el.addClass( 'mw-moodBar-selected' );
  111. $el.parent()
  112. .find( '.mw-moodBar-selected' )
  113. .not( $el )
  114. .removeClass( 'mw-moodBar-selected' );
  115. } )
  116. .get( 0 )
  117. );
  118. } );
  119. return elems;
  120. } )
  121. .hover( function() {
  122. $( this ).addClass( 'mw-moodBar-types-hover' );
  123. }, function() {
  124. $( this ).removeClass( 'mw-moodBar-types-hover' );
  125. } )
  126. .end()
  127. // Link what-button
  128. .find( '.mw-moodBar-overlayWhatTrigger' )
  129. .text( mw.msg( 'moodbar-what-collapsed' ) )
  130. .end()
  131. .find( '.mw-moodBar-overlayWhat > a' )
  132. .click( function( e ) {
  133. e.preventDefault();
  134. mb.ui.overlay
  135. .find( '.mw-moodBar-overlayWhatContent' )
  136. .each( function() {
  137. var $el = $( this ),
  138. $trigger = mb.ui.overlay.find( '.mw-moodBar-overlayWhatTrigger' );
  139. if ( $el.is( ':visible' ) ) {
  140. $el.slideUp( 'fast' );
  141. $trigger.html( mw.msg( 'moodbar-what-collapsed' ) );
  142. } else {
  143. $el.slideDown( 'fast' );
  144. $trigger.html( mw.msg( 'moodbar-what-expanded' ) );
  145. }
  146. } )
  147. } )
  148. .end()
  149. .find( '.mw-moodBar-overlayWhatContent' )
  150. .html(
  151. function() {
  152. var message, linkMessage, link,
  153. disableMsg, disableLink, out;
  154. message = mw.msg( 'moodbar-what-content' );
  155. linkMessage = mw.msg( 'moodbar-what-link' );
  156. link = mw.html.element( 'a', {
  157. 'href': mb.conf.infoUrl,
  158. 'title': linkMessage
  159. }, linkMessage );
  160. out = mw.html.escape( message )
  161. .replace( /\$1/, link );
  162. out = mw.html.element( 'p', {},
  163. new mw.html.Raw( out )
  164. );
  165. disableMsg = mw.msg( 'moodbar-disable-link' )
  166. disableLink = mw.html.element( 'a', {
  167. 'href' : '#',
  168. 'class' : 'mw-moodBar-disable'
  169. }, disableMsg );
  170. out += mw.html.element( 'p', {},
  171. new mw.html.Raw( disableLink )
  172. );
  173. return out;
  174. }
  175. )
  176. .find('.mw-moodBar-disable')
  177. .click( mb.event.disable )
  178. .end()
  179. .end()
  180. .find( '.mw-moodBar-privacy' )
  181. .html(
  182. function() {
  183. var message, linkMessage, link;
  184. message = mw.msg( 'moodbar-privacy' );
  185. linkMessage = mw.msg( 'moodbar-privacy-link' );
  186. link = mw.html.element( 'a', {
  187. 'href': mb.conf.privacyUrl,
  188. 'title': linkMessage
  189. }, linkMessage );
  190. return mw.html.escape( message )
  191. .replace( /\$1/, link );
  192. }
  193. )
  194. .end()
  195. // Submit
  196. .find( '.mw-moodBar-formSubmit' )
  197. .val( mw.msg( 'moodbar-form-submit' ) )
  198. .click( function() {
  199. mb.feedbackItem.comment = mb.ui.overlay.find( '.mw-moodBar-formInput' ).val();
  200. mb.swapContent( mb.tpl.loading );
  201. $.moodBar.submit( mb.feedbackItem );
  202. } )
  203. .end();
  204. },
  205. core: function() {
  206. // Create overlay
  207. mb.ui.overlay = $( mb.tpl.overlayBase )
  208. .localize()
  209. // Bind close-toggle
  210. .find( '.mw-moodBar-overlayClose' )
  211. .click( mb.event.trigger )
  212. .end();
  213. mb.swapContent( mb.tpl.userinput );
  214. mb.ui.overlay
  215. // Inject overlay
  216. .appendTo( 'body' )
  217. // Fix the width after the icons and titles are localized and inserted
  218. .width( function( i, width ) {
  219. return width + 10;
  220. } );
  221. // Bind triger
  222. mb.ui.trigger.click( mb.event.trigger );
  223. },
  224. swapContent: function( tpl ) {
  225. var sitenameParams = [mw.config.get( 'wgSiteName' )], // Cache common params
  226. msgOptions = {
  227. keys: {
  228. INTROTITLE: 'moodbar-intro-' + mb.conf.bucketKey
  229. },
  230. params: {
  231. INTROTITLE: sitenameParams,
  232. 'moodbar-loading-subtitle': sitenameParams,
  233. 'moodbar-success-subtitle': sitenameParams,
  234. 'moodbar-error-subtitle': sitenameParams
  235. }
  236. };
  237. mb.ui.overlay
  238. .find( '.mw-moodBar-overlayContent' )
  239. .html( tpl )
  240. .localize( msgOptions );
  241. if ( tpl == mb.tpl.userinput ) {
  242. mb.prepareUserinputContent( mb.ui.overlay );
  243. }
  244. return true;
  245. }
  246. } );
  247. if ( !mb.isDisabled() ) {
  248. mb.core();
  249. }
  250. } )( jQuery );