modal.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Steve L. Nyemba
  3. * The modal window creates a modal window on the basis of a url or a html
  4. * The namespace also provides a mechanism for the modal to be removed upon
  5. */
  6. var __jx_modalw__ = {width:0,height:0}
  7. /**
  8. * @param {*} info {uri|html}
  9. */
  10. __jx_modalw__.init = function(info,pointers){
  11. // if(info.match(/^[a-z](+\/[a-z,.]+){1,}/i)){
  12. // var uri = info
  13. // var http = HttpClient.instance()
  14. // http.get(uri,function(x){
  15. // var html = x.responseText
  16. // __jx_modalw__.render.modal(html,pointers)
  17. // })
  18. // }else{
  19. // var html = info
  20. // __jx_modalw__.render.modal(id,html,pointers)
  21. // }
  22. }
  23. __jx_modalw__.cache = {}
  24. __jx_modalw__.render = {}
  25. __jx_modalw__.render.modal = function(args){
  26. var bg = jx.dom.get.instance('DIV')
  27. var frame = jx.dom.get.instance('DIV')
  28. var buttons = jx.dom.get.instance('DIV')
  29. bg.style.position = 'relative'
  30. bg.style.zIndex = 99
  31. bg.style.width = '100%'
  32. bg.style.height = '100%'
  33. bg.style.overflow = 'hidden'
  34. bg.style.backgroundColor= 'rgba(242,242,242,0.7)'
  35. bg.style.display = 'grid'
  36. if(args.url != null){
  37. //
  38. // a modal window that is a reference to another site
  39. bg.style.gridTemplateColumns = '10% 80% 10%'
  40. bg.style.gridTemplateRows = '10% 80% 10%'
  41. var iframe = jx.dom.get.instance('IFRAME')
  42. args.css = (args.css != null)?args.css:{}
  43. iframe.src = args.url
  44. iframe.frameBorder = 0
  45. iframe.style.width = '99%' //(args.css.width == null)?'99%':args.css.width
  46. iframe.style.height= '99%' //(args.css.height==null)?'99%':args.css.height
  47. iframe.style.backgroundColor = '#ffffff'
  48. iframe.className = 'border-round border'
  49. frame.appendChild(iframe)
  50. }else{
  51. //
  52. // This is the case of a modal window that's based on an html document/inline script
  53. //
  54. var text = jx.dom.get.instance('DIV')
  55. text.innerHTML = args.html
  56. frame.appendChild(text)
  57. bg.style.gridTemplateColumns = '20% 60% 20%'
  58. bg.style.gridTemplateRows = '20% 60% 20%'
  59. frame.style.display = 'flex'
  60. frame.style.justifyContent = 'center'
  61. frame.style.alignItems = 'center'
  62. // frame.style.resize = 'both'
  63. text.className = 'border border-round'
  64. text.style.backgroundColor = '#ffffff'
  65. }
  66. frame.style.gridRow = '2/3'
  67. frame.style.gridColumn = '2/3'
  68. bg.appendChild(frame)
  69. if(args.id == null){
  70. //
  71. // If no identifier is assigned to the modal window
  72. // This means it will close upon click on the background
  73. bg.onclick = function(){
  74. jx.dom.remove(this)
  75. }
  76. }else{
  77. //
  78. // We will persist the pane and the calling code will invoke jx.modal.remove
  79. //
  80. jx.modal.set(args.id,bg)
  81. }
  82. args.frame = frame
  83. args.background = bg
  84. return args
  85. }
  86. /**
  87. * This function is designed to layout the pane with the background on it
  88. */
  89. __jx_modalw__.render.show = function(info){
  90. info.background.className = 'jxmodal'
  91. // info.background.style.position = 'absolute'
  92. var parent = (info.target != null)?jx.dom.get.instance(info.target): document.body
  93. var height = $(parent).height()
  94. var width = $(parent).width()
  95. parent.appendChild(info.background)
  96. // if(info.html == null){
  97. info.background.style.top = 0
  98. info.background.style.left =0
  99. info.background.style.position = 'absolute'
  100. // }else{
  101. // $(info.background).width(width+(width*0.01))
  102. // $(info.background).height(height+(height*0.1))
  103. // info.background.style.top = -(height)
  104. // info.background.style.left= -10
  105. // }
  106. }
  107. if(! jx){
  108. var jx = {}
  109. }
  110. /**
  111. * This function will generate a modal window
  112. * @param {*} args {html,url,id}
  113. */
  114. jx.modal = {cache:{}}
  115. jx.modal.show= function(args){
  116. if(args.constructor == String){
  117. if (args.match(/^http|^\//)){
  118. args = {url:args}
  119. }else{
  120. args={html:args}
  121. }
  122. }
  123. var info = __jx_modalw__.render.modal(args)
  124. __jx_modalw__.render.show(info)
  125. if(info.render != null){
  126. if(info.render.args != null){
  127. var args = info.render.args
  128. }else{
  129. var args = null
  130. }
  131. info.render.pointer(args)
  132. }
  133. }
  134. jx.modal.remove = function(id){
  135. if(jx.modal.cache[id] == null){
  136. var pane = $('.jxmodal')[0]
  137. }else{
  138. var pane = jx.modal.cache[id]
  139. }
  140. jx.dom.remove(pane)
  141. }
  142. jx.modal.set = function(id,pane){
  143. jx.modal.cache[id] = pane
  144. delete jx.modal.cache[id]
  145. }
  146. jx.modal.close = jx.modal.remove
  147. // jx.modal.render = function(id,html){
  148. // var info = __jx_modalw__.render.modal(id,html)
  149. // // console.log(info)
  150. // }
  151. jx.modal_example = function(){
  152. busy = '<div class="small" align="center">Please wait</div><i class="fa fa-cog fa-5x faa-wrench animated" style="color:#4682b4"></i><i class="fa fa-cog fa-spin fa-3x" style="color:black"></i><i class="fa fa-cog faa-wrench animated fa-5x" style="color:#4682B4"></i>'
  153. jx.modal(busy)
  154. }