PageRenderTime 63ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Bundle/LichessBundle/Resources/public/js/ctrl.js

http://github.com/ornicar/lichess
JavaScript | 295 lines | 258 code | 33 blank | 4 comment | 37 complexity | b1b2a3e1d9aab974267d405a5be2aed7 MD5 | raw file
  1. if (typeof console == "undefined" || typeof console.log == "undefined") console = {
  2. log: function() {}
  3. };
  4. // declare now, populate later
  5. var lichess_translations = [];
  6. var lichess = {
  7. socket: null,
  8. socketDefaults: {
  9. params: {
  10. username: $('#username_tag').text()
  11. },
  12. events: {
  13. n: function(e) {
  14. var $tag = $('#nb_connected_players');
  15. $tag.html($tag.html().replace(/\d+/, e)).removeClass('none');
  16. }
  17. },
  18. options: {
  19. name: "site",
  20. offlineTag: $('#connection_lost')
  21. }
  22. },
  23. onProduction: /.+\.lichess\.org/.test(document.domain)
  24. };
  25. lichess.socketUrl = lichess.onProduction ? "lichess.org:9000" : "127.0.0.1:9000";
  26. $(function() {
  27. if (!$.websocket.available) {
  28. $.ajax('/bundles/lichess/browser.html', { success: function(html) { $('body').prepend(html); } });
  29. }
  30. // Start game
  31. var $game = $('div.lichess_game').orNot();
  32. if ($game) {
  33. $game.game(lichess_data);
  34. if (!lichess_data.player.spectator) {
  35. $('a.blank_if_play').click(function() {
  36. $(this).attr('target', '_blank');
  37. });
  38. }
  39. }
  40. setTimeout(function() {
  41. if (lichess.socket == null && $('div.server_error_box').length == 0) {
  42. lichess.socket = new $.websocket(lichess.socketUrl + "/socket", 0, lichess.socketDefaults);
  43. }
  44. }, 2000);
  45. $('input.lichess_id_input').select();
  46. if ($bw = $('div.lichess_board_wrap').orNot()) {
  47. if ($('div.lichess_homepage').length == 0)
  48. $.displayBoardMarks($bw, $('#lichess > div.lichess_player_white').length);
  49. // board color
  50. var $board = $bw.find('> div.lichess_board');
  51. var $picker = $('#top a.colorpicker');
  52. var colors = ['brown', 'grey', 'green', 'blue'];
  53. var color;
  54. function setColor(c) {
  55. color = c;
  56. $picker.add($board).removeClass(colors.join(' ')).addClass(c);
  57. }
  58. setColor($picker.data('color'));
  59. $picker.click(function() {
  60. var c = colors[(colors.indexOf(color) + 1) % colors.length];
  61. setColor(c);
  62. $.ajax($picker.attr("href"), {
  63. type: 'POST',
  64. data: {color: c}
  65. });
  66. return false;
  67. });
  68. } else {
  69. $('#top a.colorpicker').remove();
  70. }
  71. $.centerOverboard = function() {
  72. if ($overboard = $('div.lichess_overboard.auto_center').orNot()) {
  73. $overboard.css('top', (238 - $overboard.height() / 2) + 'px').show();
  74. }
  75. };
  76. $.centerOverboard();
  77. $('div.lichess_language').click(function() {
  78. $(this).toggleClass('toggled');
  79. });
  80. $('.js_email').one('click', function() {
  81. var email = ['thibault.', 'dupl', 'essis@', 'gmail.com'].join('');
  82. $(this).replaceWith($('<a/>').text(email).attr('href', 'mailto:'+email));
  83. });
  84. function loadUserLinks() {
  85. $('a.user_link:not(.qtiped)').each(function() {
  86. var $this = $(this).addClass("qtiped");
  87. $this.qtip({
  88. content: {
  89. text: $this.clone(),
  90. ajax: {
  91. loading: true,
  92. url: $this.attr("href").replace(/@/, "preview"),
  93. type: 'GET',
  94. cache: false,
  95. success: function(content, status) {
  96. this.set('content.text', content);
  97. $('body').trigger('lichess.content_loaded');
  98. }
  99. }
  100. },
  101. show: {
  102. effect: false,
  103. event: 'click'
  104. },
  105. hide: {
  106. effect: false,
  107. event: 'mouseleave',
  108. fixed: true,
  109. delay: 0
  110. },
  111. position: {
  112. my: 'top left',
  113. at: 'top left',
  114. target: $this,
  115. adjust: {
  116. x: -9,
  117. y: -6
  118. }
  119. }
  120. }).click(function() { return false; });
  121. });
  122. }
  123. loadUserLinks();
  124. $('body').on('lichess.content_loaded', loadUserLinks);
  125. function translateTexts() {
  126. $('.trans_me').each(function() {
  127. $(this).removeClass('trans_me').text($.trans($(this).text()));
  128. });
  129. }
  130. translateTexts();
  131. $('body').on('lichess.content_loaded', translateTexts);
  132. $.tipsyfy = function($elem) {
  133. $elem.find('a:not(div.game_list_inner a):not(.notipsy):not(#boardTable a), input, label, div.tipsyme, button').filter('[title]').tipsy({
  134. fade: true,
  135. html: false,
  136. live: true
  137. });
  138. };
  139. $.tipsyfy($('body'));
  140. if ($autocomplete = $('input.autocomplete').orNot()) {
  141. $autocomplete.autocomplete({
  142. source: $autocomplete.attr('data-provider'),
  143. minLength: 2,
  144. delay: 100
  145. });
  146. }
  147. $('a.toggle_signin').toggle(function() {
  148. $('#top').find('div.security').addClass('show_signin_form').find('input:first').focus();
  149. },
  150. function() {
  151. $('#top').find('div.security').removeClass('show_signin_form');
  152. });
  153. $('#lichess_message input[value=""]:first, #fos_user_registration_form_username').focus();
  154. $('#lichess_translation_form_code').change(function() {
  155. if ("0" != $(this).val()) {
  156. location.href = $(this).closest('form').attr('data-change-url').replace(/__/, $(this).val());
  157. }
  158. });
  159. $('#incomplete_translation a.close').one('click', function() {
  160. $(this).parent().remove();
  161. });
  162. $('a.delete, input.delete').click(function() {
  163. return confirm('Delete?');
  164. });
  165. $('input.confirm').click(function() {
  166. return confirm('Confirm this action?');
  167. });
  168. var elem = document.createElement('audio');
  169. var canPlayAudio = !! elem.canPlayType && elem.canPlayType('audio/ogg; codecs="vorbis"');
  170. $.playSound = function() {
  171. if (canPlayAudio && 'on' == $('body').attr('data-sound-enabled')) {
  172. var sound = $('#lichess_sound_player').get(0);
  173. sound.play();
  174. setTimeout(function() {
  175. sound.pause();
  176. },
  177. 1000);
  178. }
  179. }
  180. if (canPlayAudio) {
  181. $('body').append($('<audio id="lichess_sound_player">').attr('src', $('body').attr('data-sound-file')));
  182. $('#sound_state').click(function() {
  183. var $toggler = $(this);
  184. $.post($toggler.attr('href'), {},
  185. function(data) {
  186. $toggler.attr('class', 'sound_state_' + data);
  187. $('body').attr('data-sound-enabled', data);
  188. $.playSound();
  189. });
  190. return false;
  191. });
  192. $game && $game.trigger('lichess.audio_ready');
  193. } else {
  194. $('#sound_state').addClass('unavailable');
  195. }
  196. if (false || lichess.onProduction) {
  197. var homeUrl = $('#site_title').attr('href');
  198. setTimeout(function() {
  199. if ($gameSharing = $('div.game_share_widgets').orNot()) {
  200. $gameSharing.find('div.plusone_placeholder').replaceWith('<div class="lichess_plusone"><g:plusone size="medium" href="'+homeUrl+'"></g:plusone></div>');
  201. $gameSharing.find('div.facebook_placeholder').replaceWith('<div class="lichess_facebook"><iframe src="http://www.facebook.com/plugins/like.php?href=' + encodeURIComponent(homeUrl) + '&amp;layout=button_count&amp;show_faces=false&amp;width=110&amp;action=like&amp;font=lucida+grande&amp;colorscheme=light&amp;height=22"></iframe></div>');
  202. $.getScript('http://platform.twitter.com/widgets.js', function() {
  203. $gameSharing.addClass('loaded')
  204. });
  205. } else {
  206. $('div.lichess_social').append('<div class="lichess_facebook"><iframe src="http://www.facebook.com/plugins/like.php?href='+encodeURIComponent(homeUrl)+'%2F&amp;layout=button_count&amp;show_faces=false&amp;width=110&amp;action=like&amp;font=lucida+grande&amp;colorscheme=light&amp;height=22"></iframe></div><div><g:plusone size="medium" href="'+homeUrl+'"></g:plusone></div>');
  207. }
  208. $.getScript('https://apis.google.com/js/plusone.js');
  209. },
  210. 2000);
  211. }
  212. if(Boolean(window.chrome)) {
  213. $("div.addtochrome").show();
  214. }
  215. });
  216. $.fn.scrollable = function() {
  217. this.mousewheel(function(e, delta) {
  218. this.scrollTop -= delta * 30;
  219. return false;
  220. });
  221. };
  222. $.fn.orNot = function() {
  223. return this.length == 0 ? false: this;
  224. };
  225. $.trans = function(text) {
  226. return lichess_translations[text] ? lichess_translations[text] : text;
  227. }
  228. $.displayBoardMarks = function($board, isWhite) {
  229. if (isWhite) {
  230. var factor = 1;
  231. var base = 0;
  232. } else {
  233. var factor = - 1;
  234. var base = 575;
  235. }
  236. $board.find('span.board_mark').remove();
  237. var letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
  238. var marks = '';
  239. for (i = 1; i < 9; i++) {
  240. marks += '<span class="board_mark vert" style="bottom:' + (factor * i * 64 - 38 + base) + 'px;">' + i + '</span>';
  241. marks += '<span class="board_mark horz" style="left:' + (factor * i * 64 - 35 + base) + 'px;">' + letters[i - 1] + '</span>';
  242. }
  243. $board.append(marks);
  244. };
  245. if (lichess.onProduction) {
  246. //analytics
  247. var _gaq = _gaq || [];
  248. _gaq.push(['_setAccount', 'UA-7935029-3']);
  249. _gaq.push(['_trackPageview']);
  250. (function() {
  251. var ga = document.createElement('script');
  252. ga.type = 'text/javascript';
  253. ga.async = true;
  254. ga.src = 'http://www.google-analytics.com/ga.js';
  255. var s = document.getElementsByTagName('script')[0];
  256. s.parentNode.insertBefore(ga, s);
  257. })();
  258. }
  259. function urlToLink(text) {
  260. var exp = /\bhttp:\/\/[a-z]{0,3}\.(lichess\.org[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  261. return text.replace(exp,"<a href='http://$1'>$1</a>");
  262. }