notify.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * 桌面通知插件(支持IE啊)
  3. * createname:雨中磐石
  4. * homeurl:http://www.rockoa.com/
  5. * Copyright (c) 2016 rainrock (xh829.com)
  6. * Date:2016-01-01
  7. * var notify = notifyClass({
  8. * 'sound':'声音文件地址','soundbo':true,'icon':'通知图标'
  9. * });
  10. * notify.showpopup('这是个通知?');
  11. * soundbo 声音提示
  12. * sound 声音文件地址
  13. */
  14. function notifyClass(opts){
  15. var me = this;
  16. this.title = '系统提醒';
  17. this.icon = 'images/logo.png';
  18. this.notbool =true;
  19. this.lastmsg = '';
  20. this.sound = '';
  21. this.sounderr= '';
  22. this.soundbo = true;
  23. this.showbool= false;
  24. this.macos = false;
  25. this._init=function(){
  26. if(opts)for(var o1 in opts)this[o1]=opts[o1];
  27. var strsr = '';
  28. if(typeof(Notification)=='undefined'){
  29. this.notbool=false;
  30. strsr = '<bgsound id="notify_sound_audio" src="" hidden="true" autostart="false" loop="false">';
  31. }else{
  32. strsr = '<audio id="notify_sound_audio" src="web/res/sound/wu.mp3" autoplay="autoplay" hidden="true"></audio>';
  33. }
  34. if(this.sound)$('body').append(strsr);
  35. var llq = navigator.userAgent.toLowerCase();
  36. if(llq.indexOf('reimclient')>0)this.macos = true;
  37. };
  38. this.setsound = function(bo){
  39. this.soundbo=bo;
  40. };
  41. this.getsound = function(){
  42. return this.soundbo;
  43. };
  44. this.opennotify = function(clsfun){
  45. if(!this.notbool)return false;
  46. if(!clsfun)clsfun=function(){};
  47. if(Notification.permission === 'granted')return false;
  48. if(Notification.permission !== 'denied'){
  49. Notification.requestPermission(function (permission){
  50. clsfun();
  51. if(!('permission' in Notification)) {
  52. Notification.permission = permission;
  53. }
  54. if(permission==='granted') {
  55. }
  56. });
  57. }
  58. };
  59. this.showpopup = function(msg,cans){
  60. this.lastmsg = msg;
  61. var can = {body:msg,icon:this.icon,soundbo:this.soundbo,sound:this.sound,tag:'rockwebkitMeteoric',title:this.title,click:function(){}};
  62. if(cans)for(var oi in cans)can[oi]=cans[oi];
  63. var clsfun=can.click,title=can.title;
  64. if(this.showbool)this.show(can);
  65. if(!this.notbool){
  66. this._showpopupie(msg,clsfun,can);
  67. return;
  68. }else{
  69. var lx = this.getaccess();
  70. if(lx!='ok'){
  71. this.opennotify();
  72. }
  73. }
  74. var notification = false;
  75. if(nwjsgui){
  76. localStorage.setItem('xinhuoa_closelx','no');
  77. this.close();
  78. var url =NOWURL+'web/res/js/notification.html?'+Math.random()+'';
  79. localStorage.setItem('xinhuoa_notification', JSON.stringify({icon:can.icon,title:can.title,body:can.body}));
  80. var canss={"frame": false,title:"消息提醒","width": 350,resizable:false,'always_on_top':true,show:false,"height": 110,"show_in_taskbar":false,"transparent":true}
  81. nw.Window.open(url,canss,function(wis){
  82. me.notification = wis;
  83. wis.on('close',function(){
  84. this.close(true);
  85. });
  86. wis.on('closed',function(){
  87. if(localStorage.getItem('xinhuoa_closelx')=='yes'){
  88. var salx=clsfun(can);
  89. if(!salx)nwjs.winshow();
  90. }
  91. me.notification=false;
  92. });
  93. });
  94. }else if(this.macos){
  95. rockclient.rockFun("Notification",{
  96. title:can.title,
  97. msg:can.body
  98. }, function(ret){
  99. });
  100. }else{
  101. var notification= new Notification(title, can);
  102. notification.onclick = function(){
  103. var salx=clsfun(can);
  104. if(!salx)nwjs.winshow();
  105. this.close();
  106. }
  107. }
  108. this.notification = notification;
  109. if(can.soundbo)this.playsound(can.sound);
  110. };
  111. this.close = function(){
  112. try{
  113. if(this.notification)this.notification.close(true);
  114. }catch(e){}
  115. this.notification = false;
  116. };
  117. this.playsound=function(src){
  118. if(!src)src=this.sound;
  119. var boa=document.getElementById('notify_sound_audio');
  120. if(boa){
  121. boa.src=src;
  122. if(boa.play)boa.play();
  123. }
  124. };
  125. this.playerrsound=function(src){
  126. if(!src)src=this.sounderr;
  127. if(src)this.playsound(src);
  128. };
  129. this.getaccess=function(){
  130. var lx = 'none';
  131. if(typeof(Notification)=='undefined'){
  132. lx='ok';
  133. return lx;
  134. }
  135. lx = Notification.permission;
  136. if(lx=='granted'){lx='ok';}else if(lx=='denied'){lx='jz';}else{lx='mr';}
  137. return lx;
  138. };
  139. this._showpopupie=function(msg, clsfun, can){
  140. if(typeof(createPopup)=='undefined')return;
  141. var x = window.screenLeft?window.screenLeft: window.screenX,
  142. y = window.screenTop?window.screenTop: window.screenY;
  143. var w = 310,h=80;
  144. var l = screen.width-x-w-10,
  145. t = screen.height-y-h-60;
  146. var p=window.createPopup();
  147. var pbody=p.document.body;
  148. pbody.style.backgroundColor='#f5f5f5';
  149. pbody.style.border= 'solid #cccccc 1px';
  150. msg = msg.replace(/\n/gi,'<br>');
  151. var s = '<div style="cursor:pointer">';
  152. s+='<span id="createPopup_close" style="position:absolute;right:0px;top:0px;cursor:pointer;">×</span>';
  153. s+='<table id="createPopup_body"><tr valign="top">';
  154. s+='<td style="padding:5px"><img width="60px" src="'+can.icon+'" height="60px"><td>';
  155. s+='<td style="padding:0px 5px"><div style="font-size:14px;line-height:20px;padding-top:3px" align="left"><b>'+can.title+'</b></div><div style="font-size:12px;padding-top:3px;height:50px;overflow:hidden;">'+msg+'</div><td>';
  156. s+='</tr></table>';
  157. s+='</div>';
  158. pbody.innerHTML=s;
  159. p.show(l,t,w,h,document.body);
  160. p.document.getElementById('createPopup_close').onclick=function(){p.hide();};
  161. p.document.getElementById('createPopup_body').onclick=function(){
  162. var salx=clsfun(can);
  163. if(!salx)nwjs.winshow();
  164. p.hide();
  165. };
  166. if(can.soundbo)this.playsound(can.sound);
  167. };
  168. this.getnotifystr=function(ostr){
  169. var slx = '<font color="green">[已开启]</font>';
  170. var olx = this.getaccess();
  171. if(olx=='jz'){
  172. slx = '<font color="red">[已禁止]</font>,<a href="http://www.rockoa.com/view_notify.html" target="_blank">(去设置)</a>';
  173. }
  174. if(olx=='mr'){
  175. slx = '<font color="#ff6600">[未开启]</font>,<a onclick="'+ostr+'" href="javascript:;">[开启]</a>';
  176. }
  177. return slx;
  178. };
  179. //右边提示的
  180. this.show=function(cans){
  181. if(!cans)cans={};
  182. var can = {body:'',icon:'images/todo.png',type:'info',right:'30px',top:'80px',closetime:0,soundbo:this.soundbo,sound:this.sound,title:this.title,click:false,rand:js.getrand()};
  183. if(cans)for(var oi in cans)can[oi]=cans[oi];
  184. var coarr = {
  185. 'info':['#31708f', '#d9edf7','#bce8f1'],
  186. 'success':['#3c763d', '#dff0d8','#d6e9c6'],
  187. 'error':['#a94442', '#f2dede','#ebccd1'],
  188. 'wait':['#8a6d3b', '#fcf8e3','#faebcc']
  189. };
  190. var cos = coarr[can.type],id = 'notify_show_'+can.rand+'';
  191. $('#'+id+'').remove();
  192. var wz = this.showwei(can.right,can.top),mess=can.body
  193. mess = mess.replace(/\n/gi, '<br>');
  194. var s = '<div id="'+id+'" temp="notifyshow" class="boxs" style="position:absolute;z-index:70;right:'+wz[0]+';top:'+wz[1]+';border:1px '+cos[2]+' solid; background:'+cos[1]+';color:'+cos[0]+';border-radius:5px">';
  195. if(can.closetime==0)s+='<div onclick="$(this).parent().fadeOut(function(){$(this).remove()})" style="position:absolute;right:3px;top:0px;cursor:pointer">×</div>';
  196. s+='<table style="margin:15px"><tr valign="top">';
  197. s+=' <td width="53px" align="left"><img style="width:40px;height:40px" src="'+can.icon+'"></td>';
  198. s+=' <td id="'+id+'_td" align="left"><div style="padding-bottom:3px;font-size:14px"><b>'+can.title+'</b></div><div>'+mess+'</div></td>';
  199. s+='</tr></table>';
  200. s+='</div>';
  201. $('body').append(s);
  202. if(can.closetime>0)setTimeout(function(){me.showclose(id)}, can.closetime*1000);
  203. if(typeof(can.click)=='function'){
  204. var clsfun=can.click;
  205. $('#'+id+'_td').click(function(){
  206. var salx=clsfun(can);
  207. me.showclose(id);
  208. });
  209. }
  210. };
  211. this.showwei=function(r,t){
  212. var cas = $("div[temp='notifyshow']");
  213. if(cas.length>0){
  214. var o = cas[cas.length-1];
  215. var t1 = parseInt(o.style.top)+$(o).height()+20;
  216. t = ''+t1+'px';
  217. }
  218. return [r,t];
  219. };
  220. this.showclose=function(id){
  221. $('#'+id+'').fadeOut(function(){$(this).remove();})
  222. }
  223. this._init();
  224. }
粤ICP备19079148号