notify.js 7.3 KB

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