U.MN.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //#region 桌面消息区域
  2. //w3c 消息区域 还在草案中
  3. Namespace.register("U.MN");
  4. U.MN.nif = window.notificationInstance; //消息总控
  5. //#region
  6. if (window.webkitNotifications) {
  7. //消息使用
  8. U.MN._UPS = window.webkitNotifications;
  9. //函数初始化使用
  10. U.M.HEPropert(U.MN._UPS, "onshow", function () {
  11. return this.ondisplay;
  12. },
  13. function (UVE) { this.ondisplay = UVE; });
  14. U.MN.UFN = function (UMT, UDE) {
  15. var _UCE = U.MN._UPS.createNotification(UDE.icon, UMT, UDE.body);
  16. _UCE.show(); return _UCE;
  17. }
  18. //继承消息
  19. U.MN.UFN.prototype = window.webkitNotifications;
  20. U.M.Setprototype(U.MN.UFN, {
  21. requestPermission: function (UCB) { U.MN._UPS.requestPermission(UCB); },
  22. permission: function () {
  23. return { 0: "granted", 1: "denied", 2: "default"}[U.MN._UPS.checkPermission()];
  24. },
  25. close: function () { this.cancel(); }
  26. });
  27. }
  28. //消息初始化
  29. U.MN.Notis = window.Notification = window.Notification || window.webkitNotifications;
  30. //#endregion
  31. //#region
  32. //添加消息提示
  33. U.MN.Noti = function (UDE) {
  34. return U.MN.Noti.get(UDE);
  35. }
  36. //初始化
  37. U.MN.Noti.get = function (UDE) {
  38. var _UDE = U.MN.Notis;
  39. if (_UDE) {
  40. if (_UDE.permission == "granted") {
  41. if (UDE) {
  42. return new U.MN.Noti.init(UDE);
  43. }
  44. } //初始化消息
  45. if (_UDE.permissi0on != "denied") {
  46. _UDE.requestPermission(function (UTF) {
  47. if (UTF == "granted") { U.MN.Noti.get(UDE); }
  48. });
  49. } //消息
  50. }
  51. }
  52. //初始化消息
  53. U.MN.Noti.init = function (UDE) {
  54. this.UNM; this.UT = UDE; this.UE = UDE.e; this.time = UDE.t || 0;
  55. }
  56. U.MN.Noti.checkPermission = function () {
  57. }
  58. //显示所有消息
  59. U.MN.Noti.show = function () {
  60. try { U.MN.nif.show() }
  61. catch (e) { return e; }
  62. }
  63. //清除消息
  64. U.MN.Noti.cancel = function () {
  65. try { U.MN.nif.cancel() }
  66. catch (e) { return e; }
  67. }
  68. //消息功能区域
  69. U.MN.Noti.init.prototype = {
  70. create: function (UMT, UDE, USE) { //创建消息 body dir icon lang onlcick onclose onerrer onshow slient tag title data noscreen renotify sound vibrate
  71. var _UME, _UME, _UTF = UDE == "string", Noti = U.MN.Notis; this.UE = USE = USE ? USE : this.UE;
  72. if (Noti.permission == "granted") {
  73. _UME = this.UNM = new Noti(UMT, UDE);
  74. (USE) && (this.event(USE)); (this.time) && (this.timerc(this.time));
  75. } //允许消息传递
  76. else if (Noti.permission != "denied") { Noti.requestPermission(U.M.apply(this, [[this.Permission, [UMT, UDE]]])); } //认证允许
  77. else { U.Alert("请启动消息推送!"); }
  78. },
  79. Permission: function (UMT, UDE, UTF) {//认证成功后
  80. if (UTF == "granted") { this.create(UMT, UDE); }
  81. },
  82. set: function (UDE) { //消息设置
  83. U.Ut.AddObj(this.UNM, UDE);
  84. },
  85. bind: function (UDE, UAE) { //事件绑定
  86. $(this.UNM).bind(UDE, UAE);
  87. },
  88. unbind: function () {//取消事件绑定
  89. $(this.UNM).unbind(UDE, UAE);
  90. },
  91. event: function (UDE) { //初始化消息
  92. var i, _UND = this.UNM, _UDE = { oc: "onclick", ocl: "onclose", oe: "onerror", os: "onshow" };
  93. for (i in UDE) { _UND[i] = _UND[_UDE[i]] = U.M.apply(this, [[this.aevent, [i, _UDE[i]]]]); }
  94. },
  95. aevent: function (UTF, UTP) {//事件统一出口
  96. var _UDE = this.UE;
  97. if (_UDE && _UDE[UTF]) {
  98. if (U.Ut.isFunction(_UDE[UTF])) {
  99. _UDE[UTF]();
  100. }
  101. else { U.M.apply(this, _UDE[UTF])(); }
  102. if (UTF == "oc" || UTF == "onclick") {//执行函数
  103. this.close();
  104. }
  105. }
  106. },
  107. close: function () {//关闭
  108. if (this.UNM) {
  109. this.UNM.close();
  110. this.dispose();
  111. }
  112. },
  113. dispose: function () { this.UT = this.UE = this.UNM = null; },
  114. timerc: function (UT, UFE) { //指定事件关闭消息
  115. setTimeout(U.M.apply(this, [[UFE], [this.close]]), UT);
  116. },
  117. show: function () { U.MN.show(); },
  118. cancel: function () { U.MN.cancel(); }
  119. }
  120. //#endregion
  121. //#region
  122. //IFrame之间的消息通知。全兼容解决方案,太复杂,需要精简版本。
  123. // 页面消息传递,解决跨域问题。
  124. U.MN.message = function (UDE) {
  125. return new U.MN.message.get.init(UDE);
  126. }
  127. U.MN.message._UTF = ("onmessage" in window);
  128. U.MN.message._UFS = function (UDE) {
  129. try {
  130. var _UME, _USE = "";
  131. if (U.MN.message._UTF) { _USE = UDE.data; }
  132. else { _USE = UDE || window.name; } //设置
  133. _USE = _USE.parseJSON();
  134. _UME = U.MN.message.get.func[_USE[1]];
  135. (_UME) && (_UME.message[_USE[0], _USE[2], _USE[3]]);
  136. } catch (e) { }
  137. };
  138. if (U.MN.message._UTF) {
  139. U.M.AddEvent("message", window, U.MN.message._UFS);
  140. }
  141. U.MN.message.get = U.MN.message.prototype = {
  142. func: {},
  143. ZS: 1,
  144. init: function (UDE) { //初始化消息
  145. U.Ut.AddObj(this, UDE);
  146. this.id = this.id || Guid.guidNoDash();
  147. this.func[this.id] = this;
  148. if (!U.MN.message._UTF) {
  149. if (this.Sender) {
  150. var _USE = this.obj;
  151. (U.Ut.isString(_USE)) && (_USE = $(_USE)[0]);
  152. this.url = U.M.getUrl(_USE.src).host;
  153. this.obj = $$("iframe", { style: { display: "none" }, width: "0", height: "0" }, document.body);
  154. }
  155. else { $().ready(U.M.apply(this, U.MN.message._UFS)); }
  156. }
  157. },
  158. message: function (UDE) {//接收消息
  159. (U.Ut.isFunction(this.me)) && (this.me(UDE));
  160. if (this.Sender && !U.MN.message._UTF) {
  161. var _UD = this.obj.contentWindow.document;
  162. _UD.open();
  163. _UD.write("");
  164. _UD.close();
  165. $(this.obj).remove();
  166. } //释放
  167. },
  168. post: function (UDE, UDID, UCE) {//提交消息
  169. var _UKE, _USE = this.obj;
  170. UDE = U.MS.jsonToStr[UDE, UDID || "", this.id, UCE];
  171. (U.Ut.isString(_USE)) && (_USE = $(_USE)[0]);
  172. if (U.MN.message._UTF) {
  173. try { _USE = _USE.contentWindow || _USE; }
  174. catch (e) { _USE = this.obj; } _USE.postMessage(UDE, "*");
  175. }
  176. else {
  177. if (this.url) {
  178. _USE.src = "about:blank";
  179. U.MD.IframeLoad(_USE, [[U.M.apply(this, [[this.ie, [_USE, UDE]]])]]);
  180. }
  181. else { window.name = UDE; window.location = this.lurl; }
  182. } //消息提示
  183. },
  184. ie: function (USE, UDE) { USE.contentWindow.name = UDE; USE.contentWindow.location.href = this.url; }
  185. }
  186. U.MN.message.get.init.prototype = U.MN.message.get;
  187. U.MN.message.cb = U.MN.message._UFS;
  188. //#endregion