not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
7.1 KiB

  1. // Class for a password field: let user choose into a bunch of small images
  2. enyo.kind({
  3. name: "Sugar.Password",
  4. kind: enyo.Control,
  5. published: {
  6. label: '',
  7. password: '',
  8. },
  9. events: { onEnter: "" },
  10. classes: "password-class",
  11. components: [
  12. {name: "line", classes: "password-line", components: [
  13. {name: "text", content: "xxx", classes: "password-label"},
  14. {classes: "password-input", components: [
  15. {name: "pass", classes: "password-value", content: "", allowHtml: true},
  16. {name: "cancel", classes: "password-iconcancel", showing: false, ontap: "cancelClicked"},
  17. ]},
  18. {components:[
  19. {name: "emojis", classes: "password-emojis", components: [
  20. {components: [
  21. {name: "emoji0", kind: "Sugar.Emoji", index: 0, ontap: "emojiClicked", selected: true},
  22. {name: "emoji1", kind: "Sugar.Emoji", index: 1, ontap: "emojiClicked", selected: true},
  23. {name: "emoji2", kind: "Sugar.Emoji", index: 2, ontap: "emojiClicked", selected: true},
  24. {name: "emoji3", kind: "Sugar.Emoji", index: 3, ontap: "emojiClicked", selected: true},
  25. {name: "emoji4", kind: "Sugar.Emoji", index: 4, ontap: "emojiClicked", selected: true},
  26. ]},
  27. {components: [
  28. {name: "emoji5", kind: "Sugar.Emoji", index: 5, ontap: "emojiClicked", selected: true},
  29. {name: "emoji6", kind: "Sugar.Emoji", index: 6, ontap: "emojiClicked", selected: true},
  30. {name: "emoji7", kind: "Sugar.Emoji", index: 7, ontap: "emojiClicked", selected: true},
  31. {name: "emoji8", kind: "Sugar.Emoji", index: 8, ontap: "emojiClicked", selected: true},
  32. {name: "emoji9", kind: "Sugar.Emoji", index: 9, ontap: "emojiClicked", selected: true},
  33. ]},
  34. ]},
  35. {classes: "password-emojis-category", components: [
  36. {name: "category0", kind: "Sugar.Emoji", classes: "emoji-category", ontap: "category0Clicked", index: 0, letter: false, size: 15, selected: true},
  37. {name: "category1", kind: "Sugar.Emoji", classes: "emoji-category", ontap: "category1Clicked", index: 10, letter: false, size: 15},
  38. {name: "category2", kind: "Sugar.Emoji", classes: "emoji-category", ontap: "category2Clicked", index: 20, letter: false, size: 15}
  39. ]}
  40. ]}
  41. ]},
  42. ],
  43. // Constructor
  44. create: function() {
  45. this.inherited(arguments);
  46. this.labelChanged();
  47. this.listen = false;
  48. this.password = "";
  49. var that = this;
  50. document.addEventListener('keydown', function(event) {
  51. if (that.listen) {
  52. if (event.keyCode === 13) {
  53. that.doEnter();
  54. return true;
  55. } else if (event.keyCode == 8) {
  56. that.removeCharacter();
  57. return true;
  58. } else {
  59. that.addCharacter(event.key);
  60. return true;
  61. }
  62. return false;
  63. }
  64. return false;
  65. });
  66. },
  67. startInputListening: function() {
  68. this.listen = true;
  69. },
  70. stopInputListening: function() {
  71. this.listen = false;
  72. },
  73. // Property changed
  74. labelChanged: function() {
  75. this.$.text.setContent(this.label);
  76. },
  77. passwordChanged: function() {
  78. this.drawPassword();
  79. if (this.password.length == 0) {
  80. this.$.cancel.setShowing(false);
  81. }
  82. },
  83. // Event handling
  84. emojiClicked: function(emoji) {
  85. emoji.animate();
  86. this.addCharacter(constant.emojis[emoji.getIndex()].letter);
  87. },
  88. category0Clicked: function(category) {
  89. if (category.getSelected()) {
  90. return;
  91. }
  92. var categoryIndex = category.index;
  93. if (categoryIndex - 10 >= 0) {
  94. // Scroll categories
  95. this.$.category0.setIndex(categoryIndex-10);
  96. this.$.category1.setIndex(categoryIndex);
  97. this.$.category2.setIndex(categoryIndex+10);
  98. category = this.$.category1;
  99. }
  100. this.changeCategory(category);
  101. },
  102. category1Clicked: function(category) {
  103. if (category.getSelected()) {
  104. return;
  105. }
  106. this.changeCategory(category);
  107. },
  108. category2Clicked: function(category) {
  109. if (category.getSelected()) {
  110. return;
  111. }
  112. var categoryIndex = category.index;
  113. if (categoryIndex + 10 < constant.emojis.length) {
  114. // Scroll categories
  115. this.$.category0.setIndex(categoryIndex-10);
  116. this.$.category1.setIndex(categoryIndex);
  117. this.$.category2.setIndex(categoryIndex+10);
  118. category = this.$.category1;
  119. }
  120. this.changeCategory(category);
  121. },
  122. changeCategory: function(category) {
  123. this.$.category0.setSelected(false);
  124. this.$.category1.setSelected(false);
  125. this.$.category2.setSelected(false);
  126. category.setSelected(true);
  127. var startIndex = category.index;
  128. this.$.emoji0.setIndex(startIndex++);
  129. this.$.emoji1.setIndex(startIndex++);
  130. this.$.emoji2.setIndex(startIndex++);
  131. this.$.emoji3.setIndex(startIndex++);
  132. this.$.emoji4.setIndex(startIndex++);
  133. this.$.emoji5.setIndex(startIndex++);
  134. this.$.emoji6.setIndex(startIndex++);
  135. this.$.emoji7.setIndex(startIndex++);
  136. this.$.emoji8.setIndex(startIndex++);
  137. this.$.emoji9.setIndex(startIndex++);
  138. },
  139. cancelClicked: function() {
  140. this.password = "";
  141. this.drawPassword();
  142. this.$.cancel.setShowing(false);
  143. },
  144. // Handle password changed
  145. drawPassword: function() {
  146. var html = "";
  147. for (var i = 0 ; i < this.password.length ; i++) {
  148. for (var j = 0 ; j < constant.emojis.length ; j++) {
  149. if (constant.emojis[j].letter == this.password[i]) {
  150. html += "&#x"+constant.emojis[j].value+";";
  151. }
  152. }
  153. }
  154. this.$.pass.setContent(html);
  155. },
  156. addCharacter: function(char) {
  157. if (this.password.length == 8) {
  158. return;
  159. }
  160. var len = constant.emojis.length;
  161. for (var i = 0 ; i < len ; i++) {
  162. if (char == constant.emojis[i].letter) {
  163. break;
  164. }
  165. }
  166. if (i == len) {
  167. return;
  168. }
  169. this.password += char;
  170. this.$.cancel.setShowing(true);
  171. this.drawPassword();
  172. },
  173. removeCharacter: function() {
  174. var len = this.password.length;
  175. if (len == 0) {
  176. return;
  177. }
  178. this.password = this.password.substr(0, len-1);
  179. if (this.password.length == 0) {
  180. this.$.cancel.setShowing(false);
  181. }
  182. this.drawPassword();
  183. }
  184. });
  185. // Class for an emoji
  186. enyo.kind({
  187. name: "Sugar.Emoji",
  188. kind: enyo.Control,
  189. published: {
  190. index: null,
  191. letter: true,
  192. size: null,
  193. selected: false
  194. },
  195. classes: "emoji",
  196. components: [
  197. {name: "icon", content: "", allowHtml: true, classes: "emoji-icon"},
  198. {name: "letter", content: "", showing: true, classes: "emoji-letter"}
  199. ],
  200. // Constructor
  201. create: function() {
  202. this.inherited(arguments);
  203. this.indexChanged();
  204. this.letterChanged();
  205. this.sizeChanged();
  206. this.selectedChanged();
  207. },
  208. rendered: function() {
  209. this.inherited(arguments);
  210. if (this.size) {
  211. document.getElementById(this.$.icon.id).style = "font-size: "+this.size+"pt";
  212. }
  213. },
  214. // Property changed
  215. indexChanged: function() {
  216. if (this.index >= 0 && this.index < constant.emojis.length) {
  217. var emoji = constant.emojis[this.index];
  218. this.$.icon.setContent("&#x"+emoji.value+";");
  219. this.$.letter.setContent(emoji.letter);
  220. }
  221. },
  222. letterChanged: function() {
  223. this.$.letter.setShowing(this.letter);
  224. },
  225. sizeChanged: function() {
  226. },
  227. selectedChanged: function() {
  228. this.removeClass("emoji-selected");
  229. this.removeClass("emoji-unselected");
  230. if (this.selected) {
  231. this.addClass("emoji-selected");
  232. } else {
  233. this.addClass("emoji-unselected");
  234. }
  235. },
  236. // Do a small animation to show a click
  237. animate: function() {
  238. var that = this;
  239. that.addClass("emoji-flash");
  240. window.setTimeout(function() {
  241. that.removeClass("emoji-flash");
  242. }, 500);
  243. }
  244. });