foop ES6.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //Store Class
  2. {
  3. class Store{
  4. #reducer;
  5. #state;
  6. #cbs = []
  7. constructor(reducer){
  8. this.#reducer=reducer
  9. this.#state= this.#reducer(undefined, {})
  10. }
  11. get state(){
  12. return this.#state
  13. }
  14. getState(){
  15. return this.#state
  16. }
  17. subscribe(cb){
  18. (this.#cbs.push(cb), () => this.#cbs = this.#cbs.filter(c => c !== cb))
  19. }
  20. dispatch(action){
  21. const newState = this.#reducer(this.#state, action)
  22. if (newState !== this.#state){
  23. this.#state = newState
  24. for (let cb of this.#cbs) cb()
  25. }
  26. }
  27. }
  28. }
  29. class StoreThunk extends Store{
  30. dispatch(action) {
  31. if (typeof action === 'function') {
  32. return action(this.dispatch.bind(this), this.state)
  33. }
  34. super.dispatch(action);
  35. }
  36. }
  37. //Password Class
  38. {
  39. class Password{
  40. constructor(parent, open){
  41. let input = document.createElement('input')
  42. let checkBox = document.createElement('input')
  43. checkBox.type = 'checkbox'
  44. checkBox.onchange =()=>this.open(checkBox.checked)
  45. parent.append(input, checkBox)
  46. }
  47. set value(value){
  48. this.input.value = value
  49. }
  50. set open(open){
  51. this.input.type = open ?'text':'password'
  52. }
  53. get value(){
  54. this.input.value
  55. }
  56. get open(){
  57. this.input.type
  58. }
  59. }
  60. let p = new Password(document.body, true)
  61. p.onChange = data => console.log(data)
  62. p.onOpenChange = open => console.log(open)
  63. p.value('qwerty')
  64. console.log(p.value())
  65. p.open(false)
  66. console.log(p.open())
  67. }
  68. class RGB{
  69. #r
  70. #g
  71. #b
  72. get r(){
  73. return this.#r
  74. }
  75. get g(){
  76. return this.#g
  77. }
  78. get b(){
  79. return this.#b
  80. }
  81. get rgb(){
  82. return `rgb(${this.#r},${this.#g},${this.#b})`
  83. }
  84. get hex(){
  85. return "#" + ((1 << 24) + (this.#r << 16) + (this.#g << 8) + this.#b).toString(16).slice(1)
  86. }
  87. set r(value){
  88. if(typeof value === 'number' && value<=255 && value>=0){
  89. return this.#r=value
  90. }else{throw new RangeError(value)}
  91. }
  92. set g(value){
  93. if(typeof value === 'number' && value<=255 && value>=0){
  94. return this.#g=value
  95. }else{throw new RangeError(value)}
  96. }
  97. set b(value){
  98. if(typeof value === 'number' && value<=255 && value>=0){
  99. return this.#b=value
  100. }else{throw new RangeError(value)}
  101. }
  102. set rgb(rgb){
  103. var matchColors = /rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/;
  104. var match = matchColors.exec(rgb);
  105. if (match !== null) {
  106. this.#r = match[1]
  107. this.#g = match[2]
  108. this.#b = match[3]
  109. }else{throw new RangeError(rgb)}
  110. }
  111. set hex(hex){
  112. var match = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  113. if (match !== null) {
  114. this.#r = parseInt(match[1], 16)
  115. this.#g = parseInt(match[2], 16)
  116. this.#b = parseInt(match[3], 16)
  117. }else{throw new SyntaxError(hex)}
  118. }
  119. }
  120. class RGBA extends RGB{
  121. #a
  122. get a(){
  123. return this.#a
  124. }
  125. set a(value){
  126. if(typeof value === 'number' && value<=1 && value>=0){
  127. return this.#a=value
  128. }else{throw new RangeError(value)}
  129. }
  130. set hex(hex){
  131. if(hex.length>7){
  132. var match = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  133. if (match !== null) {
  134. this.r = parseInt(match[1], 16)
  135. this.g = parseInt(match[2], 16)
  136. this.b = parseInt(match[3], 16)
  137. this.#a= parseInt(match[4], 16)/255
  138. this.#a= +this.#a.toFixed(2)
  139. }else{throw new SyntaxError(hex)}
  140. }else{return super.hex=hex}
  141. }
  142. get hex(){
  143. debugger
  144. let transparency=((this.#a * 255) | 1 << 8).toString(16).slice(1)
  145. if (transparency.length == 1) transparency = '0' + transparency;
  146. return super.hex+transparency
  147. }
  148. set rgba(rgba){
  149. debugger
  150. var matchColors = /^rgba\((\d{1,3}), *(\d{1,3}), *(\d{1,3})(?:, *(\d*\.?\d+))?\)$/;
  151. var match = matchColors.exec(rgba);
  152. if (match !== null) {
  153. this.r = Number(match[1])
  154. this.g = Number(match[2])
  155. this.b = Number(match[3])
  156. this.#a = Number(match[4])
  157. }else{throw new RangeError(rgba)}
  158. }
  159. get rgba(){
  160. return `rgba(${this.r},${this.g},${this.b},${this.#a})`
  161. }
  162. set color(value){
  163. try{
  164. this.hex=value
  165. }catch(e){}
  166. try{
  167. this.rgba=value
  168. }catch(e){}
  169. try{
  170. this.rgb=value
  171. }catch(e){}
  172. }
  173. }