bratFastMode.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // Brat fast mode : add a fast annotation mode for document level labelling
  3. //
  4. // Detect brat document : activate the extension only if brat detected
  5. try {
  6. is_brat = document.getElementById("mainlogo").textContent == "brat"
  7. } catch (e) {
  8. is_brat = false
  9. }
  10. if (is_brat == false) {
  11. exit()
  12. }
  13. // Script start here : if brat = true
  14. /*
  15. Event for fast annotation
  16. */
  17. //// Fast annotator :
  18. // Detect trigger word
  19. // If trigger word exist : raise an event of trigger on selected word
  20. // Fast annotator is triggered on each refresh of document
  21. function initiateFastAnnotator () {
  22. // Remove event listener
  23. document.removeEventListener("keyup", triggerLabel)
  24. // Search for trigger word id
  25. text_documents = document.querySelectorAll("tspan")
  26. for (i in text_documents) {
  27. if (text_documents[i].textContent == targetWord) {
  28. // Get word id
  29. word_id = text_documents[i].getAttribute("data-chunk-id");
  30. // Get word trigered
  31. triggerWord(word_id);
  32. break;
  33. }
  34. }
  35. }
  36. function fastAnnotator () {
  37. // The observer will check for content change and trigger fastAnnotator action
  38. observer = new MutationObserver(initiateFastAnnotator);
  39. observer.observe(document.getElementById("svg"), { attributes: true, childList: true, subtree: true });
  40. }
  41. function triggerWord (word_id) {
  42. // Display word triggering
  43. node = document.querySelector("tspan[data-chunk-id='"+word_id+"']")
  44. // Adding CSS rule
  45. if (typeof(observer) != 'undefined') {
  46. observer.disconnect();
  47. }
  48. node.classList.add("triggerWord")
  49. fastAnnotator();
  50. // Trigger label event
  51. document.addEventListener("keyup", triggerLabel)
  52. }
  53. /*
  54. Function for page interaction
  55. */
  56. function createEvent(node, eventType) {
  57. var x = node.getBoundingClientRect()["x"];
  58. var y = node.getBoundingClientRect()["y"];
  59. var x_coor = x+node.getBoundingClientRect()["width"]/2;
  60. var y_coor = y+node.getBoundingClientRect()["height"]/2;
  61. if (eventType == 'mousedown') {
  62. var x_start = x;
  63. var y_start = y;
  64. } else {
  65. var x_start = x+node.getBoundingClientRect()["width"]-1;
  66. var y_start = y+node.getBoundingClientRect()["height"]-1;
  67. }
  68. var evt = new MouseEvent(eventType, {
  69. bubbles: true,
  70. view: window,
  71. altKey: false,
  72. ctrlKey: false,
  73. metaKey: false,
  74. shiftKey: false,
  75. clientX: x_start,
  76. clientY: y_start,
  77. movementX: 0,
  78. movementY: 0,
  79. });
  80. return(evt)
  81. }
  82. function simulateClick(mouseClickList, obj) {
  83. for (clickValue in mouseClickList) {
  84. node.dispatchEvent(createEvent(obj, mouseClickList[clickValue]));
  85. }
  86. }
  87. function SelectText(text) {
  88. var selection = window.getSelection();
  89. var range = document.createRange();
  90. range.selectNodeContents(text);
  91. selection.removeAllRanges();
  92. selection.addRange(range);
  93. }
  94. function triggerLabel (e) {
  95. if (["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","z","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"].indexOf(e.key) != -1) {
  96. // Simulate click
  97. node = document.querySelector("tspan[data-chunk-id='"+word_id+"']")
  98. SelectText(node)
  99. simulateClick(["mousedown","mouseup","dblclick"], node)
  100. // Simulate label selection
  101. // Select label
  102. document.dispatchEvent(new KeyboardEvent('keydown', {
  103. 'keyCode':e.keyCode
  104. }));
  105. // Click on ok button
  106. document.getElementById("span_form-ok").click()
  107. // Go to next document
  108. document.getElementById("next").click()
  109. }
  110. }
  111. /*
  112. UI Creation
  113. */
  114. //// Select target word
  115. function createWordSelectionUI () {
  116. // Hiding existing title
  117. var currentTitle = document.querySelector("#document_name input")
  118. currentTitle.classList.add("hidden") // Hiding existing
  119. // Adding new title
  120. var newTitle = document.createElement("input");
  121. currentTitle.parentNode.insertBefore(newTitle, currentTitle.nextSibling);
  122. newTitle.classList.add("ui-widget-header");
  123. newTitle.classList.add("targetWordSelection");
  124. newTitle.setAttribute("readonly","readonly");
  125. newTitle.setAttribute("value","Select the target word");
  126. newTitle.id = 'wordSelection';
  127. }
  128. function destroyWordSelectionUI () {
  129. // Displaying existing title
  130. var currentTitle = document.querySelector("#document_name input");
  131. currentTitle.classList.remove("hidden"); // Hiding existing
  132. // Remove new title
  133. var newTitle = document.getElementById("wordSelection");
  134. newTitle.remove();
  135. }
  136. function selectTargetWord () {
  137. /*
  138. Get UI for target word selection
  139. The target word is then stored in a global variable
  140. */
  141. // UI Creation
  142. createWordSelectionUI()
  143. // Event trigger
  144. document.getElementsByClassName("text")[0].addEventListener("click", function (e) {
  145. // Getting trigger word
  146. targetWord = e.target.textContent;
  147. // Removing UI
  148. destroyWordSelectionUI();
  149. // Getting fastAnnotator enabled
  150. initiateFastAnnotator();
  151. }, { "once":true })
  152. }
  153. //// Creating main UI button
  154. // Functions relative to the button
  155. function activateButton (buttonElement) {
  156. // Set has activate
  157. buttonNode.setAttribute("activate", true)
  158. // Change button class
  159. buttonNode.classList.add("activate")
  160. buttonNode.classList.remove("desactivate")
  161. // Get target word
  162. var targetWord = selectTargetWord();
  163. }
  164. function inactivateButton (buttonElement) {
  165. // Set has activate
  166. buttonNode.setAttribute("activate", false)
  167. // Change button class
  168. buttonNode.classList.remove("activate")
  169. buttonNode.classList.add("desactivate")
  170. // Remove observer
  171. if (typeof(observer) != 'undefined') {
  172. observer.disconnect()
  173. }
  174. document.removeEventListener("keyup", triggerLabel)
  175. }
  176. // Button initiation
  177. var buttonName = "FM";
  178. var buttonId = "bratFastModeUI";
  179. var buttonNode = document.createElement("div");
  180. var nextNode = document.getElementById("navbuttons");
  181. nextNode.parentNode.insertBefore(buttonNode, nextNode)
  182. buttonNode.id = buttonId;
  183. buttonNode.textContent = buttonName;
  184. buttonNode.classList.add("unselectable");
  185. inactivateButton(buttonNode);
  186. buttonNode.addEventListener("click", function(e) {
  187. var node = e.target;
  188. if (node.attributes.activate.value == "true") {
  189. inactivateButton(node);
  190. } else {
  191. activateButton(node);
  192. }
  193. })
  194. // Dark mode
  195. //// Creating dark mode UI button
  196. // Functions relative to the button
  197. function activateDM (buttonElement) {
  198. // Set has activate
  199. buttonElement.setAttribute("activate", true)
  200. // Change button class
  201. buttonElement.classList.add("activate")
  202. buttonElement.classList.remove("desactivate")
  203. darkMode_trigger(true);
  204. }
  205. function inactivateDM (buttonElement) {
  206. // Set has activate
  207. buttonElement.setAttribute("activate", false)
  208. // Change button class
  209. buttonElement.classList.remove("activate")
  210. buttonElement.classList.add("desactivate")
  211. darkMode_trigger(false);
  212. }
  213. function darkMode_trigger (dm_state) {
  214. // New CSS rules
  215. // Initiate observer
  216. if (dm_state == true) {
  217. document.getElementById("svg").classList.add("svg_dark")
  218. } else {
  219. document.getElementById("svg").classList.remove("svg_dark")
  220. }
  221. }
  222. var darkModeName = "DM";
  223. var darkModeId = "bratDarkMode";
  224. var darkMode = document.createElement("div");
  225. var nextNode = document.getElementById("bratFastModeUI");
  226. nextNode.parentNode.insertBefore(darkMode, nextNode)
  227. darkMode.id = darkModeId;
  228. darkMode.textContent = darkModeName;
  229. darkMode.classList.add("unselectable");
  230. inactivateDM(darkMode);
  231. darkMode.addEventListener("click", function(e) {
  232. var node = e.target;
  233. if (node.attributes.activate.value == "true") {
  234. inactivateDM(darkMode);
  235. } else {
  236. activateDM(darkMode);
  237. }
  238. })