game.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. ErrorLevel = {
  2. error: 0,
  3. warning: 1,
  4. ok: 2
  5. };
  6. /* fix for popovers */
  7. $('body').on('hidden.bs.popover', function (e) {
  8. $(e.target).data("bs.popover").inState.click = false;
  9. });
  10. /* set help menu to screen height */
  11. $('.footer-container').css('max-height', $(window).height() * 0.8);
  12. $(document).ready(function () {
  13. console.log("ready");
  14. $.ajaxSetup({
  15. headers: {
  16. 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  17. }
  18. });
  19. /*enable click button in popover */
  20. $(document).on('click', ".words-categories-button", function () {
  21. console.log("click sur autres categories");
  22. get_words_postags(null);
  23. $(this).css('display', 'none');
  24. });
  25. /* enable click on rows in categories tables */
  26. $(document).on('click', ".table > tbody > tr", function () {
  27. console.log("in click on categories table");
  28. var row = $(this).find('td:first');
  29. $('.selected').removeClass('is-in-error');
  30. var category = $('.selected').parent().find('.category-label');
  31. category.text(row.text());
  32. category.attr('id', row.attr('id'));
  33. category.show();
  34. $('.word.selected').popover('hide');
  35. });
  36. $('.main-button').click(function () {
  37. annotations = [];
  38. $('.word-container').each(function (index, word_container) {
  39. annotation = {};
  40. annotation['postag_id'] = $(word_container).find('.category-label').attr('id');
  41. annotation['word_id'] = $(word_container).find('.word').attr('id');
  42. if (annotation['postag_id']) {
  43. annotations.push(annotation);
  44. }
  45. });
  46. $.ajax({
  47. method: 'PUT',
  48. context: $("#main-container"),
  49. data: {
  50. annotations: annotations
  51. },
  52. success: function (response) {
  53. if (response) {
  54. if (response.constructor === Array) {
  55. console.log("FALSE");
  56. render_correction(response);
  57. } else {
  58. console.log("ANNOTATION CREATED");
  59. $('#message').hide();
  60. $("#sentence-container").html(response);
  61. reload_javascript_on_words();
  62. }
  63. } else {
  64. window.location.href = '/home';
  65. }
  66. }
  67. });
  68. });
  69. function reload_javascript_on_words() {
  70. console.log("reload");
  71. console.log("loading postags");
  72. $('.word').each(function () {
  73. word_id = $(this).prop('id')
  74. if (!/^[!"#$%&'()*+, \-./:;<=>?@ [\\\]^_`{|}~„“]$/.test($(this).attr('value'))) {
  75. get_words_postags($(this).attr('id'));
  76. $(this).addClass('not-punct');
  77. console.log("pretag");
  78. console.log($(this).attr('tag'))
  79. if ($(this).attr('tag')) {
  80. $('#right_' + word_id).css({'display': 'inline'});
  81. $('#left_' + word_id).css({'display': 'inline'});
  82. $('#right_' + word_id).css({'visibility': 'hidden'});
  83. $('#left_' + word_id).css({'visibility': 'hidden'});
  84. var category = $(this).parent().find('.category-label');
  85. category.addClass('auto-annotated');
  86. category.text($(this).attr('tag'));
  87. category.show();
  88. } else {
  89. $('#question_' + word_id).css({'display': 'inline'});
  90. }
  91. }
  92. });
  93. $('.word').not(".is-correct").hover(
  94. function () {
  95. if (!/^[!"#$%&'()*+, \-./:;<=>?@ [\\\]^_`{|}~„“]$/.test($(this).attr('value'))) {
  96. $(this).addClass('highlighted');
  97. }
  98. },
  99. function () {
  100. if (!/^[!"#$%&'()*+, \-./:;<=>?@ [\\\]^_`{|}~„“]$/.test($(this).attr('value'))) {
  101. $(this).removeClass('highlighted');
  102. }
  103. }
  104. );
  105. $('.word').not(".is-correct").click(
  106. function () {
  107. if (!/^[!"#$%&'()*+, \-./:;<=>?@ [\\\]^_`{|}~„“]$/.test($(this).attr('value'))) {
  108. $('.word').removeClass('selected');
  109. $('.word').removeClass('is-in-error');
  110. $(this).addClass('selected');
  111. $('.sentence-main-container').width('60%');
  112. }
  113. }
  114. );
  115. console.log("loading js on labels");
  116. $('.word').hover(
  117. function () {
  118. word_id = $(this).attr('id').match(/[0-9]+/);
  119. console.log(word_id);
  120. $(this).css('display', 'block');
  121. $('#right_' + word_id).css({'visibility': 'visible'});
  122. $('#left_' + word_id).css({'visibility': 'visible'});
  123. }
  124. );
  125. $('.word').mouseleave(function () {
  126. word_id = $(this).attr('id').match(/[0-9]+/);
  127. console.log(word_id);
  128. $('#right_' + word_id).css({'visibility': 'hidden'});
  129. $('#left_' + word_id).css({'visibility': 'hidden'});
  130. }
  131. );
  132. $('.labels').hover(
  133. function () {
  134. word_id = $(this).attr('name').match(/[0-9]+/);
  135. console.log(word_id);
  136. $(this).css('display', 'block');
  137. $('#right_' + word_id).css({'visibility': 'visible'});
  138. $('#left_' + word_id).css({'visibility': 'visible'});
  139. }
  140. );
  141. $('.labels').mouseleave(function () {
  142. word_id = $(this).attr('name').match(/[0-9]+/);
  143. console.log(word_id);
  144. $('#right_' + word_id).css({'visibility': 'hidden'});
  145. $('#left_' + word_id).css({'visibility': 'hidden'});
  146. }
  147. );
  148. $('.question-label').click(function () {
  149. $('.word.selected').popover('hide');
  150. $('.word').removeClass('selected');
  151. var category = $(this).parent().find('.category-label');
  152. category.empty();
  153. category.removeClass('auto-annotated');
  154. word_id = $(this).attr('id').match(/[0-9]+/);
  155. $('#' + word_id).addClass('selected');
  156. $('#' + word_id).popover('show');
  157. });
  158. $('.leftlabel').click(function () {
  159. word_id = $(this).attr('id').match(/[0-9]+/);
  160. $('.word.selected').popover('hide');
  161. $('.word').removeClass('selected');
  162. $('#right_' + word_id).css({'display': 'none'});
  163. $('#left_' + word_id).css({'display': 'none'});
  164. var category = $(this).parent().find('.category-label');
  165. category.empty();
  166. category.removeClass('auto-annotated');
  167. $('#' + word_id).addClass('selected');
  168. $('#' + word_id).popover('show');
  169. $('#' + word_id).removeClass('auto-annotated');
  170. });
  171. $('.rightlabel').click(function () {
  172. word_id = $(this).attr('id').match(/[0-9]+/);
  173. $('.word.selected').popover('hide');
  174. $('.word').removeClass('selected');
  175. $('#right_' + word_id).css({'display': 'none'});
  176. $('#left_' + word_id).css({'display': 'none'});
  177. $('#' + word_id).addClass('selected');
  178. $('#' + word_id).removeClass('auto-annotated');
  179. var category = $(this).parent().find('.category-label');
  180. category.text($('#'+word_id).attr('tag'));
  181. category.attr('id', $('#'+word_id).attr('potential_tag_id'));
  182. category.removeClass('auto-annotated');
  183. });
  184. /* enable click on rows in categories tables */
  185. add_on_click_on_categories_table()
  186. // $('#tags').on('keyup', function (e) {
  187. // var tagElems = $('#tag-help').children(".panel");
  188. // console.log("tagElems");
  189. // console.log(tagElems);
  190. // for (var i = 0; i < tagElems.length; i++) {
  191. // var look = "<b>" + $(this).val().toLowerCase() + "</b>";
  192. // var panel_content = $(tagElems).eq(i).text().toLowerCase();
  193. // if (panel_content.search(look) !== -1) {
  194. // console.log(look);
  195. // console.log(panel_content);
  196. // console.log(panel_content.search(look));
  197. // panel_to_show = $(tagElems).eq(i);
  198. // }
  199. // }
  200. // panel_to_show.collapse("show");
  201. //
  202. // });
  203. }
  204. ;
  205. function render_correction(answers) {
  206. postags_descriptions = [];
  207. postags_full_names = [];
  208. postags_names = [];
  209. error_status = ErrorLevel.error;
  210. for (var i = 0; i < answers.length; i++) {
  211. if (answers[i]['is_correct'] === true) {
  212. word = $('#' + answers[i]['word_id'] + '.word');
  213. word.removeClass('is-in-error').addClass('is-correct');
  214. error_status = ErrorLevel.warning;
  215. word.removeClass('highlighted');
  216. word.off('click');
  217. word.unbind('mouseenter mouseleave');
  218. } else {
  219. $('#' + answers[i]['word_id'] + '.word').removeClass('is-correct').addClass('is-in-error');
  220. if (postags_names.indexOf(answers[i]['postag_name']) === -1) {
  221. postags_descriptions.push(answers[i]['postag_description']);
  222. postags_full_names.push(answers[i]['postag_full_name']);
  223. postags_names.push(answers[i]['postag_name']);
  224. }
  225. }
  226. }
  227. if (postags_descriptions.length > 0) {
  228. error_status = ErrorLevel.error;
  229. }
  230. show_message(error_status, postags_names, postags_full_names, postags_descriptions);
  231. }
  232. function create_table_with_postags(postags) {
  233. var content = '';
  234. for (var i = 0; i < postags.length; i++) {
  235. content += '<tr data-trigger="hover" title="Exemples" data-container="body" data-placement="left" data-toggle="popover" data-content="' + postags[i]['description'] + '">';
  236. content += '<td id=' + postags[i]['id'] + '>' + postags[i]['name'];
  237. content += '<span class=full-name-category> (' + postags[i]['full_name'] + ') ';
  238. content += '</span>';
  239. content += '</td>';
  240. content += '</tr>';
  241. }
  242. return content;
  243. }
  244. function create_table_with_postags_on_word(postags, word_id) {
  245. var content = '';
  246. content += '<table class="table table-hover categories-table" id="categories-table[' + word_id + ']" style="display: table;">';
  247. content += '<thead>';
  248. content += '<tr>';
  249. content += '<th class="ostrich" style="text-align: center"> <h3> <b>Categories grammaticales</b></h3></th>';
  250. content += '</tr>';
  251. content += '</thead>';
  252. content += '<tbody>';
  253. for (var i = 0; i < postags.length; i++) {
  254. // content += '<tr data-trigger="hover" title="Exemples" data-container="body" data-placement="left" data-toggle="popover" data-content="' + postags[i]['description'] + '">';
  255. content += '<tr>';
  256. content += '<td id=' + postags[i]['id'] + '>' + postags[i]['name'];
  257. content += '<span class=full-name-category> (' + postags[i]['full_name'] + ') ';
  258. content += '</span>';
  259. content += '</td>';
  260. content += '</tr>';
  261. }
  262. content += '</tbody> </table>';
  263. content += '<div class="words-categories-button">'
  264. content += '<button>Aucune de ces catégories ne convient</button>'
  265. content += '</div>'
  266. return content;
  267. }
  268. function create_errors_content(postag_names, postag_full_names, postag_descriptions) {
  269. var content = '';
  270. content += '<ul>';
  271. for (var i = 0; i < postag_descriptions.length; i++) {
  272. content += '<li><p>';
  273. content += postag_names[i];
  274. content += ' (<i>';
  275. content += postag_full_names[i];
  276. content += ' </i>) :';
  277. content += ' <h4 style="text-align:center"> Quelques exemples <h4> <br> ';
  278. content += postag_descriptions[i];
  279. content += '</p></li>';
  280. }
  281. content += '</ul>';
  282. return content;
  283. }
  284. function show_message(is_in_error, postag_names, postag_full_names, postag_descriptions) {
  285. switch (is_in_error) {
  286. case ErrorLevel.error:
  287. if (postag_names.length > 0) {
  288. $('#message-title').text("Vous avez des erreurs. Rappel sur les catégories que vous avez choisies :");
  289. } else {
  290. /* no word has been annotated yet */
  291. $('#message-title').text("Vous ne pouvez pas passer la phrase dans le mode entraînement");
  292. /* Je veux des messages différents en fonction du type de jeu
  293. * $('#message-title').text("Vous devez annoter au moins un mot !");
  294. */
  295. }
  296. var content = create_errors_content(postag_names, postag_full_names, postag_descriptions);
  297. $('#message-content').empty().append(content)
  298. $('#message').removeClass('alert-success alert-warning').addClass('alert-danger');
  299. break;
  300. case ErrorLevel.warning:
  301. $('#message-title').text("Bravo !");
  302. $('#message-content').text("Annotez les mots restants pour pouvoir passer à la phrase suivante.");
  303. $('#message').removeClass('alert-success alert-danger').addClass('alert-warning');
  304. break;
  305. case ErrorLevel.success:
  306. $('#message-title').text("Bravo !");
  307. $('#message-content').text("Vous avez tout bon");
  308. $('#message').removeClass('alert-danger alert-warning').addClass('alert-success');
  309. break;
  310. }
  311. $('#message').show();
  312. }
  313. function get_words_postags(word_id) {
  314. $.ajax({
  315. method: 'GET',
  316. url: "/postags",
  317. dataType: 'json',
  318. data: {
  319. word_id: word_id
  320. },
  321. success: function (response) {
  322. console.log("in get_words_postags");
  323. var content_words = create_table_with_postags_on_word(response['postags'], word_id);
  324. /* used to fill categories when all categories displayed */
  325. var content = create_table_with_postags(response['postags']);
  326. $('.categories-table').find('tbody').empty().append(content);
  327. // if (!response['all_categories']) {
  328. // $('.categories-button').show();
  329. // } else {
  330. // $('.categories-button').hide();
  331. // }
  332. $('#' + word_id).popover({
  333. placement: 'bottom',
  334. html: true,
  335. animation: true,
  336. content: content_words,
  337. }).popover();
  338. $('#' + word_id).on('click', function () {
  339. $('.word').not(this).popover('hide');
  340. });
  341. console.log("end get_words_postags");
  342. add_on_click_on_categories_table();
  343. }
  344. });
  345. }
  346. function add_on_click_on_categories_table() {
  347. // $('.categories-table').find('tr').click(function () {
  348. // console.log("in click on categories table");
  349. // var row = $(this).find('td:first');
  350. // $('.selected').removeClass('is-in-error');
  351. // var category = $('.selected').parent().find('.category-label');
  352. // category.text(row.text());
  353. // category.attr('id', row.attr('id'));
  354. // category.show();
  355. // $('.word.selected').popover('hide');
  356. // });
  357. $(document).on('click', ".table > tbody > tr", function () {
  358. console.log("in click on categories table");
  359. var row = $(this).find('td:first');
  360. $('.selected').removeClass('is-in-error');
  361. word_id = $('.selected').attr('id').match(/[0-9]+/);
  362. var category = $('.selected').parent().find('.category-label');
  363. var question = $('.selected').parent().find('.question-label');
  364. category.text(row.text().split(" ")[0]);
  365. category.attr('id', row.attr('id'));
  366. category.show();
  367. category.removeClass('auto-annotated');
  368. question.css({'display': 'none'});
  369. $('.word.selected').popover('hide');
  370. $('#right_' + word_id).css({'display': 'none'});
  371. $('#left_' + word_id).css({'display': 'none'});
  372. });
  373. }
  374. /* js for accordion help menu */
  375. var acc = document.getElementsByClassName("accordion");
  376. var i;
  377. $(acc[0]).css({'border-top-left-radius': '10px', 'border-top-right-radius': '10px'});
  378. $(acc[acc.length - 1]).css({'border-bottom-left-radius': '10px', 'border-bottom-right-radius': '10px'});
  379. var toggle_acc = false;
  380. $(acc[acc.length - 1]).on('click', function () {
  381. if (toggle_acc == false) {
  382. $(acc[acc.length - 1]).css({'border-bottom-left-radius': '0px', 'border-bottom-right-radius': '0px'})
  383. toggle_acc = true;
  384. } else {
  385. $(acc[acc.length - 1]).css({'border-bottom-left-radius': '10px', 'border-bottom-right-radius': '10px'})
  386. toggle_acc = false;
  387. }
  388. });
  389. for (i = 0; i < acc.length; i++) {
  390. acc[i].onclick = function () {
  391. this.classList.toggle("active");
  392. this.nextElementSibling.classList.toggle("show");
  393. }
  394. }
  395. reload_javascript_on_words();
  396. });