/Source/SNDK/javascript/sui/textbox.js

https://github.com/sundowndk/SNDK · JavaScript · 802 lines · 551 code · 130 blank · 121 comment · 71 complexity · 3f07c41bbc589a719d58177b00c3a125 MD5 · raw file

  1. // -------------------------------------------------------------------------------------------------------------------------
  2. // textbox ([attributes])
  3. // -------------------------------------------------------------------------------------------------------------------------
  4. //
  5. // .getAttribute (string)
  6. // .setAttribute (string, string)
  7. //
  8. // id get
  9. // tag get/set
  10. // name get/set
  11. // stylesheet get/set
  12. // appendTo get/set
  13. // managed get/set
  14. // width get/set
  15. // height get
  16. // disabled get/set
  17. // autoComplete get/set
  18. // readOnly get/set
  19. // transform get/set
  20. // focus get/set
  21. // password get/set
  22. // onFocus get/set
  23. // onBlur get/set
  24. // onChange get/set
  25. // onKeyUp get/set
  26. // onEnter get/set
  27. // value get/set
  28. // tabIndex get/set
  29. //
  30. /**
  31. * @constructor
  32. */
  33. textbox : function (attributes)
  34. {
  35. var _elements = new Array ();
  36. var _attributes = attributes;
  37. var _temp = { initialized: false,
  38. cache: new Array ()
  39. };
  40. _attributes.id = SNDK.tools.newGuid ()
  41. setAttributes ();
  42. // Private functions
  43. this._attributes = _attributes;
  44. this._elements = _elements;
  45. this._temp = _temp;
  46. this._init = init;
  47. // Functions
  48. this.type = "TEXTBOX";
  49. this.refresh = functionRefresh;
  50. this.dispose = functionDispose;
  51. this.getAttribute = functionGetAttribute;
  52. this.setAttribute = functionSetAttribute;
  53. // Construct
  54. construct ();
  55. // Initialize
  56. SNDK.SUI.addInit (this);
  57. // ------------------------------------
  58. // Private functions
  59. // ------------------------------------
  60. // ------------------------------------
  61. // init
  62. // ------------------------------------
  63. function init ()
  64. {
  65. updateCache ();
  66. _attributes.heightType = "pixel";
  67. _attributes.height = _temp.cache["containerPadding"]["vertical"] + _temp.cache["containerHeight"] +"px";
  68. }
  69. // ------------------------------------
  70. // construct
  71. // ------------------------------------
  72. function construct ()
  73. {
  74. // Container
  75. _elements["container"] = SNDK.tools.newElement ("div", {});
  76. _elements["container"].setAttribute ("id", _attributes.id);
  77. _elements["container"].className = _attributes.stylesheet;
  78. // Control Left
  79. _elements["left"] = SNDK.tools.newElement ("div", {className: "Left", appendTo: _elements["container"]});
  80. // Control Center
  81. _elements["center"] = SNDK.tools.newElement ("div", {className: "Center", appendTo: _elements["container"]});
  82. // Control Right
  83. _elements["right"] = SNDK.tools.newElement ("div", {className: "Right", appendTo: _elements["container"]});
  84. // Control Input
  85. // FIXTHIS
  86. var type = "text";
  87. if (_attributes.password)
  88. {
  89. type = "password";
  90. }
  91. _elements["input"] = SNDK.tools.newElement ("input", {className: "Input", type: type, appendTo: _elements["center"]});
  92. _elements["input"].setAttribute ("name", _attributes.name);
  93. if (_attributes.textTransform)
  94. {
  95. _elements["input"].style.textTransform = _attributes.textTransform;
  96. }
  97. // Hook Events
  98. _elements["input"].onfocus = eventOnFocus;
  99. _elements["input"].onblur = eventOnBlur;
  100. _elements["input"].onchange = eventOnChange;
  101. _elements["input"].onkeyup = eventOnKeyUp;
  102. _elements["input"].onkeypress = eventOnKeyPress;
  103. window.addEvent (window, 'SUIREFRESH', refresh);
  104. }
  105. function functionDispose ()
  106. {
  107. window.removeEvent (window, 'SUIREFRESH', refresh);
  108. }
  109. // ------------------------------------
  110. // refresh
  111. // ------------------------------------
  112. function refresh ()
  113. {
  114. if (!SNDK.debugStopRefresh)
  115. {
  116. if (_temp.initialized)
  117. {
  118. if (_attributes.disabled)
  119. {
  120. _elements["container"].className = _attributes.stylesheet +" "+ _attributes.stylesheet+"Disabled";
  121. _elements["input"].disabled = true;
  122. _attributes.focus = false;
  123. eventOnBlur ();
  124. }
  125. else
  126. {
  127. if (_attributes.focus)
  128. {
  129. _elements["container"].className = _attributes.stylesheet +" "+ _attributes.stylesheet+"Focus";
  130. setFocus ();
  131. }
  132. else
  133. {
  134. _elements["container"].className = _attributes.stylesheet;
  135. }
  136. _elements["input"].disabled = false;
  137. }
  138. if (_attributes.autoComplete)
  139. {
  140. _elements["input"].setAttribute ("autocomplete", "on");
  141. }
  142. else
  143. {
  144. _elements["input"].setAttribute ("autocomplete", "off");
  145. }
  146. if (_attributes.readOnly)
  147. {
  148. _elements["input"].setAttribute ("readonly", "true");
  149. }
  150. else
  151. {
  152. _elements["input"].removeAttribute ("readonly");
  153. }
  154. if (_attributes.value != null)
  155. {
  156. _elements["input"].value = _attributes.value;
  157. }
  158. _elements["input"].tabIndex = _attributes.tabIndex;
  159. }
  160. setDimensions ();
  161. }
  162. }
  163. // ------------------------------------
  164. // updateCache
  165. // ------------------------------------
  166. function updateCache ()
  167. {
  168. if (!_temp.cacheUpdated)
  169. {
  170. _temp.cache["containerPadding"] = SNDK.tools.getElementStyledPadding (_elements["container"]);
  171. _temp.cache["containerWidth"] = (SNDK.tools.getElementStyledWidth (_elements["left"]) + SNDK.tools.getElementStyledWidth (_elements["right"]));
  172. _temp.cache["containerHeight"] = SNDK.tools.getElementStyledHeight (_elements["center"]);
  173. _temp.cache["inputPadding"] = SNDK.tools.getElementStyledPadding (_elements["input"]);
  174. }
  175. _temp.cacheUpdated = true;
  176. }
  177. // ------------------------------------
  178. // setAttributes
  179. // ------------------------------------
  180. function setAttributes ()
  181. {
  182. // Name
  183. if (!_attributes.name)
  184. _attributes.name = "";
  185. // Stylesheet
  186. if (!_attributes.stylesheet)
  187. _attributes.stylesheet = "SUITextbox";
  188. // Managed
  189. if (!_attributes.managed)
  190. _attributes.managed = false;
  191. // Width
  192. if (!_attributes.width)
  193. _attributes.width = "100px";
  194. if (_attributes.width.substring (_attributes.width.length - 1) == "%")
  195. {
  196. _attributes.widthType = "percent";
  197. _attributes.width = _attributes.width.substring (0, _attributes.width.length - 1)
  198. }
  199. else
  200. {
  201. _attributes.widthType = "pixel";
  202. _attributes.width = _attributes.width.substring (0, _attributes.width.length - 2)
  203. }
  204. // Disabled
  205. if (!_attributes.disabled)
  206. _attributes.disabled = false;
  207. // AutoComplete
  208. if (!_attributes.autoComplete)
  209. _attributes.autoComplete = false;
  210. // ReadOnly
  211. if (!_attributes.readOnly)
  212. _attributes.readOnly = false;
  213. // Focus
  214. if (!_attributes.focus)
  215. _attributes.focus = false;
  216. // Password
  217. if (!_attributes.password)
  218. _attributes.password = false;
  219. // textTransform
  220. if (!_attributes.textTransform)
  221. _attributes.textTransform = "none";
  222. // onFocus
  223. if (!_attributes.onFocus)
  224. _attributes.onFocus = null;
  225. // onBlur
  226. if (!_attributes.onBlur)
  227. _attributes.onBlur = null;
  228. // onChange
  229. if (!_attributes.onChange)
  230. _attributes.onChange = null;
  231. // onEnter
  232. if (!_attributes.onEnter)
  233. _attributes.onEnter = null;
  234. // Value
  235. if (!_attributes.value)
  236. _attributes.value = "";
  237. // TabIndex
  238. if (!_attributes.tabIndex)
  239. _attributes.tabIndex = 0;
  240. }
  241. // ------------------------------------
  242. // setDimensions
  243. // ------------------------------------
  244. function setDimensions ()
  245. {
  246. if (_temp.initialized)
  247. {
  248. var width = {};
  249. if (!_attributes.managed && _attributes.widthType != "pixel")
  250. {
  251. width.container = ((SNDK.tools.getElementInnerWidth (_elements["container"].parentNode) * _attributes.width) / 100) - _temp.cache.containerPadding["horizontal"];
  252. }
  253. else
  254. {
  255. if (_attributes.managed && _attributes.widthType == "percent")
  256. {
  257. width.container = _attributes.managedWidth - _temp.cache.containerPadding["horizontal"];
  258. }
  259. else
  260. {
  261. width.container = _attributes.width - _temp.cache.containerPadding["horizontal"];
  262. }
  263. }
  264. width.center = width.container - _temp.cache.containerWidth;
  265. width.input = width.center - _temp.cache.inputPadding["horizontal"];
  266. _elements["container"].style.width = width.container +"px";
  267. _elements["center"].style.width = width.center +"px";
  268. _elements["input"].style.width = width.input +"px";
  269. }
  270. }
  271. // ------------------------------------
  272. // setFocus
  273. // ------------------------------------
  274. function setFocus ()
  275. {
  276. setTimeout ( function () { _attributes.focus = true; _elements["input"].focus (); }, 25);
  277. }
  278. // ------------------------------------
  279. // keyHandler
  280. // ------------------------------------
  281. function keyHandler (event)
  282. {
  283. var key;
  284. if (!event && window.event)
  285. {
  286. event = window.event;
  287. }
  288. if (event)
  289. {
  290. key = event.keyCode;
  291. }
  292. else
  293. {
  294. key = event.which;
  295. }
  296. return key;
  297. }
  298. // ------------------------------------
  299. // Public functions
  300. // ------------------------------------
  301. // ------------------------------------
  302. // refresh
  303. // ------------------------------------
  304. function functionRefresh ()
  305. {
  306. refresh ();
  307. }
  308. // ------------------------------------
  309. // getAttribute
  310. // ------------------------------------
  311. function functionGetAttribute (attribute)
  312. {
  313. switch (attribute)
  314. {
  315. case "id":
  316. {
  317. return _attributes[attribute];
  318. }
  319. case "tag":
  320. {
  321. return _attributes[attribute];
  322. }
  323. case "name":
  324. {
  325. return _attributes[attribute];
  326. }
  327. case "stylesheet":
  328. {
  329. return _attributes[attribute];
  330. }
  331. case "width":
  332. {
  333. if (_attributes.widthType == "percent")
  334. {
  335. return _attributes.width + "%";
  336. }
  337. if (_attributes.widthType == "pixel")
  338. {
  339. return _attributes.width + "px";
  340. }
  341. }
  342. case "height":
  343. {
  344. if (_attributes.heightType == "percent")
  345. {
  346. return _attributes.height + "%";
  347. }
  348. if (_attributes.heightType == "pixel")
  349. {
  350. return _attributes.height + "px";
  351. }
  352. }
  353. case "appendTo":
  354. {
  355. return _attributes[attribute];
  356. }
  357. case "managed":
  358. {
  359. return _attributes[attribute];
  360. }
  361. case "disabled":
  362. {
  363. return _attributes[attribute];
  364. }
  365. case "autocomplete":
  366. {
  367. return _attributes[attribute];
  368. }
  369. case "readOnly":
  370. {
  371. return _attributes[attribute];
  372. }
  373. case "focus":
  374. {
  375. return _attributes[attribute];
  376. }
  377. case "password":
  378. {
  379. return _attributes[attribute];
  380. }
  381. case "textTransform":
  382. {
  383. return _attributes[attribute];
  384. }
  385. case "onFocus":
  386. {
  387. return _attributes[attribute];
  388. }
  389. case "onBlur":
  390. {
  391. return _attributes[attribute];
  392. }
  393. case "onChange":
  394. {
  395. return _attributes[attribute];
  396. }
  397. case "onKeyUp":
  398. {
  399. return _attributes[attribute];
  400. }
  401. case "onEnter":
  402. {
  403. return _attributes[attribute];
  404. }
  405. case "value":
  406. {
  407. return getValue ();
  408. }
  409. case "tabIndex":
  410. {
  411. return _attributes[attribute];
  412. }
  413. default:
  414. {
  415. throw "No attribute with the name '"+ attribute +"' exist in this object";
  416. }
  417. }
  418. }
  419. // ------------------------------------
  420. // setAttribute
  421. // ------------------------------------
  422. function functionSetAttribute (attribute, value)
  423. {
  424. switch (attribute)
  425. {
  426. case "id":
  427. {
  428. throw "Attribute with name ID is ready only.";
  429. break;
  430. }
  431. case "tag":
  432. {
  433. _attributes[attribute] = value;
  434. break;
  435. }
  436. case "name":
  437. {
  438. _attributes[attribute] = value;
  439. break;
  440. }
  441. case "stylesheet":
  442. {
  443. _attributes[attribute] = value;
  444. break;
  445. }
  446. case "width":
  447. {
  448. if (value.substring (value.width.length, 3) == "%")
  449. {
  450. _attributes.widthType = "percent";
  451. _attributes.width = value.width.substring (0, value.width.length - 1)
  452. }
  453. else
  454. {
  455. _attributes.widthType = "pixel";
  456. _attributes.width = value.width.substring (0, value.width.length - 2)
  457. }
  458. break;
  459. }
  460. case "height":
  461. {
  462. throw "Attribute with name HEIGHT is ready only.";
  463. break;
  464. }
  465. case "appendTo":
  466. {
  467. _attributes[attribute] = value;
  468. _attributes.appendTo.appendChild (_elements["container"]);
  469. break;
  470. }
  471. case "managed":
  472. {
  473. _attributes[attribute] = value;
  474. if (value)
  475. {
  476. window.removeEvent (window, 'SUIREFRESH', refresh);
  477. }
  478. else
  479. {
  480. window.addEvent (window, 'SUIREFRESH', refresh);
  481. }
  482. break;
  483. }
  484. case "disabled":
  485. {
  486. _attributes[attribute] = value;
  487. refresh ();
  488. break;
  489. }
  490. case "autocomplete":
  491. {
  492. _attributes[attribute] = value;
  493. break;
  494. }
  495. case "readOnly":
  496. {
  497. _attributes[attribute] = value;
  498. refresh ();
  499. break;
  500. }
  501. case "focus":
  502. {
  503. _attributes[attribute] = value;
  504. refresh ();
  505. break;
  506. }
  507. case "password":
  508. {
  509. _attributes[attribute] = value;
  510. refresh ();
  511. break;
  512. }
  513. case "textTransform":
  514. {
  515. _attributes[attribute] = value;
  516. refresh ();
  517. break;
  518. }
  519. case "onFocus":
  520. {
  521. _attributes[attribute] = value;
  522. break;
  523. }
  524. case "onBlur":
  525. {
  526. _attributes[attribute] = value;
  527. break;
  528. }
  529. case "onChange":
  530. {
  531. _attributes[attribute] = value;
  532. break;
  533. }
  534. case "onKeyUp":
  535. {
  536. _attributes[attribute] = value;
  537. break;
  538. }
  539. case "onEnter":
  540. {
  541. _attributes[attribute] = value;
  542. break;
  543. }
  544. case "value":
  545. {
  546. _attributes[attribute] = value;
  547. refresh ();
  548. if (_temp.initialized)
  549. {
  550. eventOnChange ();
  551. }
  552. break;
  553. }
  554. case "tabIndex":
  555. {
  556. _attributes[attribute] = value;
  557. refresh ();
  558. }
  559. default:
  560. {
  561. throw "No attribute with the name '"+ attribute +"' exist in this object";
  562. }
  563. }
  564. }
  565. // ------------------------------------
  566. // Events
  567. // ------------------------------------
  568. // ------------------------------------
  569. // ------------------------------------
  570. // eventOnKeyPress
  571. // ------------------------------------
  572. function eventOnKeyPress (event)
  573. {
  574. _attributes.value = _elements["input"].value;
  575. }
  576. function getValue ()
  577. {
  578. var result = _attributes.value;
  579. switch (_attributes.textTransform.toLowerCase ())
  580. {
  581. case "uppercase":
  582. {
  583. result = result.toUpperCase ();
  584. break;
  585. }
  586. }
  587. return result;
  588. }
  589. function transform ()
  590. {
  591. switch (_attributes.textTransform.toLowerCase ())
  592. {
  593. case "uppercase":
  594. {
  595. //_attributes.value = _attributes.value.toUpperCase ();
  596. return _attributes.value.toUpperCase ();
  597. //_elements["input"].value = _attributes.value.toUpperCase ();
  598. break;
  599. }
  600. }
  601. }
  602. // ------------------------------------
  603. // onKeyUp
  604. // ------------------------------------
  605. function eventOnKeyUp (event)
  606. {
  607. var result = true;
  608. var key = keyHandler (event);
  609. //_attributes.value = _elements["input"].value;
  610. if (_attributes.onKeyUp != null)
  611. {
  612. setTimeout( function () { _attributes.onKeyUp (_attributes.tag); }, 1);
  613. }
  614. // ONENTER
  615. if (key == 13)
  616. {
  617. if (_attributes.onEnter != null)
  618. {
  619. setTimeout( function () { _attributes.onEnter (_attributes.tag); }, 1);
  620. }
  621. }
  622. eventOnChange ();
  623. return result;
  624. }
  625. // ------------------------------------
  626. // onFocus
  627. // ------------------------------------
  628. function eventOnFocus ()
  629. {
  630. if (!_attributes.disabled)
  631. {
  632. if (!_attributes.focus)
  633. {
  634. _attributes.focus = true;
  635. refresh ();
  636. if (_attributes.onFocus != null)
  637. {
  638. setTimeout( function () { _attributes.onFocus (_attributes.tag); }, 1);
  639. }
  640. }
  641. }
  642. }
  643. // ------------------------------------
  644. // onBlur
  645. // ------------------------------------
  646. function eventOnBlur ()
  647. {
  648. if (!_attributes.disabled)
  649. {
  650. if (_attributes.focus)
  651. {
  652. _attributes.focus = false;
  653. refresh ();
  654. if (_attributes.onBlur != null)
  655. {
  656. setTimeout( function () { _attributes.onBlur (_attributes.tag); }, 1);
  657. }
  658. }
  659. }
  660. }
  661. // ------------------------------------
  662. // onChange
  663. // ------------------------------------
  664. function eventOnChange ()
  665. {
  666. _attributes.value = _elements["input"].value;
  667. if (_attributes.onChange != null)
  668. {
  669. setTimeout( function () { _attributes.onChange (_attributes.tag); }, 1);
  670. }
  671. }
  672. }