/js/formernotebook.js

https://github.com/amundo/notebook · JavaScript · 241 lines · 211 code · 27 blank · 3 comment · 0 complexity · ebfcee7b03126281b0040c0cc733ad2f MD5 · raw file

  1. $(function(){
  2. window.key = localStorage.notebook;
  3. /*window.data = JSON.parse(localStorage['notebook-' + window.key]);*/
  4. window.data = [
  5. {
  6. "sentence": "Ni xi\u01ceng y\u00e0o chi sh\u00e9nme?",
  7. "translation": "What do you want to eat? "
  8. },
  9. {
  10. "sentence": "w\u01d2 xi\u01ceng y\u00e0o xu\u00e9 P\u01d4t\u014dnghu\u00e0.",
  11. "translation": "I want to learn Putonghua."
  12. },
  13. {
  14. "sentence": "w\u01d2 shu\u01d2 de du\u00ec m\u0101?",
  15. "translation": "Did I say it correctly?"
  16. },
  17. {
  18. "sentence": "zh\u00e8 sh\u00ec sh\u00e9nme?",
  19. "translation": "What is this?"
  20. },
  21. {
  22. "sentence": "N\u01d0 n\u00e9ng za\u00ec shu\u01d2 bi\u00e0n m\u00e1?",
  23. "translation": "Please say that again."
  24. },
  25. {
  26. "sentence": "N\u00ed ji\u00e0o sh\u00e9nme m\u00edng z\u00ec?",
  27. "translation": "What is your name? Como te llamas?"
  28. },
  29. {
  30. "sentence": "w\u01d2 de m\u00edng z\u00ec shi Patrick.",
  31. "translation": "My name is Patrick."
  32. },
  33. {
  34. "sentence": "n\u01d0 de m\u00edng z\u00ec",
  35. "translation": "your name"
  36. },
  37. {
  38. "sentence": "de ho er jian ",
  39. "translation": "See you later. "
  40. },
  41. {
  42. "sentence": "w\u01d2 x\u01d0 hu\u0101n k\u0101f\u0113i.",
  43. "translation": "I like coffee."
  44. },
  45. {
  46. "sentence": "w\u01d2 b\u00f9 x\u01d0 hu\u0101n ch\u00e1.",
  47. "translation": "I don't like tea."
  48. },
  49. {
  50. "sentence": "b\u00f9 h\u01ceo y\u00ec s\u012b w\u01d2 b\u00f9 x\u01d0 hu\u0101n ch\u00e1.",
  51. "translation": "Excuse me, but I don't like tea."
  52. },
  53. {
  54. "sentence": "k\u00e0n sh\u016b",
  55. "translation": "see book (read for pleasure)"
  56. },
  57. {
  58. "sentence": "d\u00fa sh\u016b",
  59. "translation": "read (study book)"
  60. },
  61. {
  62. "sentence": "ni\u00e0n sh\u016b",
  63. "translation": "read (study book; Taiwan)"
  64. },
  65. {
  66. "sentence": "h\u00e8n ji\u00f2u",
  67. "translation": "very old"
  68. },
  69. {
  70. "sentence": "b\u012bng",
  71. "translation": "cold (to the touch)"
  72. },
  73. {
  74. "sentence": "j\u016b r\u00f2u",
  75. "translation": "pork (pig meat)"
  76. },
  77. {
  78. "sentence": "r\u00f2u",
  79. "translation": "meat"
  80. },
  81. {
  82. "sentence": "w\u01d2 b\u00f9 ch\u0113 r\u00f2u",
  83. "translation": "I don't eat meat."
  84. },
  85. {
  86. "sentence": "m\u00e0ng",
  87. "translation": "busy"
  88. },
  89. {
  90. "sentence": "w\u01cen",
  91. "translation": "play"
  92. },
  93. {
  94. "sentence": "d\u01ce l\u00e1n qi\u00fa",
  95. "translation": "play basketball"
  96. },
  97. {
  98. "sentence": "d\u01ce",
  99. "translation": "to beat"
  100. },
  101. {
  102. "sentence": "l\u00e1n",
  103. "translation": "basket"
  104. },
  105. {
  106. "sentence": "qi\u00fa",
  107. "translation": "ball"
  108. },
  109. {
  110. "sentence": "z\u00fa qi\u00fa",
  111. "translation": "football"
  112. },
  113. {
  114. "sentence": "z\u00fa",
  115. "translation": "foot"
  116. },
  117. {
  118. "sentence": "g\u01cen l\u01cen qi\u00fa",
  119. "translation": "American football"
  120. },
  121. {
  122. "sentence": "b\u00e0ng qi\u00fa",
  123. "translation": "baseball"
  124. },
  125. {
  126. "sentence": "b\u00e0ng",
  127. "translation": "stick"
  128. },
  129. {
  130. "sentence": "m\u011bi sh\u00ec z\u00fa qi\u00fa",
  131. "translation": "American style football"
  132. }
  133. ];
  134. window.Sentence = Backbone.Model.extend({
  135. tokenize: function(){
  136. var words = this.get('sentence').split(' ');
  137. this.set({words: words});
  138. console.log(this.get('sentence').split(' '));
  139. }
  140. });
  141. window.Text = Backbone.Collection.extend({
  142. model : Sentence,
  143. localStorage : new Store('notebook')
  144. });
  145. window.SentenceView = Backbone.View.extend({
  146. tagName: 'li',
  147. events : {
  148. //'click' : 'logModel'
  149. 'dblclick' : 'editInPlace'
  150. },
  151. initialize : function(){
  152. _.bindAll(this, 'render');
  153. },
  154. editInPlace : function(ev){
  155. console.log(this.model.get('sentence'));
  156. },
  157. render : function(){
  158. var rendered = _.template('<ol class=phrase><li class=sentence> <%= sentence %></li><li class=translation> <%= translation %> </li></ol>', this.model.toJSON());
  159. $(this.el).html(rendered);
  160. return this;
  161. }
  162. })
  163. window.SentenceEditorView = Backbone.View.extend({
  164. el: '#sentenceEditor',
  165. events : {
  166. 'keyup #plain' : 'transliterate',
  167. 'submit' : 'addPhrase'
  168. },
  169. initialize : function(){
  170. _.bindAll(this, 'transliterate', 'addPhrase');
  171. },
  172. addPhrase : function(ev){
  173. ev.preventDefault();
  174. var sentence = new Sentence({});
  175. sentence.set({
  176. 'sentence': this.$('.sentence').val(),
  177. 'translation': this.$('.translation').val(),
  178. });
  179. this.collection.add(sentence);
  180. this.$('input').val('').first().focus();
  181. },
  182. transliterate : function(ev){
  183. var PinyinTransliterator = new Transliterator({rules: PinyinRules});
  184. var transliterated = PinyinTransliterator.convert($(ev.target).val());
  185. this.$('input#plain').val(transliterated);
  186. }
  187. })
  188. window.TextView = Backbone.View.extend({
  189. el : $('#notebook') ,
  190. initialize : function(){
  191. _.bindAll(this, 'render');
  192. //this.collection.bind('add', this.render);
  193. this.collection.bind('add', this.render, this)
  194. },
  195. render : function(){
  196. this.$('#sentences').empty();
  197. this.collection.each(function(sentence){
  198. var view = new SentenceView({model: sentence});
  199. this.$('ul#sentences').prepend(view.render().el);
  200. });
  201. return this;
  202. }
  203. });
  204. Notebook = {};
  205. Notebook.text = new Text({ });
  206. Notebook.text.reset(data);
  207. Notebook.textView = new TextView({
  208. collection: Notebook.text
  209. });
  210. Notebook.sentenceEditorView = new SentenceEditorView({
  211. collection: Notebook.text
  212. });
  213. Notebook.textView.render();
  214. })