dom.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * Simple Javascript eXtension - 1.0
  3. * (c) 2011 - 2015 Steve L. Nyemba, steve@the-phi.com
  4. * License GPL version 3.0
  5. *
  6. * Handling of DOM objects will rely on DOM object identifiers (ID) with a few exceptions.
  7. * The focus of a dom object is mostly centered around a single DOM object,
  8. * We advise using utils.js in order to handle collections of DOM objects as utils.js implements various utilities and industry standard design patterns
  9. *
  10. * This implementation is designed by W3C specifications of HTML5 and will integrate well with other frameworks that do so.
  11. * In addition we tried to mildly specify preconditions for executions of functions.
  12. * Architecturally the namespace serves as a wrapper around common DOM based tasks and can/should be cross-browser compatible because based on W3C standards
  13. *
  14. * LICENSE: GPLv3:
  15. * This program comes with absolute NO WARRANTY or implied warranty and is free to use for any purpose: modified, integrated, distributed at will.
  16. */
  17. if(!jx){
  18. var jx = {}
  19. }
  20. jx.dom = {} ;
  21. /**
  22. * Determines if a DOM object exists or not
  23. * @param {type} id
  24. * @returns {Boolean}
  25. */
  26. jx.dom.exists = function(id){
  27. return document.getElementById(id) != null ;
  28. }
  29. /**
  30. * This function will remove a node given it's identifier
  31. * @pre jx.dom.exists(id)
  32. * @param {type} id
  33. * @returns {undefined}
  34. */
  35. jx.dom.remove = function(id){
  36. var _item = null;
  37. if (id.constructor == String){
  38. _item = jx.dom.get.instance(id)
  39. }else{
  40. _item = id ;
  41. }
  42. _item.parentNode.removeChild(_item) ;
  43. return _item ;
  44. }
  45. /**
  46. * Append a child to DOM to a given DOM object provided the identifier
  47. * @pre jx.dom.exists(id) && _child != null
  48. * @param id identifier of the DOM object
  49. * @param _child child dom object
  50. */
  51. jx.dom.append = function(id,_child){
  52. var _parent = jx.dom.get.instance(id) ;
  53. _parent.appendChild(_child) ;
  54. }
  55. /**
  56. * This function will show a DOM object (assuming the DOM was hidden or not)
  57. * @pre jx.dom.exiss(_id)
  58. * @param _id DOM object identifier
  59. */
  60. jx.dom.show = function(_id){
  61. _dom = jx.dom.get.instance(_id) ;
  62. _dom.style.display = null;
  63. }
  64. /**
  65. * This function will hide a DOM object (assuming the DOM is visible or not)
  66. * @pre jx.dom.exiss(_id)
  67. * @param _id DOM object identifier
  68. */
  69. jx.dom.hide = function(_id){
  70. _dom = jx.dom.get.instance(_id) ;
  71. _dom.style.display = 'none' ;
  72. }
  73. /**
  74. * This function allow extraction from an select tag, if mutil selection is enabled an array is returned otherwise a scalar or string
  75. * The function also supports accessing a user defined attribute if specified otherwise it will use the default 'value', text can be specified
  76. * @param {type} id
  77. * @param {type} field
  78. * @returns {Array|jx.dom.get.dropdown.value}
  79. */
  80. _getdropdownvalue = function(id,field){
  81. var _dom = document.getElementById(id) ;
  82. field = (field)?field:'value'
  83. var value = null;
  84. if(field!= null && _dom != null){
  85. value = []
  86. var option = 0;
  87. for(var i=0; i < _dom.options.length; i++){
  88. option = _dom.options[i] ;
  89. if(option.selected){
  90. value.push(option[field])
  91. }
  92. }
  93. }
  94. if(value.length == 0){
  95. value = 0
  96. }else if (value.length == 1){
  97. value = value[0]
  98. }
  99. return value;
  100. }
  101. /**
  102. * This function returns a value for checkboxes given a name
  103. * @param {type} id name of the checkboxes
  104. * @returns {value}
  105. */
  106. _getcheckboxvalue = function(id){
  107. ldoms = document.getElementsByName(id) ;
  108. value = null;
  109. for(var i=0; i < ldoms.length; i++){
  110. if(ldoms[i].checked == true){
  111. value = ldoms[i].value;
  112. break;
  113. }
  114. }
  115. return value;
  116. }
  117. _getradiovalue = function(id){
  118. return _getcheckboxvalue(id);
  119. }
  120. /**
  121. * Returns the input value of an input with type == text|password
  122. * @param {type} id
  123. * @returns {document@call;getElementById.value}
  124. */
  125. _getinputvalue = function(id){
  126. _input = document.getElementById(id) ;
  127. return _input.value ;
  128. }
  129. /**
  130. * This function returns the value of a SPAN|DIV
  131. * @pre jx.dom.exists(id)
  132. */
  133. _getspanvalue = function(id){
  134. _input = document.getElementById(id) ;
  135. return _input.innerHTML ;
  136. }
  137. jx.dom.get = {} ;
  138. /**
  139. * This function will return either a newly created dom object or an existing one if passed an identifier {name|id}
  140. * @pre : jx.dom.exists(id) == true || id.match(/<htmlTag>/i)
  141. * @post: jx.dom.get.instance(id) != null
  142. * @returns {NodeList|obj|Element}
  143. */
  144. jx.dom.get.instance = function(id){
  145. if(jx.dom.exists(id)){
  146. obj = (document.getElementById(id)) ;
  147. if(obj == null){
  148. obj = document.getElementsByName(id)
  149. if(obj.length == 1){
  150. obj = obj[0]
  151. }else{
  152. obj = null;
  153. }
  154. }
  155. }else{
  156. obj = document.createElement(id)
  157. }
  158. return obj ;
  159. }
  160. /**
  161. * This function will return the value of a dom object regardless of the object
  162. * @pre : jx.dom.exists(id) == true
  163. * @post: No Exception is Thrown
  164. * @param {type} id
  165. * @returns {undefined}
  166. */
  167. jx.dom.get.value = function(id){
  168. obj = document.getElementById(id) ;
  169. if(obj == null){
  170. obj = document.getElementsByName(id) ;
  171. if (obj != null){
  172. obj = obj[0] ;
  173. }
  174. }
  175. //-- at this point we've tried two methods
  176. tag = obj.tagName ;
  177. if(tag.match(/input/i)){
  178. if(obj.type.match(/text|password/i)){
  179. key = 'INPUT'
  180. }else {
  181. key = obj.type ;
  182. }
  183. }else{
  184. key = tag ;
  185. }
  186. key = key.toUpperCase();
  187. pointer = {} ;
  188. pointer['SELECT'] = _getdropdownvalue ;
  189. pointer['SPAN'] = _getspanvalue;
  190. pointer['DIV'] = _getspanvalue;
  191. pointer['CHECKBOX']= _getcheckboxvalue;
  192. pointer['RADIO'] = _getradiovalue ;
  193. pointer['INPUT'] = _getinputvalue;
  194. return pointer[key](id)
  195. }
  196. /**
  197. * This function returns an attribute for a given DOM object
  198. * @pre jx.dom.exists(id)
  199. * @post: none
  200. */
  201. jx.dom.get.attribute = function(id,field){
  202. _dom = jx.dom.get.instance(id)
  203. if (_dom[field] != null) {
  204. return _dom[field]
  205. }
  206. return _dom.getAttribute(field) ;
  207. }
  208. /**
  209. * This function returns a list of children nodes provided an existing node identifier
  210. * @pre : jx.dom.exists(id) || jx.dom.get.instance(id) != null
  211. * @post: jx.dom.get.children(id).length >= 0
  212. * @param {type} id
  213. * @returns {Array|list}
  214. */
  215. jx.dom.get.children = function(id){
  216. list = [] ;
  217. if(id.constructor == String){
  218. nodes = document.getElementById(id).childNodes ;
  219. }else{
  220. nodes = id.childNodes;
  221. }
  222. for(var i=0; i < nodes.length; i++){
  223. node = nodes[i];
  224. if(node.nodeName.match('^#.*') == null){
  225. list.push(node) ;
  226. }
  227. }
  228. return list ;
  229. }
  230. _setinputvalue = function(id,value){
  231. _input = document.getElementById(id) ;
  232. if(_input.value == null){
  233. _input.innerHTML = value
  234. }else{
  235. _input.value = value;
  236. }
  237. }
  238. /**
  239. * ref _setinputvalue
  240. * The following are aliases intended for a more readable codebase, the just set a value to {DIV,SPAN,INPUT(text|password)}
  241. */
  242. _setspanvalue = _setinputvalue;
  243. _setdivalue = _setinputvalue
  244. _setcheckbox = function(id,value){
  245. _checkboxes = document.getElementsByName(id)
  246. for(i in _checkboxes){
  247. if(checkbox.value == value){
  248. checkbox.checked = true;
  249. break;
  250. }
  251. }
  252. }
  253. _setradiobox = _setcheckbox ;
  254. _setdropdown = function(id,field,value){
  255. _select = document.getElementById(id) ;
  256. if(value == null && field != null){
  257. aux = field ;
  258. field = 'value' ;
  259. value = aux ;
  260. }
  261. options = _select.options;
  262. for(i in options){
  263. if(options[i][field] == value){
  264. options[i].selected = true ;
  265. break;
  266. }
  267. }
  268. }
  269. jx.dom.set = {} ;
  270. /**
  271. * This function will set a value to a dom object on a page
  272. * @pre : jx.dom.exists(id) == true
  273. * @post: No Exceptio is Thrown
  274. */
  275. jx.dom.set.value= function(id,value){
  276. obj = document.getElementById(id) ;
  277. if(obj != null){
  278. key = obj.tagName ;
  279. }else {
  280. obj = document.getElementsByName(id)
  281. if(obj != null){
  282. key = obj[0].tagName ;
  283. }
  284. }
  285. if(key.match(/div|span|textarea/i) || (key.match(/input/i)&& obj.type.match(/text|password/i) ) ){
  286. if(key.match(/input/i) == null){
  287. obj.innerHTML = value ;
  288. }else{
  289. obj.value = value;
  290. }
  291. }
  292. }
  293. /**
  294. * This function sets an attribute a value to a given attribute or creates and sets it otherwise
  295. * @pre : jx.dom.exists(id)
  296. * @post: jx.dom.get.attribute(id,field) == value || or none
  297. */
  298. jx.dom.set.attribute = function(id,field,value){
  299. _dom = jx.dom.get.instance(id);
  300. if (value.constructor == Object) {
  301. _dom[field] = value
  302. } else {
  303. _dom.setAttribute(field,value) ;
  304. }
  305. }
  306. /**
  307. * This function acts as a wrapper to set the style of a dom object
  308. * @returns {undefined}
  309. */
  310. jx.dom.set.style = function(id,field,value){
  311. _dom = jx.dom.get.instance(id) ;
  312. _dom.style[field] = value;
  313. }
  314. /**
  315. * This function will set the css class name of a DOM object and override any existing one
  316. * @pre : jx.dom.exists(id)
  317. * @post: jx.dom.get.attribute(id,'className') == value
  318. * @returns {undefined}
  319. */
  320. jx.dom.set.css = function(id,value){
  321. _dom = jx.dom.get.instance(id) ;
  322. _dom.className = value;
  323. }
  324. /**
  325. * This function will set the focus on a given DOM object
  326. * @param {type} id
  327. * @returns {undefined}
  328. */
  329. jx.dom.set.focus = function(id){
  330. _dom = jx.dom.get.instance(id) ;
  331. _dom.focus();
  332. }