scriptOop.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. // function Password(parent, open) {
  2. // let input = document.createElement('input');
  3. // input.placeholder = 'password';
  4. // let checkbox = document.createElement('input');
  5. // checkbox.type = 'checkbox';
  6. // let param = document.createElement('label');
  7. // param.innerText = 'увидеть пароль :';
  8. // if ( open === true) {
  9. // input.type = 'text';
  10. // } else {
  11. // input.type = 'password';
  12. // }
  13. // this.setValue = function(value) {
  14. // input.value = value ;
  15. // }
  16. // this.getValue = function() {
  17. // console.log(input.value);
  18. // return input.value;
  19. // };
  20. // this.setOpen = function (value) {
  21. // if (value === true) {
  22. // input.type = 'text';
  23. // open = value;
  24. // }else {
  25. // input.type = 'password';
  26. // open = value;
  27. // }
  28. // };
  29. // this.getOpen = function () {
  30. // console.log(open);
  31. // return open;
  32. // }
  33. // input.oninput = (e) => {
  34. // if(typeof this.onChange === 'function') {
  35. // this.onChange(e.target.value);
  36. // }
  37. // }
  38. // checkbox.onclick = (e) => {
  39. // if(input.type === 'text') {
  40. // input.type = 'password';
  41. // open = false;
  42. // }else if (input.type === 'password') {
  43. // input.type = 'text';
  44. // open = true;
  45. // }
  46. // if (typeof this.onOpenChange === 'function') {
  47. // this.onOpenChange(open);
  48. // }
  49. // };
  50. // parent.appendChild(param);
  51. // parent.appendChild(input);
  52. // param.appendChild(checkbox);
  53. // }
  54. // let formP = new Password(containerPassword, true);
  55. // formP.onOpenChange = (open) => console.log(open);
  56. // formP.onChange = (password) => {
  57. // console.log(password);
  58. // };
  59. // formP.setValue('Введите ваш пароль');
  60. // formP.setOpen(false);
  61. // formP.getValue();
  62. // function Login(parent, open) {
  63. // let input = document.createElement('input');
  64. // input.placeholder = 'login';
  65. // let checkbox = document.createElement('input');
  66. // checkbox.type = 'checkbox';
  67. // let param = document.createElement('label');
  68. // param.innerText = 'увидеть пароль :';
  69. // let btn = document.createElement('button');
  70. // btn.innerText = 'Введите Логин';
  71. // btn.setAttribute('disabled', 'disabled');
  72. // let check = document.createElement('input');
  73. // check.type = 'checkbox';
  74. // if ( open === true) {
  75. // input.type = 'text';
  76. // } else {
  77. // input.type = 'password';
  78. // }
  79. // let value = new Password(containerPassword, false);
  80. // value.onChange = () => {
  81. // if(pass.getValue() !=='' && input.value !== ''){
  82. // button.removeAttribute('disabled');
  83. // }else {
  84. // button.setAttribute('disabled', 'disabled');
  85. // }
  86. // };
  87. // input.oninput = (e) => {
  88. // if(pass.getValue() !== '' && input.value !== '') {
  89. // button.removeAttribute('disabled');
  90. // }else {
  91. // button.setAttribute('disabled', 'disabled');
  92. // }
  93. // };
  94. // check.onclick = (e) => {
  95. // if(input.type === 'text') {
  96. // input.type = 'password';
  97. // open = false;
  98. // }else if (input.type === 'password') {
  99. // input.type = 'text';
  100. // open = true;
  101. // }
  102. // };
  103. // parent.appendChild(input);
  104. // parent.appendChild(label);
  105. // label.appendChild(check);
  106. // parent.appendChild(br);
  107. // parent.appendChild(button);
  108. // }
  109. // let Log = new Login(containerLogin, false);
  110. //LoginForm
  111. // function Login(parent, open) {
  112. // let input = document.createElement('input');
  113. // input.placeholder = 'login';
  114. // let param = document.createElement('label');
  115. // param.innerText = 'Введите логин';
  116. // let br = document.createElement('br');
  117. // let button = document.createElement('button');
  118. // button.innerText = 'Push';
  119. // button.setAttribute('disabled', 'disabled');
  120. // let check = document.createElement('input');
  121. // check.type = 'checkbox';
  122. // if (open === true) {
  123. // input.type = 'text';
  124. // } else {
  125. // input.type = 'password';
  126. // }
  127. // let pass = new Password(containerPassword, false);
  128. // pass.onChange = () => {
  129. // if (pass.getValue() !== '' && input.value !== '') {
  130. // button.removeAttribute('disabled');
  131. // } else {
  132. // button.setAttribute('disabled', 'disabled');
  133. // }
  134. // };
  135. // input.oninput = (e) => {
  136. // if (pass.getValue() !== '' && input.value !== '') {
  137. // button.removeAttribute('disabled');
  138. // } else {
  139. // button.setAttribute('disabled', 'disabled');
  140. // }
  141. // };
  142. // check.onclick = (e) => {
  143. // if (input.type === 'text') {
  144. // input.type = 'password';
  145. // open = false;
  146. // } else if (input.type === 'password') {
  147. // input.type = 'text';
  148. // open = true;
  149. // }
  150. // };
  151. // parent.appendChild(input);
  152. // parent.appendChild(param);
  153. // param.appendChild(check);
  154. // parent.appendChild(br);
  155. // parent.appendChild(button);
  156. // }
  157. // let log = new Login(containerLogin, false);
  158. // LoginForm Constructor_____________________________________
  159. // function Passw(parent, open) {
  160. // let checkbox = document.createElement("input");
  161. // checkbox.type = "checkbox";
  162. // checkbox.checked = open;
  163. // let br = document.createElement('br');
  164. // let password = document.createElement("input");
  165. // let button = document.createElement("button");
  166. // let login = document.createElement("input");
  167. // button.innerText = "Push";
  168. // button.disabled = true;
  169. // this.sP = (paramValue) => (login.value = paramValue);
  170. // this.gP = () => login.value;
  171. // this.sP2 = (paramValue) => (password.value = paramValue);
  172. // this.gP2 = () => password.value;
  173. // this.sO = () => {
  174. // checkbox.checked ? (password.type = "text") : (password.type = "password");
  175. // };
  176. // this.gO = () => {
  177. // checkbox.checked ? (password.type = "text") : (password.type = "password");
  178. // };
  179. // checkbox.onchange = () => {
  180. // checkbox.checked ? this.sO() : this.gO();
  181. // };
  182. // login.oninput = () => this.onChange(login.value);
  183. // password.oninput = () => this.onChange(password.value);
  184. // this.sA = (openCheck) => (button.disabled = openCheck);
  185. // this.gA = () => button.disabled;
  186. // const letsCheck = () =>
  187. // this.gP () !== "" && this.gP2() !== ""
  188. // ? this.sA(false)
  189. // : this.sA(true);
  190. // this.onChange = () => letsCheck();
  191. // parent.append(login);
  192. // parent.append(br);
  193. // parent.append(password);
  194. // parent.append(checkbox);
  195. // parent.append(button);
  196. // }
  197. // let p = new Passw(wrapper, true);
  198. // Password Verify__________________________
  199. // function Password(parent, open) {
  200. // const password = document.createElement("input");
  201. // const checkbox = document.createElement("input");
  202. // checkbox.type = "checkbox";
  203. // checkbox.checked = open;
  204. // const login = document.createElement("input");
  205. // const button = document.createElement("button");
  206. // button.innerText = "Push";
  207. // button.disabled = true;
  208. // this.sP = (paramValue) => (login.value = paramValue);
  209. // this.gP = () => login.value;
  210. // this.sP2 = (paramValue) => (password.value = paramValue);
  211. // this.gP2 = () => password.value;
  212. // checkbox.onchange = () => {
  213. // if(checkbox.checked) {
  214. // login.type = "text";
  215. // login.remove();
  216. // button.remove() }
  217. // else {
  218. // login.type = "password";
  219. // parent.append(login,button);
  220. // }
  221. // };
  222. // this.setLogin = () => {
  223. // password.type = "password" ? (login.style.display = "none") : (login.style.display = "");
  224. // };
  225. // this.activeButton = (password, login) => {
  226. // password.value == login.value ? (button.disabled = false) : (button.disabled = true);
  227. // };
  228. // password.oninput = () => {
  229. // this.activeButton(password, login);
  230. // };
  231. // login.oninput = () => {
  232. // this.activeButton(password, login);
  233. // };
  234. // parent.append(password);
  235. // parent.append(checkbox);
  236. // parent.append(login);
  237. // parent.append(button);
  238. // }
  239. // let p = new Password(wrapper, true);
  240. // Form_________________________________________
  241. function Form(el, data, okCallback, cancelCallback) {
  242. let inputCreators = {String(key, value, oninput) {
  243. let input = document.createElement("input");
  244. input.type = "text";
  245. input.placeholder = key[0] === "*" ? key.slice(1, key.length) : key;
  246. input.id = `${key}`;
  247. if (value.charAt(0) === "*") {
  248. data[key] = "";
  249. input.type = "password";
  250. input.placeholder = "password";
  251. input.id = `${key}`;
  252. input.oninput = () => oninput(input.value);
  253. return input;
  254. }
  255. input.value = value;
  256. input.oninput = () => oninput(input.value);
  257. return input;
  258. },
  259. Boolean(key, value, oninput) {
  260. let input = document.createElement("input");
  261. input.type = "checkbox";
  262. input.checked = value;
  263. input.onchange = () => oninput(input.checked);
  264. input.id = `${key}`;
  265. return input;
  266. },
  267. Date: function (key, value, oninput) {
  268. let input = document.createElement("input");
  269. input.type = "datetime-local";
  270. input.placeholder = key;
  271. input.id = `${key}`;
  272. input.value = value.toISOString().slice(0, -1);
  273. input.oninput = () => oninput(Date(input.value));
  274. return input;
  275. },
  276. };
  277. this.validators = {};
  278. let table = document.createElement("table");
  279. formContainer.appendChild(table);
  280. for (let [key, value] of Object.entries(data)) {
  281. let tr = document.createElement("tr");
  282. let th = document.createElement("th");
  283. let td = document.createElement("td");
  284. let tdError = document.createElement("td");
  285. const span = document.createElement("span");
  286. const spanValue = document.createElement("span");
  287. let input = inputCreators[data[key].constructor.name](
  288. key,
  289. data[key],
  290. (value) => {
  291. data[key] = value;
  292. }
  293. );
  294. input.onchange = () => {
  295. let valueInput = input.value;
  296. let valueVal = true;
  297. if (value.constructor.name === Boolean) {
  298. valueInput = input.checked;
  299. }
  300. if (typeof this.validators[key] === "function") {
  301. valueVal = this.validators[key](valueInput,key,data,input);
  302. if (typeof valueVal === "string") {
  303. valueVal = false;
  304. input.style.backgroundColor = "#95adab";
  305. tdError.innerHTML = ` ${this.validators[key](valueInput,key,data,input)}`;
  306. }
  307. if (valueVal) {
  308. input.style.backgroundColor = "white";
  309. tdError.innerHTML = ` `;
  310. }
  311. }
  312. valueInput === "" ? (input.style.border = "1px solid rgb(255, 13, 0)") : (input.style.border = null);
  313. if (key[0] == "*") {
  314. if (data[key] == "") {
  315. input.style.backgroundColor = "#95adab";
  316. } else {
  317. input.style.backgroundColor = "white";
  318. }
  319. }
  320. };
  321. if (key[0] === "*") {
  322. const start = key.charAt(0);
  323. spanValue.innerHTML = `${key.slice(1, key.length)}`;
  324. span.innerText = `${start} `;
  325. } else {
  326. spanValue.innerHTML = key;
  327. }
  328. table.append(tr);
  329. tr.append(th);
  330. tr.append(td);
  331. tr.append(tdError);
  332. td.appendChild(input);
  333. th.appendChild(span);
  334. th.append(spanValue);
  335. }
  336. this.copyData = { ...data };
  337. this.checkInput = function () {
  338. let input = document.querySelector(input);
  339. input.value;
  340. };
  341. let formBody = document.createElement("div");
  342. let okButton = document.createElement("button");
  343. okButton.innerHTML = "OK";
  344. if (typeof okCallback === "function") {
  345. formBody.appendChild(okButton);
  346. okButton.onclick = (e) => {
  347. let keyValue = 0;
  348. let countVal = 0;
  349. for (let [key, value] of Object.entries(data)) {
  350. let valueInput = value;
  351. let valueVal = true;
  352. if (valueVal && valueInput) {
  353. countVal++;
  354. }
  355. keyValue++;
  356. }
  357. if (keyValue === countVal) {
  358. console.log(this);
  359. this.okCallback(this.data);
  360. }
  361. };
  362. }
  363. let cancelButton = document.createElement("button");
  364. cancelButton.innerHTML = "Cancel";
  365. cancelButton.addEventListener("click", () => {
  366. console.log(this.copyData);
  367. for (let [key, value] of Object.entries(this.copyData)) {
  368. let input = document.getElementById(key);
  369. if (value.constructor.name === "String") {
  370. input.value = `${value}`;
  371. }
  372. if (value.constructor.name === "Boolean") {
  373. input.checked = `${value}`;
  374. }
  375. if (value.constructor.name === "Date") {
  376. input.value = value.toISOString().slice(0, -1);
  377. }
  378. }
  379. });
  380. if (typeof cancelCallback === "function") {
  381. formBody.appendChild(cancelButton);
  382. cancelButton.onclick = cancelCallback;
  383. }
  384. el.appendChild(formBody);
  385. this.okCallback;
  386. this.cancelCallback = cancelCallback;
  387. this.data = data;
  388. }
  389. let form = new Form(
  390. formContainer,
  391. {
  392. "*name": "Anakin",
  393. surname: "Skywalker",
  394. married: true,
  395. password: "******",
  396. birthday: new Date(new Date().getTime() - 86400000 * 30 * 365),
  397. },
  398. () => console.log("ok"),
  399. () => console.log("cancel")
  400. );
  401. form.okCallback = (data) => console.log(data);
  402. form.validators.surname = (value, key, data, input) =>
  403. value.length > 2 && value[0].toUpperCase() == value[0] && !value.includes(" ")
  404. ? true
  405. : "Wrong name";