PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/dom/js/WinPeer.js

https://bitbucket.org/bedlaczech/fan-1.0
JavaScript | 224 lines | 161 code | 29 blank | 34 comment | 35 complexity | 22e37631cb3481e870627ed24e11a94d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2009, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 8 Jan 09 Andy Frank Creation
  7. // 20 May 09 Andy Frank Refactor to new OO model
  8. // 8 Jul 09 Andy Frank Split webappClient into sys/dom
  9. //
  10. fan.dom.WinPeer = fan.sys.Obj.$extend(fan.sys.Obj);
  11. //////////////////////////////////////////////////////////////////////////
  12. // Construction
  13. //////////////////////////////////////////////////////////////////////////
  14. fan.dom.WinPeer.prototype.$ctor = function(self)
  15. {
  16. this.win = null;
  17. }
  18. fan.dom.WinPeer.cur = function()
  19. {
  20. if (fan.dom.WinPeer.$cur == null)
  21. {
  22. fan.dom.WinPeer.$cur = fan.dom.Win.make();
  23. fan.dom.WinPeer.$cur.peer.win = window;
  24. }
  25. return fan.dom.WinPeer.$cur;
  26. }
  27. //////////////////////////////////////////////////////////////////////////
  28. // Secondary Windows
  29. //////////////////////////////////////////////////////////////////////////
  30. fan.dom.WinPeer.open = function(uri, name, opts)
  31. {
  32. if (name === undefined) name = null;
  33. if (opts === undefined) opts = null;
  34. var optStr = "";
  35. if (opts != null)
  36. {
  37. var keys = opts.keys();
  38. for (var i=0; i<keys.size(); i++)
  39. {
  40. var key = keys.get(i);
  41. var val = opts.get(key);
  42. if (optStr != null) optStr += ",";
  43. optStr += key + "=" + val;
  44. }
  45. }
  46. var w = fan.dom.Win.make();
  47. if (opts != null) w.peer.win = window.open(uri.encode(), name, optStr);
  48. if (name != null) w.peer.win = window.open(uri.encode(), name);
  49. else w.peer.win = window.open(uri.encode());
  50. return w;
  51. }
  52. fan.dom.WinPeer.prototype.close = function(self)
  53. {
  54. this.win.close();
  55. }
  56. //////////////////////////////////////////////////////////////////////////
  57. // Access
  58. //////////////////////////////////////////////////////////////////////////
  59. fan.dom.WinPeer.prototype.doc = function(self)
  60. {
  61. if (this.$doc == null)
  62. {
  63. this.$doc = fan.dom.Doc.make();
  64. this.$doc.peer.doc = this.win.document;
  65. }
  66. return this.$doc;
  67. }
  68. fan.dom.WinPeer.prototype.alert = function(self, obj)
  69. {
  70. this.win.alert(obj);
  71. }
  72. fan.dom.WinPeer.prototype.viewport = function(self)
  73. {
  74. return (typeof this.win.innerWidth != "undefined")
  75. ? fan.gfx.Size.make(this.win.innerWidth, this.win.innerHeight)
  76. : fan.gfx.Size.make(
  77. this.win.document.documentElement.clientWidth,
  78. this.win.document.documentElement.clientHeight);
  79. }
  80. //////////////////////////////////////////////////////////////////////////
  81. // Uri
  82. //////////////////////////////////////////////////////////////////////////
  83. fan.dom.WinPeer.prototype.uri = function(self)
  84. {
  85. return fan.sys.Uri.decode(this.win.location.toString());
  86. }
  87. fan.dom.WinPeer.prototype.hyperlink = function(self, uri)
  88. {
  89. this.win.location = uri.encode();
  90. }
  91. fan.dom.WinPeer.prototype.reload = function(self, force)
  92. {
  93. this.win.location.reload(force);
  94. }
  95. //////////////////////////////////////////////////////////////////////////
  96. // History
  97. //////////////////////////////////////////////////////////////////////////
  98. fan.dom.WinPeer.prototype.hisBack = function(self) { this.win.history.back(); }
  99. fan.dom.WinPeer.prototype.hisForward = function(self) { this.win.history.forward(); }
  100. fan.dom.WinPeer.prototype.hisPushState = function(self, title, uri, map)
  101. {
  102. // TODO FIXIT: serializtaion
  103. var array = [];
  104. map.each(fan.sys.Func.make(
  105. fan.sys.List.make(fan.sys.Param.$type, [
  106. new fan.sys.Param("val","sys::Obj",false),
  107. new fan.sys.Param("key","sys::Str",false)
  108. ]),
  109. fan.sys.Void.$type,
  110. function(val,key) { array[key] = val }));
  111. this.win.history.pushState(array, title, uri.encode());
  112. }
  113. //////////////////////////////////////////////////////////////////////////
  114. // EventTarget
  115. //////////////////////////////////////////////////////////////////////////
  116. fan.dom.WinPeer.prototype.onEvent = function(self, type, useCapture, handler)
  117. {
  118. var f = function(e)
  119. {
  120. var evt = fan.dom.DomEventPeer.make(e);
  121. if (type == "popstate")
  122. {
  123. // copy state object into Event.meta
  124. // TODO FIXIT: deserializtaion
  125. var array = e.state;
  126. for (var key in array) evt.m_meta.set(key, array[key]);
  127. }
  128. handler.call(evt);
  129. if (type == "beforeunload")
  130. {
  131. var msg = evt.m_meta.get("beforeunloadMsg");
  132. if (msg != null)
  133. {
  134. e.returnValue = msg;
  135. return msg;
  136. }
  137. }
  138. }
  139. if (window.addEventListener)
  140. {
  141. // trap hashchange for non-supporting browsers
  142. if (type == "hashchange" && !("onhashchange" in window))
  143. {
  144. this.fakeHashChange(self, handler);
  145. return;
  146. }
  147. this.win.addEventListener(type, f, useCapture);
  148. }
  149. else
  150. {
  151. this.win.attachEvent('on'+type, f);
  152. }
  153. }
  154. fan.dom.WinPeer.prototype.fakeHashChange = function(self, handler)
  155. {
  156. var $this = this;
  157. var getHash = function()
  158. {
  159. var href = $this.win.location.href;
  160. var index = href.indexOf('#');
  161. return index == -1 ? '' : href.substr(index+1);
  162. }
  163. var oldHash = getHash();
  164. var checkHash = function()
  165. {
  166. var newHash = getHash();
  167. if (oldHash != newHash)
  168. {
  169. oldHash = newHash;
  170. handler.call(fan.dom.DomEventPeer.make(null));
  171. }
  172. }
  173. setInterval(checkHash, 100);
  174. }
  175. //////////////////////////////////////////////////////////////////////////
  176. // Storage
  177. //////////////////////////////////////////////////////////////////////////
  178. fan.dom.WinPeer.prototype.sessionStorage = function(self)
  179. {
  180. if (this.$sessionStorage == null)
  181. {
  182. this.$sessionStorage = fan.dom.Storage.make();
  183. this.$sessionStorage.peer.$instance = this.win.sessionStorage;
  184. }
  185. return this.$sessionStorage;
  186. }
  187. fan.dom.WinPeer.prototype.localStorage = function(self)
  188. {
  189. if (this.$localStorage == null)
  190. {
  191. this.$localStorage = fan.dom.Storage.make();
  192. this.$localStorage.peer.$instance = this.win.localStorage;
  193. }
  194. return this.$localStorage;
  195. }