-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathIm.html
1322 lines (949 loc) · 71.2 KB
/
Im.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>IM演示</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link type="image/x-icon" href="/image/favicon.ico" rel="icon">
<link rel="stylesheet" type="text/css" href="./css/font-awesome.min.css" />
<!--[if IE 7]>
<link rel="stylesheet" href="./css/font-awesome-ie7.min.css">
<![endif]-->
<link rel="stylesheet" type="text/css" href="./css/im.css" />
<script type="text/javascript" src="./script/jquery.js"></script>
<script src="./script/jquery.nanoscroller.min.js"></script>
<script src="./script/promise-1.0.0.min.js"></script>
<script>
WEB_SOCKET_SWF_LOCATION = "./script/flash/WebSocketMain.swf";//用于兼容ie9以及不支持websocket的浏览器
</script>
<script src="./script/flash/swfobject.js"></script>
<script src="./script/flash/web_socket.js"></script>
<!--[if lte IE 8]>
<script src="./script/es5-shim.js"></script>
<![endif]-->
<!--[if lte IE 7]>
<script src="./script/json2.js"></script>
<![endif]-->
</head>
<body>
<script>
// avchat.js
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.AVChatClient=e()}}(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){XMLHttpRequest=typeof XMLHttpRequest==="undefined"?require("xmlhttprequest").XMLHttpRequest:XMLHttpRequest;WebSocket=typeof WebSocket==="undefined"||WebSocket==null?require("ws"):WebSocket;var Promise=require("es6-promise").Promise;var EventEmitter=require("events").EventEmitter;module.exports=AVChatClient;function AVChatClient(settings){if(this instanceof AVChatClient==false){return new AVChatClient(settings)}var _emitter,_settings,_waitCommands,server,ws,keepAliveTimeout;var cmdMap={direct:"ack",sessionopen:"sessionopened",sessionadd:"sessionadded",sessionquery:"sessionquery-result",roomjoin:"roomjoined",roominvite:"roominvited",roomleave:"roomleft",roomkick:"roomkicked"};var timers=[];function auth(peerId,watchingPeerIds){return Promise.resolve({watchingPeerIds:watchingPeerIds||[]})}function groupAuth(peerId,groupId,action,groupPeerIds){return Promise.resolve({groupPeerIds:groupPeerIds||[]})}function initialize(settings){if(!settings)throw new Error("settings");if(!settings.appId)throw new Error("settings.appId");if(!settings.peerId)throw new Error("settings.peerId");if(settings.auth&&typeof settings.auth!="function"){throw new Error("sesstings.auth")}if(settings.groupAuth&&typeof settings.groupAuth!="function"){throw new Error("sesstings.groupAuth")}_settings=settings||{};_settings.auth=settings.auth||auth;_settings.groupAuth=settings.groupAuth||groupAuth;_settings.watchingPeerIds=settings.watchingPeerIds||[];_settings.sp=settings.sp;_settings.server=settings.server;_waitCommands=[];_emitter=new EventEmitter;keepAliveTimeout=6e4}initialize(settings);function _getServerInfo(appId,secure){var protocol="http://";if(typeof window!="undefined"&&window.location&&window.location.protocol=="https:"){protocol="https://"}var url=protocol+"router-g0-push.avoscloud.com/v1/route?appId="+appId;if(_settings.server&&_settings.server=="us"){url=protocol+"router-a0-push.avoscloud.com/v1/route?appId="+appId}if(secure){url+="&secure=1"}return get(url)}function _connect(){if(server&&new Date<server.expires){return new Promise(function(resolve,reject){ws=new WebSocket(server.server);_timeout("connectopen",function(){reject()});ws.onopen=function(){if(timers.length>0){clearTimeout(timers.shift()[1])}resolve(server)};ws.onclose=function(e){doClose();_emitter.emit("close",e)};ws.onmessage=function(message){var data=JSON.parse(message.data);var cmd=data.op?data.cmd+data.op:data.cmd;if(!cmd){cmd="{}"}if(_waitCommands.length>0&&_waitCommands[0][0]===cmd){_waitCommands.shift()[1](data)}if(timers.length>0&&timers[0][0]==cmd){clearTimeout(timers.shift()[1])}if(data.cmd=="session"){if(data.op=="opened"||data.op=="added"){_emitter.emit("online",data.onlineSessionPeerIds)}}else if(data.cmd=="presence"){if(data.status=="on"){_emitter.emit("online",data.sessionPeerIds)}else if(data.status=="off"){_emitter.emit("offline",data.sessionPeerIds)}}else if(data.cmd=="direct"){_emitter.emit("message",data);var msg={cmd:"ack",peerId:_settings.peerId,appId:_settings.appId,ids:[].concat(data.id)};var s=JSON.stringify(msg);ws.send(s)}else if(data.cmd=="room"){if(data.op=="members-joined"){_emitter.emit("membersJoined",data)}else if(data.op=="members-left"){_emitter.emit("membersLeft",data)}else if(data.op=="joined"){_emitter.emit("joined",data)}else if(data.op=="left"){_emitter.emit("left",data)}}}})}else{return _getServerInfo(_settings.appId,_settings.secure).then(function(result){server=result;server.expires=Date.now()+server.ttl*1e3;return _connect()})}}function _openSession(){return _settings.auth(_settings.peerId,_settings.watchingPeerIds,_settings.sp).then(function(data){_settings.watchingPeerIds=data.watchingPeerIds;return doCommand("session","open",{sessionPeerIds:data.watchingPeerIds,s:data.s,t:data.t,n:data.n,sp:data.sp})})}function _timeout(name,reject){timers.push([name,setTimeout(function(){if(reject){reject(name+"timeout")}doClose()},1e4)])}function _keepAlive(){clearTimeout(_keepAlive.handle);_keepAlive.handle=setTimeout(function(){if(ws.readyState==1){ws.send("{}");_timeout("{}");_keepAlive()}},keepAliveTimeout)}function doClose(){ws.close();clearTimeout(_keepAlive.handle);timers.forEach(function(v,i){clearTimeout(v[1])});_waitCommands.forEach(function(v){v[2]()});timers=[];_waitCommands=[]}function doCommand(cmd,op,props){_keepAlive();var msg={cmd:cmd,peerId:_settings.peerId,appId:_settings.appId};if(op){msg.op=op}if(props){for(k in props){msg[k]=props[k]}}if(!ws){return Promise.reject()}if(ws.readyState!=1){return Promise.reject(ws.readyState)}ws.send(JSON.stringify(msg));var c=typeof op=="undefined"?cmd:cmd+op;if(cmd=="direct"&&props.transient==true||["sessionremove","sessionclose"].indexOf(c)>-1){return Promise.resolve()}else{return new Promise(function(resolve,reject){_waitCommands.push([cmdMap[c]||c,resolve,reject]);_timeout(cmdMap[c]||c,reject)})}}this.open=function(){if(ws&&ws.readyState==0){return Promise.reject(0)}if(ws&&ws.readyState==1){return Promise.resolve()}timers.forEach(function(v,i){clearTimeout(v[1])});timers=[];return _connect().then(function(){return _openSession()})};this.close=function(){doCommand("session","close");doClose();return Promise.resolve()};this.send=function(msg,to,transient){var obj={msg:msg,toPeerIds:[].concat(to)};if(typeof transient!="undefined"&&transient==true){obj.transient=transient}return doCommand("direct",undefined,obj)};this.on=function(name,func){_emitter.on(name,func)};this.watch=function(peers){return _settings.auth(_settings.peerId,[].concat(peers)).then(function(data){var watch=[].concat(data.watchingPeerIds);watch.forEach(function(v,k){if(_settings.watchingPeerIds.indexOf(v)==-1){_settings.watchingPeerIds.push(v)}});return doCommand("session","add",{sessionPeerIds:[].concat(data.watchingPeerIds),s:data.s,t:data.t,n:data.n})})};this.unwatch=function(peers){peers.forEach(function(v,k){if(_settings.watchingPeerIds.indexOf(v)>-1){_settings.watchingPeerIds.splice(_settings.watchingPeerIds.indexOf(v),1)}});return doCommand("session","remove",{sessionPeerIds:[].concat(peers)})};this.getStatus=function(peers){return doCommand("session","query",{sessionPeerIds:[].concat(peers)})};this.joinGroup=function(groupId){return _settings.groupAuth(_settings.peerId,groupId,"join",[]).then(function(data){return doCommand("room","join",{roomId:groupId,s:data.s,t:data.t,n:data.n})})};this.sendToGroup=function(msg,groupId,transient){var obj={msg:msg,roomId:groupId};if(typeof transient!="undefined"&&transient==true){obj.transient=transient}return doCommand("direct",undefined,obj)};this.inviteToGroup=function(groupId,groupPeerIds){return _settings.groupAuth(_settings.peerId,groupId,"invite",[].concat(groupPeerIds)).then(function(data){return doCommand("room","invite",{roomId:groupId,roomPeerIds:[].concat(data.groupPeerIds),s:data.s,t:data.t,n:data.n})})};this.kickFromGroup=function(groupId,groupPeerIds){return _settings.groupAuth(_settings.peerId,groupId,"kick",[].concat(groupPeerIds)).then(function(data){return doCommand("room","kick",{roomId:groupId,roomPeerIds:[].concat(groupPeerIds),s:data.s,t:data.t,n:data.n})})};this.leaveGroup=function(groupId){return doCommand("room","leave",{roomId:groupId})}}function get(url){if(typeof jQuery!=="undefined"){return Promise.resolve(jQuery.getJSON.call(jQuery,url+="&cb=?"))}else{return new Promise(function(resolve,reject){var req=new XMLHttpRequest;req.open("GET",url);req.onload=function(){if(req.status==200){resolve(JSON.parse(req.responseText))}else{reject(Error(req.statusText))}};req.onerror=function(){reject(Error("Network Error"))};req.send()})}}},{"es6-promise":2,events:12,ws:undefined,xmlhttprequest:undefined}],2:[function(require,module,exports){"use strict";var Promise=require("./promise/promise").Promise;var polyfill=require("./promise/polyfill").polyfill;exports.Promise=Promise;exports.polyfill=polyfill},{"./promise/polyfill":6,"./promise/promise":7}],3:[function(require,module,exports){"use strict";var isArray=require("./utils").isArray;var isFunction=require("./utils").isFunction;function all(promises){var Promise=this;if(!isArray(promises)){throw new TypeError("You must pass an array to all.")}return new Promise(function(resolve,reject){var results=[],remaining=promises.length,promise;if(remaining===0){resolve([])}function resolver(index){return function(value){resolveAll(index,value)}}function resolveAll(index,value){results[index]=value;if(--remaining===0){resolve(results)}}for(var i=0;i<promises.length;i++){promise=promises[i];if(promise&&isFunction(promise.then)){promise.then(resolver(i),reject)}else{resolveAll(i,promise)}}})}exports.all=all},{"./utils":11}],4:[function(require,module,exports){(function(process,global){"use strict";var browserGlobal=typeof window!=="undefined"?window:{};var BrowserMutationObserver=browserGlobal.MutationObserver||browserGlobal.WebKitMutationObserver;var local=typeof global!=="undefined"?global:this===undefined?window:this;function useNextTick(){return function(){process.nextTick(flush)}}function useMutationObserver(){var iterations=0;var observer=new BrowserMutationObserver(flush);var node=document.createTextNode("");observer.observe(node,{characterData:true});return function(){node.data=iterations=++iterations%2}}function useSetTimeout(){return function(){local.setTimeout(flush,1)}}var queue=[];function flush(){for(var i=0;i<queue.length;i++){var tuple=queue[i];var callback=tuple[0],arg=tuple[1];callback(arg)}queue=[]}var scheduleFlush;if(typeof process!=="undefined"&&{}.toString.call(process)==="[object process]"){scheduleFlush=useNextTick()}else if(BrowserMutationObserver){scheduleFlush=useMutationObserver()}else{scheduleFlush=useSetTimeout()}function asap(callback,arg){var length=queue.push([callback,arg]);if(length===1){scheduleFlush()}}exports.asap=asap}).call(this,require("_process"),typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{_process:13}],5:[function(require,module,exports){"use strict";var config={instrument:false};function configure(name,value){if(arguments.length===2){config[name]=value}else{return config[name]}}exports.config=config;exports.configure=configure},{}],6:[function(require,module,exports){(function(global){"use strict";var RSVPPromise=require("./promise").Promise;var isFunction=require("./utils").isFunction;function polyfill(){var local;if(typeof global!=="undefined"){local=global}else if(typeof window!=="undefined"&&window.document){local=window}else{local=self}var es6PromiseSupport="Promise"in local&&"resolve"in local.Promise&&"reject"in local.Promise&&"all"in local.Promise&&"race"in local.Promise&&function(){var resolve;new local.Promise(function(r){resolve=r});return isFunction(resolve)}();if(!es6PromiseSupport){local.Promise=RSVPPromise}}exports.polyfill=polyfill}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"./promise":7,"./utils":11}],7:[function(require,module,exports){"use strict";var config=require("./config").config;var configure=require("./config").configure;var objectOrFunction=require("./utils").objectOrFunction;var isFunction=require("./utils").isFunction;var now=require("./utils").now;var all=require("./all").all;var race=require("./race").race;var staticResolve=require("./resolve").resolve;var staticReject=require("./reject").reject;var asap=require("./asap").asap;var counter=0;config.async=asap;function Promise(resolver){if(!isFunction(resolver)){throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")}if(!(this instanceof Promise)){throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")}this._subscribers=[];invokeResolver(resolver,this)}function invokeResolver(resolver,promise){function resolvePromise(value){resolve(promise,value)}function rejectPromise(reason){reject(promise,reason)}try{resolver(resolvePromise,rejectPromise)}catch(e){rejectPromise(e)}}function invokeCallback(settled,promise,callback,detail){var hasCallback=isFunction(callback),value,error,succeeded,failed;if(hasCallback){try{value=callback(detail);succeeded=true}catch(e){failed=true;error=e}}else{value=detail;succeeded=true}if(handleThenable(promise,value)){return}else if(hasCallback&&succeeded){resolve(promise,value)}else if(failed){reject(promise,error)}else if(settled===FULFILLED){resolve(promise,value)}else if(settled===REJECTED){reject(promise,value)}}var PENDING=void 0;var SEALED=0;var FULFILLED=1;var REJECTED=2;function subscribe(parent,child,onFulfillment,onRejection){var subscribers=parent._subscribers;var length=subscribers.length;subscribers[length]=child;subscribers[length+FULFILLED]=onFulfillment;subscribers[length+REJECTED]=onRejection}function publish(promise,settled){var child,callback,subscribers=promise._subscribers,detail=promise._detail;for(var i=0;i<subscribers.length;i+=3){child=subscribers[i];callback=subscribers[i+settled];invokeCallback(settled,child,callback,detail)}promise._subscribers=null}Promise.prototype={constructor:Promise,_state:undefined,_detail:undefined,_subscribers:undefined,then:function(onFulfillment,onRejection){var promise=this;var thenPromise=new this.constructor(function(){});if(this._state){var callbacks=arguments;config.async(function invokePromiseCallback(){invokeCallback(promise._state,thenPromise,callbacks[promise._state-1],promise._detail)})}else{subscribe(this,thenPromise,onFulfillment,onRejection)}return thenPromise},"catch":function(onRejection){return this.then(null,onRejection)}};Promise.all=all;Promise.race=race;Promise.resolve=staticResolve;Promise.reject=staticReject;function handleThenable(promise,value){var then=null,resolved;try{if(promise===value){throw new TypeError("A promises callback cannot return that same promise.")}if(objectOrFunction(value)){then=value.then;if(isFunction(then)){then.call(value,function(val){if(resolved){return true}resolved=true;if(value!==val){resolve(promise,val)}else{fulfill(promise,val)}},function(val){if(resolved){return true}resolved=true;reject(promise,val)});return true}}}catch(error){if(resolved){return true}reject(promise,error);return true}return false}function resolve(promise,value){if(promise===value){fulfill(promise,value)}else if(!handleThenable(promise,value)){fulfill(promise,value)}}function fulfill(promise,value){if(promise._state!==PENDING){return}promise._state=SEALED;promise._detail=value;config.async(publishFulfillment,promise)}function reject(promise,reason){if(promise._state!==PENDING){return}promise._state=SEALED;promise._detail=reason;config.async(publishRejection,promise)}function publishFulfillment(promise){publish(promise,promise._state=FULFILLED)}function publishRejection(promise){publish(promise,promise._state=REJECTED)}exports.Promise=Promise},{"./all":3,"./asap":4,"./config":5,"./race":8,"./reject":9,"./resolve":10,"./utils":11}],8:[function(require,module,exports){"use strict";var isArray=require("./utils").isArray;function race(promises){var Promise=this;if(!isArray(promises)){throw new TypeError("You must pass an array to race.")}return new Promise(function(resolve,reject){var results=[],promise;for(var i=0;i<promises.length;i++){promise=promises[i];if(promise&&typeof promise.then==="function"){promise.then(resolve,reject)}else{resolve(promise)}}})}exports.race=race},{"./utils":11}],9:[function(require,module,exports){"use strict";function reject(reason){var Promise=this;return new Promise(function(resolve,reject){reject(reason)})}exports.reject=reject},{}],10:[function(require,module,exports){"use strict";function resolve(value){if(value&&typeof value==="object"&&value.constructor===this){return value}var Promise=this;return new Promise(function(resolve){resolve(value)})}exports.resolve=resolve},{}],11:[function(require,module,exports){"use strict";function objectOrFunction(x){return isFunction(x)||typeof x==="object"&&x!==null}function isFunction(x){return typeof x==="function"}function isArray(x){return Object.prototype.toString.call(x)==="[object Array]"}var now=Date.now||function(){return(new Date).getTime()};exports.objectOrFunction=objectOrFunction;exports.isFunction=isFunction;exports.isArray=isArray;exports.now=now},{}],12:[function(require,module,exports){function EventEmitter(){this._events=this._events||{};this._maxListeners=this._maxListeners||undefined}module.exports=EventEmitter;EventEmitter.EventEmitter=EventEmitter;EventEmitter.prototype._events=undefined;EventEmitter.prototype._maxListeners=undefined;EventEmitter.defaultMaxListeners=10;EventEmitter.prototype.setMaxListeners=function(n){if(!isNumber(n)||n<0||isNaN(n))throw TypeError("n must be a positive number");this._maxListeners=n;return this};EventEmitter.prototype.emit=function(type){var er,handler,len,args,i,listeners;if(!this._events)this._events={};if(type==="error"){if(!this._events.error||isObject(this._events.error)&&!this._events.error.length){er=arguments[1];if(er instanceof Error){throw er}throw TypeError('Uncaught, unspecified "error" event.')}}handler=this._events[type];if(isUndefined(handler))return false;if(isFunction(handler)){switch(arguments.length){case 1:handler.call(this);break;case 2:handler.call(this,arguments[1]);break;case 3:handler.call(this,arguments[1],arguments[2]);break;default:len=arguments.length;args=new Array(len-1);for(i=1;i<len;i++)args[i-1]=arguments[i];handler.apply(this,args)}}else if(isObject(handler)){len=arguments.length;args=new Array(len-1);for(i=1;i<len;i++)args[i-1]=arguments[i];listeners=handler.slice();len=listeners.length;for(i=0;i<len;i++)listeners[i].apply(this,args)}return true};EventEmitter.prototype.addListener=function(type,listener){var m;if(!isFunction(listener))throw TypeError("listener must be a function");if(!this._events)this._events={};if(this._events.newListener)this.emit("newListener",type,isFunction(listener.listener)?listener.listener:listener);if(!this._events[type])this._events[type]=listener;else if(isObject(this._events[type]))this._events[type].push(listener);else this._events[type]=[this._events[type],listener];if(isObject(this._events[type])&&!this._events[type].warned){var m;if(!isUndefined(this._maxListeners)){m=this._maxListeners}else{m=EventEmitter.defaultMaxListeners}if(m&&m>0&&this._events[type].length>m){this._events[type].warned=true;console.error("(node) warning: possible EventEmitter memory "+"leak detected. %d listeners added. "+"Use emitter.setMaxListeners() to increase limit.",this._events[type].length);if(typeof console.trace==="function"){console.trace()}}}return this};EventEmitter.prototype.on=EventEmitter.prototype.addListener;EventEmitter.prototype.once=function(type,listener){if(!isFunction(listener))throw TypeError("listener must be a function");var fired=false;function g(){this.removeListener(type,g);if(!fired){fired=true;listener.apply(this,arguments)}}g.listener=listener;this.on(type,g);return this};EventEmitter.prototype.removeListener=function(type,listener){var list,position,length,i;if(!isFunction(listener))throw TypeError("listener must be a function");if(!this._events||!this._events[type])return this;list=this._events[type];length=list.length;position=-1;if(list===listener||isFunction(list.listener)&&list.listener===listener){delete this._events[type];if(this._events.removeListener)this.emit("removeListener",type,listener)}else if(isObject(list)){for(i=length;i-->0;){if(list[i]===listener||list[i].listener&&list[i].listener===listener){position=i;break}}if(position<0)return this;if(list.length===1){list.length=0;delete this._events[type]}else{list.splice(position,1)}if(this._events.removeListener)this.emit("removeListener",type,listener)}return this};EventEmitter.prototype.removeAllListeners=function(type){var key,listeners;if(!this._events)return this;if(!this._events.removeListener){if(arguments.length===0)this._events={};else if(this._events[type])delete this._events[type];return this}if(arguments.length===0){for(key in this._events){if(key==="removeListener")continue;this.removeAllListeners(key)}this.removeAllListeners("removeListener");this._events={};return this}listeners=this._events[type];if(isFunction(listeners)){this.removeListener(type,listeners)}else{while(listeners.length)this.removeListener(type,listeners[listeners.length-1])}delete this._events[type];return this};EventEmitter.prototype.listeners=function(type){var ret;if(!this._events||!this._events[type])ret=[];else if(isFunction(this._events[type]))ret=[this._events[type]];else ret=this._events[type].slice();return ret};EventEmitter.listenerCount=function(emitter,type){var ret;if(!emitter._events||!emitter._events[type])ret=0;else if(isFunction(emitter._events[type]))ret=1;else ret=emitter._events[type].length;return ret};function isFunction(arg){return typeof arg==="function"}function isNumber(arg){return typeof arg==="number"}function isObject(arg){return typeof arg==="object"&&arg!==null}function isUndefined(arg){return arg===void 0}},{}],13:[function(require,module,exports){var process=module.exports={};process.nextTick=function(){var canSetImmediate=typeof window!=="undefined"&&window.setImmediate;var canMutationObserver=typeof window!=="undefined"&&window.MutationObserver;var canPost=typeof window!=="undefined"&&window.postMessage&&window.addEventListener;if(canSetImmediate){return function(f){return window.setImmediate(f)}}var queue=[];if(canMutationObserver){var hiddenDiv=document.createElement("div");var observer=new MutationObserver(function(){var queueList=queue.slice();queue.length=0;queueList.forEach(function(fn){fn()})});observer.observe(hiddenDiv,{attributes:true});return function nextTick(fn){if(!queue.length){hiddenDiv.setAttribute("yes","no")}queue.push(fn)}}if(canPost){window.addEventListener("message",function(ev){var source=ev.source;if((source===window||source===null)&&ev.data==="process-tick"){ev.stopPropagation();if(queue.length>0){var fn=queue.shift();fn()}}},true);return function nextTick(fn){queue.push(fn);window.postMessage("process-tick","*")}}return function nextTick(fn){setTimeout(fn,0)}}();process.title="browser";process.browser=true;process.env={};process.argv=[];function noop(){}process.on=noop;process.addListener=noop;process.once=noop;process.off=noop;process.removeListener=noop;process.removeAllListeners=noop;process.emit=noop;process.binding=function(name){throw new Error("process.binding is not supported")};process.cwd=function(){return"/"};process.chdir=function(dir){throw new Error("process.chdir is not supported")}},{}]},{},[1])(1)});
// EventEmitter.js
(function(){"use strict";function t(){}function i(t,n){for(var e=t.length;e--;)if(t[e].listener===n)return e;return-1}function n(e){return function(){return this[e].apply(this,arguments)}}var e=t.prototype,r=this,s=r.EventEmitter;e.getListeners=function(n){var r,e,t=this._getEvents();if(n instanceof RegExp){r={};for(e in t)t.hasOwnProperty(e)&&n.test(e)&&(r[e]=t[e])}else r=t[n]||(t[n]=[]);return r},e.flattenListeners=function(t){var e,n=[];for(e=0;e<t.length;e+=1)n.push(t[e].listener);return n},e.getListenersAsObject=function(n){var e,t=this.getListeners(n);return t instanceof Array&&(e={},e[n]=t),e||t},e.addListener=function(r,e){var t,n=this.getListenersAsObject(r),s="object"==typeof e;for(t in n)n.hasOwnProperty(t)&&-1===i(n[t],e)&&n[t].push(s?e:{listener:e,once:!1});return this},e.on=n("addListener"),e.addOnceListener=function(e,t){return this.addListener(e,{listener:t,once:!0})},e.once=n("addOnceListener"),e.defineEvent=function(e){return this.getListeners(e),this},e.defineEvents=function(t){for(var e=0;e<t.length;e+=1)this.defineEvent(t[e]);return this},e.removeListener=function(r,s){var n,e,t=this.getListenersAsObject(r);for(e in t)t.hasOwnProperty(e)&&(n=i(t[e],s),-1!==n&&t[e].splice(n,1));return this},e.off=n("removeListener"),e.addListeners=function(e,t){return this.manipulateListeners(!1,e,t)},e.removeListeners=function(e,t){return this.manipulateListeners(!0,e,t)},e.manipulateListeners=function(r,t,i){var e,n,s=r?this.removeListener:this.addListener,o=r?this.removeListeners:this.addListeners;if("object"!=typeof t||t instanceof RegExp)for(e=i.length;e--;)s.call(this,t,i[e]);else for(e in t)t.hasOwnProperty(e)&&(n=t[e])&&("function"==typeof n?s.call(this,e,n):o.call(this,e,n));return this},e.removeEvent=function(e){var t,r=typeof e,n=this._getEvents();if("string"===r)delete n[e];else if(e instanceof RegExp)for(t in n)n.hasOwnProperty(t)&&e.test(t)&&delete n[t];else delete this._events;return this},e.removeAllListeners=n("removeEvent"),e.emitEvent=function(r,o){var e,i,t,s,n=this.getListenersAsObject(r);for(t in n)if(n.hasOwnProperty(t))for(i=n[t].length;i--;)e=n[t][i],e.once===!0&&this.removeListener(r,e.listener),s=e.listener.apply(this,o||[]),s===this._getOnceReturnValue()&&this.removeListener(r,e.listener);return this},e.trigger=n("emitEvent"),e.emit=function(e){var t=Array.prototype.slice.call(arguments,1);return this.emitEvent(e,t)},e.setOnceReturnValue=function(e){return this._onceReturnValue=e,this},e._getOnceReturnValue=function(){return this.hasOwnProperty("_onceReturnValue")?this._onceReturnValue:!0},e._getEvents=function(){return this._events||(this._events={})},t.noConflict=function(){return r.EventEmitter=s,t},"function"==typeof define&&define.amd?define(function(){return t}):"object"==typeof module&&module.exports?module.exports=t:r.EventEmitter=t}).call(this);
window.Green = function (OldGreen) {
var Green = {
noConflict: noConflict
};
return Green;
function noConflict() {
if (window.Green === Green) {
window.Green = OldGreen;
}
return Green;
}
}(window.Green);
Green._EventHandler = function(window, document, Green, EventEmitter) {
return new EventEmitter();
}(window, document, Green, EventEmitter);
Green._Utils = function (window, document, Green) {
function isDifferentDay(prev, next) {
return (
(next.getDate() != prev.getDate()) ||
(next.getFullYear() != prev.getFullYear()) ||
(next.getMonth() != prev.getMonth())
);
};
function tranTime(date) {
return this.isDifferentDay( date, new Date() ) ?
( parseInt( date.getMonth() + 1 ) + '-' + date.getDate() + ' ' + date.toTimeString().substr(0,5) ) :
date.toTimeString().substr(0,5);
};
function template(tpl, data) {
var re = /<%([^%>]+)?%>/g,
reExp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g,
code = 'var r=[];\n',
cursor = 0;
var add = function(line, js) {
js? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
(code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
return add;
}
while(match = re.exec(html)) {
add(html.slice(cursor, match.index))(match[1], true);
cursor = match.index + match[0].length;
}
add(html.substr(cursor, html.length - cursor));
code += 'return r.join("");';
return new Function(code.replace(/[\r\t\n]/g, '')).apply(options);
};
function freeTextarea($dom, maxHeight) {
var ta = $dom,
maxHeight = maxHeight || 70,
css = (function() {
var css = {}, i = 8,
z = 'width fontSize fontFamily lineHeight wordWrap wordBreak whiteSpace letterSpacing'.split(' ');
while(i--) css[z[i]] = ta.css(z[i]);
return $.extend(css, {position : 'absolute', left : -9999, top : 0});
})(),
_ta=ta.clone().css(css).attr({id : '', name : '', tabIndex : -1}),
stCur, valCur, defHeight = ta.height(),
both=$([ta[0], _ta[0]]),
autoHeight = function() {
valCur = ta.val();
_ta.val(valCur).height(1).scrollTop(9999);
stCur = Math.min(Math.max(defHeight, _ta.scrollTop()), maxHeight);
both.height(stCur);
};
ta.after(_ta).bind("focus input change propertychange", autoHeight);
};
return {
isDifferentDay : isDifferentDay,
tranTime : tranTime,
template : template,
freeTextarea : freeTextarea
};
}(window, document, Green);
// 状态提示组件
Green._StatusView = function(window, document, Green, Utils) {
var StatusView = {};
StatusView.commit = function() {
var render = '<div class="status-view"></div>';
this.dom = $(render);
return this.dom;
};
StatusView.msg = function(log) {
this.dom.html(log).show();
};
StatusView.hide = function() {
this.dom.hide();
};
return StatusView;
}(window, document, Green, EventEmitter);
Green._View = function(window, document, Green, EventEmitter) {
function View() {
}
}(window, document, Green, EventEmitter);
// 联系人组件
Green._RosterView = function (window, document, Green, Utils, EventHandler) {
function RosterView(roster) {
this.roster = roster;
}
RosterView.prototype.commit = function render() {
var render = '<div class="roster cf" peer="'+this.roster.id+'">' +
'<span class="unread-dot" style="display:none;"></span>' +
'<span class="unread-dots" style="display:none"></span>' +
'<span class="chat-status '+this.roster.onlineStatus+'"></span>' +
'<div class="avatar-wrap">' +
'<img class="avatar" src="'+this.roster.image+'">' +
'</div>' +
'<div class="info">' +
'<div class="nick-name">' +
'<div class="name" style="">'+this.roster.name+'</div>' +
'</div>' +
'</div>' +
'</div>';
this.dom = $(render);
this.dom.click($.proxy(function() {
EventHandler.trigger('rosterClick', [this]);
}, this));
return this.dom;
};
RosterView.prototype.updateUnread = function(num) {
var unreadDom = this.dom.find(".unread-dot");
this.roster.unreadCount = num;
if(num == 0) {
unreadDom.hide();
}
else {
unreadDom.html(num).show();
}
};
RosterView.prototype.updateOnlineStatus = function(status) {
this.roster.onlineStatus = status;
this.dom.find(".chat-status").removeClass().addClass( 'chat-status ' + status);
};
return RosterView;
}(window, document, Green, Green._Utils, Green._EventHandler);
// 联系人列表组件
Green._RosterListView = function(window, document, Green, Utils, EventHandler, RosterView) {
var RosterListView = {};
RosterListView.commit = function(rosters) {
var render = '<div class="contacts">'+
'<div class="title"><i class="icon-user"></i> <b>聊天联系人</b></div>'+
'<div class="roster-list"><div class="nano"><div class="active nano-content"></div></div></div>'+
'<div class="bottom cf"><div class="online-status"><div class="me-status"><span class="chat-status online"></span><div class="status-text">在线</div></div></div><div class="toggle-btn"><div class="backward" title="展开"> <i class="icon-step-backward"></i></div><div class="forward" title="收缩"><i class="icon-step-forward"></i> <b>收缩</b></div></div></div>'+
'</div>';
this.dom = $(render);
this.rosterViews = {};
$.each(rosters, function(id, roster) {
var rosterView = new RosterView( roster );
RosterListView.rosterViews[id] = rosterView;
RosterListView.dom.find('.active').append( rosterView.commit() );
});
// 精简模式 详细模式转换
this.dom.find('.title, .toggle-btn').click(function() {
EventHandler.trigger('rosterListViewToggle', [!RosterListView.dom.hasClass('contacts-minus')]);
});
return this.dom;
};
RosterListView.init = function() {
var titHeight = 40;
var botHeight = 30;
var offset = 62;
var listHeight = $(window).height() - offset;
var innerHeight = $(window).height() - offset - titHeight - botHeight;
var domNano = RosterListView.dom.find('.nano');
domNano.css('height', innerHeight + 'px');
domNano.nanoScroller();
this.height = listHeight;
this.innerHeight = innerHeight;
};
RosterListView.get = function(id) {
return this.rosterViews[id];
};
RosterListView.toggle = function(status) {
if( status ) {
this.dom.addClass('contacts-minus');
}
else {
this.dom.removeClass('contacts-minus');
}
};
RosterListView.prepend = function(id) {
this.get(id).dom.prependTo( this.dom.find('.active') );
};
RosterListView.hide = function() {
this.dom.hide();
};
RosterListView.show = function() {
this.dom.show();
};
return RosterListView;
}(window, document, Green, Green._Utils, Green._EventHandler, Green._RosterView);
// 会话窗口组件
Green._ConverseView = function(window, document, Green, Utils, EventHandler) {
function ConverseView(roster) {
this.roster = roster;
}
ConverseView.prototype.commit = function() {
var render = '<div class="nano" peer="'+this.roster.id+'">'+
'<div class="nano-content messages">'+
'<div class="no-more-history"><i class="icon-ban-circle"></i><b>无更多历史消息</b></div>'+
'<div class="loadmore"><div class="linit"><i class="icon-time"></i><b>更多历史消息</b></div><div class="lloading"><img src="./image/loading-cir.gif">历史消息加载中...</div></div>'+
'</div>'+
'</div>';
this.dom = $(render);
_init.call(this);
return this.dom;
};
function _init() {
var domLoadmore = this.dom.find('.loadmore');
domLoadmore.click($.proxy(function() {
if( domLoadmore.hasClass('loadmore-loading') ) {
return false;
}
domLoadmore.addClass('loadmore-loading');
// AVHistoryMessageQuery
var data = {
convid : [Green.me.id, this.roster.id],
timestamp : this.lastMessage.timestamp,
limit : 30
};
EventHandler.trigger('getHistory', [data, this]);
}, this));
this.lastMessage = {};
// 是否有未读的消息
var unreadMessages = this.roster.unreadMessages;
if( unreadMessages.length == 0 ) {
this.lastMessage['timestamp'] = new Date().getTime();
domLoadmore.trigger('click');// think think!!!!
}
else {
this.lastMessage = unreadMessages[0];
this.renderMessage(unreadMessages, 'receive')
this.roster.unreadMessages = [];
}
};
ConverseView.prototype.renderMessage = function( messages, mode ) {
if( !(messages instanceof Array) ) messages = [messages];
var renderStr = '';
var date;
var message;
var sendStatus;
for(var i=0, len=messages.length; i<len; i++ ) {
date = Utils.tranTime( new Date(messages[i].timestamp) );
if( messages[i].from == Green.me.id ) {
message = {'pic' : Green.me.image, 'name' : Green.me.name, 'sign' : 'me', 'message' : messages[i].data, 'date' : date};
}
else {
message = {'pic' : Green.rosters[messages[i].from].image, 'name' : Green.rosters[messages[i].from].name, 'sign' : 'you', 'message' : messages[i].data, 'date' : date};
}
sendStatus = '';
if( mode == 'send') {
sendStatus = '<div class="send-status"><img src="./image/send-loading.gif"><b>发送失败</b></div>';
}
renderStr += '<div class="cf chat-item '+message.sign+'">' +
'<div class="time">'+message.name+':'+message.date+' </div>' +
'<div class="chat-item-content cf">' +
'<img class="avatar" src="'+message.pic+'" title="'+message.name+'" alt="'+message.name+'">' +
'<div class="cloud cloud-text">' +
'<div class="cloud-pannel">' +
'<div class="cloud-body">' +
'<div class="cloud-content">'+
'<pre style="white-space:pre-wrap;*white-space:pre;*word-wrap:break-word;">'+message.message+'</pre>'+
'</div>' +
'</div>' +
'<div class="cloud-arrow"></div>' +
'</div>' +
'</div>' + sendStatus +
'</div>' +
'</div>';
}
var domMessage = $(renderStr)
if( mode == 'history' ) {
var nowPosition = this.getPosition();
this.dom.find('.loadmore').after(domMessage);
this.scroll();
this.adjustPosition(nowPosition);
}
if( mode == 'receive' || mode == 'send' ) {
this.dom.find('.messages').append(domMessage);
this.scroll();
this.scrollDown();
}
if( mode == 'notCurrent' ) {
this.dom.find('.messages').append(domMessage);
this.scroll();
}
return domMessage;
};
ConverseView.prototype.showLoadmore = function() {
this.dom.find('.loadmore').removeClass('loadmore-loading').show().find('b').html('更多历史消息');
};
ConverseView.prototype.showHistoryFailed = function() {
this.dom.find('.loadmore').removeClass('loadmore-loading').show().find('b').html('加载历史信息失败 点击重试');
};
ConverseView.prototype.showNoHistory = function() {
this.dom.find('.loadmore').hide();
this.dom.find('.no-more-history').show();
}
ConverseView.prototype.scrollDown = function() {
// this.dom.nanoScroller({ scroll: 'bottom' });
// 貌似视觉效果好些
var domMessage = this.dom.find('.messages');
domMessage.stop().animate( {scrollTop : domMessage[0].scrollHeight} );
};
ConverseView.prototype.adjustPosition = function(pre) {
this.dom.nanoScroller({ scrollTop: this.getPosition()-pre });
};
ConverseView.prototype.getPosition = function() {
return this.dom.find('.messages')[0].scrollHeight;
};
ConverseView.prototype.scroll = function() {
this.dom.nanoScroller({ preventPageScrolling: true, alwaysVisible: true });
};
ConverseView.prototype.destroy = function() {
this.dom.nanoScroller({ destroy: true });
this.dom.remove();
delete this;
};
return ConverseView;
}(window, document, Green, Green._Utils, Green._EventHandler);
// 会话窗口列表管理组件
Green._ConverseListView = function(window, document, Green, Utils, EventHandler, ConverseView) {
var ConverseListView = {};
ConverseListView.commit = function() {
var render = '<div class="convers"></div>';
this.dom = $(render);
this.converseListViews = {};
return this.dom;
};
// 添加一个会话窗口
ConverseListView.add = function(roster) {
var view = this.get(roster.id);
if( view ) return view;
var converseView = new ConverseView( roster );
this.converseListViews[roster.id] = converseView;
this.dom.append( converseView.commit() );
this.currentView = converseView;
return converseView;
};
ConverseListView.get = function(id) {
return this.converseListViews[id];
};
ConverseListView.front = function(id) {
this.get(id).dom.css('zIndex', 100).siblings().css('zIndex', 99);
};
ConverseListView.show = function() {
this.dom.show();
};
ConverseListView.hide = function() {
this.dom.hide();
};
ConverseListView.remove = function(id) {
this.get(id).destroy();
delete this.converseListViews[id];
};
ConverseListView.removeAll = function() {
$.each(this.converseListViews, function(k, v) {
v.destroy();
});
this.converseListViews = {};
this.currentView = null;
};
return ConverseListView;
}(window, document, Green, Green._Utils, Green._EventHandler, Green._ConverseView);
// 会话联系人组件
Green._ChatWithView = function(window, document, Green, Utils, EventHandler) {
function ChatWithView(roster) {
this.roster = roster;
}
ChatWithView.prototype.commit = function render() {
var render = '<div class="chat-with cf" peer="'+this.roster.id+'">' +
'<div class="list-head-status"><span class="chat-status '+this.roster.onlineStatus+'"></span></div>' +
'<div class="chat-with-name">'+this.roster.name+'</div>' +
'<a href="javascript:void(0);" class="chat-close"><i class="icon-remove"></i></a>' +
'<a href="javascript:void(0);" class="msg-unread"><i class="icon-volume-down"></i></a>' +
'</div>';
this.dom = $(render);
_init.call(this);
return this.dom;
};
function _init() {
this.dom.click($.proxy(function() {
EventHandler.trigger('chatWithClick', [this]);
}, this));
this.dom.find('.icon-remove').click($.proxy(function() {
EventHandler.trigger('chatWithRemove', [this]);
}, this));
};
ChatWithView.prototype.updateUnread = function(num) {
var unreadDom = this.dom.find(".msg-unread");
this.roster.unreadCount = num;
if(num == 0) {
unreadDom.hide();
}
else {
unreadDom.show();
}
};
ChatWithView.prototype.updateOnlineStatus = function(status) {
this.roster.onlineStatus = status;
this.dom.find(".chat-status").removeClass().addClass( 'chat-status ' + status);
};
ChatWithView.prototype.active = function() {
this.dom.addClass('active').siblings().removeClass('active');
};
ChatWithView.prototype.destroy = function() {
this.dom.remove();
delete this;
};
return ChatWithView;
}(window, document, Green, Green._Utils, Green._EventHandler);
// 会话联系人列表组件
Green._ChatWithListView = function(window, document, Green, Utils, ChatWithView, EventHandler) {
var ChatWithListView = {};
ChatWithListView.commit = function() {
var render = '<div class="pannel">'+
'<div class="c-tit chatusr-tit"><i class="icon-comments"></i>洽谈列表</div>'+
'<div class="usr-list"><div class="nano"><div class="nano-content"></div></div></div>'+
'</div>';
this.dom = $(render);
this.chatWithViews = [];
return this.dom;
};
ChatWithListView.add = function(roster) {
var view = this.get(roster.id);
if( view ) return view;
var chatWithView = new ChatWithView( roster );
this.chatWithViews.push(chatWithView);
this.dom.find('.nano-content').append( chatWithView.commit() );
this.dom.find('.nano').nanoScroller();
return chatWithView;
};
ChatWithListView.get = function(id) {
var views = this.chatWithViews;
for( var i=0, len=views.length; i<len; i++ ) {
if( views[i].roster.id == id ) return views[i];
}
return false;
};
ChatWithListView.prepend = function(id) {
this.get(id).dom.prependTo( this.dom.find('.nano-content') );
};
ChatWithListView.show = function() {
this.dom.show();
};
ChatWithListView.hide = function() {
this.dom.hide();
};
ChatWithListView.remove = function(chatWithView) {
var index = $.inArray(chatWithView, this.chatWithViews);
if( chatWithView.dom.hasClass('active') ) {
var next;
if( index == this.chatWithViews.length-1 ) {
next = this.chatWithViews[index-1];
}
else {
next = this.chatWithViews[index+1];
}
EventHandler.trigger('chatWithClick', [next]);
}
this.chatWithViews.splice(index, 1);
chatWithView.destroy();
this.dom.find('.nano').nanoScroller();
};
ChatWithListView.removeAll = function() {
$.each(this.chatWithViews, function(k, v) {
v.destroy();
});
this.chatWithViews = [];
};
return ChatWithListView;
}(window, document, Green, Green._Utils, Green._ChatWithView, Green._EventHandler);
// 聊天输入框组件
Green._InputView = function(window, document, Green, Utils, EventHandler) {
var InputView = {};
/**
* 渲染roster模版
*/
InputView.commit = function() {
var render = '<div class="send cf"><textarea class="text" placeholder="按回车发送"></textarea><a href="javascript:void(0);" class="btn">发送</a></div>';
this.dom = $(render);
_init.call(this);
return this.dom;
};
function _init() {
this.dom.find('.btn').click(function() {
_send.call(InputView);
return false;
});
this.dom.find('.text').keydown(function(e) {
if(e.which == 13 || e.which == 10) {
_send.call(InputView);
return false;
}
// else if (e.shiftKey && e.which==13 || e.which == 10) {
// EventHandler.trigger('sendMessageEnter');
// }
});
}
InputView.init = function() {
Utils.freeTextarea(this.dom.find('.text'));
}
InputView.reset = function() {
this.dom.find('.text').eq(0).val('').focus();
}
function _send() {
var domInput = this.dom.find('.text'),
text = domInput.val();
if( text == '' ) {
domInput.eq(0).focus();
return false;
}
EventHandler.trigger('sendMessageEnter', [text]);
}
return InputView;
}(window, document, Green, Green._Utils, Green._EventHandler);
Green._ChatboxView = function(window, document, Green, Utils, EventHandler, ChatWithListView, ConverseListView, InputView) {
var ChatboxView = {};
ChatboxView.commit = function() {
var render = '<div class="chatbox-wrap">'+
'<div class="chatbox cf"><div class="c-tit cf"><div class="chat-with"><img src="" /><b class="chat-name"></b></div><div class="chat-opt"><i class="icon-minus"></i> <i class="icon-remove"></i> </div></div></div>'+
'<div class="minus-pannel"><i class="icon-comments"></i><b class="cur-chat-name"></b></div>'+
'</div>';
this.dom = $(render);
this.dom.prepend( ChatWithListView.commit() );
this.dom.find('.chatbox').append( ConverseListView.commit() );
this.dom.find('.chatbox').append( InputView.commit() );
_init.call(this);
return this.dom;
};
function _init() {
this.dom.find('.icon-minus').click(function() {
ChatboxView.minifiy();
return false;
});
this.dom.find('.minus-pannel').click(function() {
EventHandler.trigger('chatboxMaxfiy');
return false;
});
this.dom.find('.icon-remove').click(function() {
ChatboxView.remove();
return false;
});
};
ChatboxView.get = function(id) {
return this.rosterViews[id];
};
ChatboxView.toggle = function(status) {
if( status ) {
this.dom.addClass('chatbox-wrap-contacts-minus');
}
else {
this.dom.removeClass('chatbox-wrap-contacts-minus');
}
};
ChatboxView.isMinus = function() {
return this.dom.hasClass('chatbox-minus');
};
ChatboxView.maxfiy = function() {
this.dom.removeClass('chatbox-minus');
if( this.dom.hasClass('chat-new-message') ) {
this.dom.removeClass('chat-new-message');
}
};
ChatboxView.minifiy = function() {
return this.dom.addClass('chatbox-minus');
};
ChatboxView.beMulti = function() {
this.dom.addClass('multi-chatbox');
};
ChatboxView.cleanMulti = function() {
this.dom.removeClass('multi-chatbox');
};
ChatboxView.hide = function() {
return this.dom.hide();
};
ChatboxView.show = function() {
return this.dom.show();
};
ChatboxView.remove = function() {
ChatWithListView.removeAll();
ConverseListView.removeAll();
this.hide();
};
return ChatboxView;
}(window, document, Green, Green._Utils, Green._EventHandler, Green._ChatWithListView, Green._ConverseListView, Green._InputView);
// 聊天服务器组件
Green._Server = function(window, document, Green, Utils, EventHandler) {
var Server = {};
var reconnectTimes = 0;
var reconnectLimit = 0;
var appId = '0s66p2ntvx0q56pk5mh8wxa7cuegr57w5abvb7nwv5dqymyo';
var handler;
Server.isOpen = false;
Server.init = function() {
handler = new AVChatClient({
appId: appId,
peerId: Green.me.id.toString(),
// auth: _auth,
// groupAuth: groupAuth,
watchingPeerIds: Green.watch
});
handler.on('close', function(event) {
console.log(event);
_onClose(event);
});
handler.on('message', function(data) {
_onMessage(data);
});
handler.on('online', function(rosters) {
_onOnline(rosters);
});
handler.on('offline', function(rosters) {
_onOffline(rosters);
});
_connect();
}
function _connect() {
if( !Server.isOpen && reconnectTimes==0 ) EventHandler.trigger('onConnectStart');
handler.open().then(function() {
Server.isOpen = true;
reconnectTimes = 0;
EventHandler.trigger('onConnectSuccess');
}, function(err) {
EventHandler.trigger('onConnectError');
});
}
function _onMessage(data) {
// console.log(data);//---------------------
var message = {'from' : data.fromPeerId, 'to' : data.peerId, 'data' : data.msg, 'timestamp' : data.timestamp};
if( data.toPeerIds ) {
message.to = data.toPeerIds[0];
}
EventHandler.trigger('messageReceived', [message]);
}
function _onClose(event) {
Server.isOpen = false;
if(reconnectTimes < reconnectLimit) {
reconnectTimes++;
EventHandler.trigger('onReconnect', [reconnectTimes]);
Server.init();
}
else {
EventHandler.trigger('onConnectClose');
}
}
function _onOnline(ids) {
for(var i=0, len=ids.length; i<len; i++) {
if( ids[i] == Green.me.id ) continue;
EventHandler.trigger('online', [ ids[i] ]);
}
}
function _onOffline(ids) {
for(var i=0, len=ids.length; i<len; i++) {
if( ids[i] == Green.me.id ) continue;
EventHandler.trigger('offline', [ ids[i] ]);
}
}
function _auth(rosterId, watchs, sp) {
return new Promise(function(resolve, reject) {
$.post('signature.php', {
self_id: rosterId,
watch_ids: watchs.join(':')
}).success(function(data) {
data = $.parseJSON(data);
resolve({
n: data.nonce,
t: data.timestamp,
s: data.signature,
sp: data.sp,
watchingPeerIds: data.watchIds
});
}).error(function(err) {
reject(err);
});
});
}
Server.getHistory = function(param, converse) {
//请求聊天列表
$.ajax({
type : "POST",
url : "conversation.php",
data : param,
success : function(msg) {
try {
var data = $.parseJSON(msg);
var messages = [];
for(var i=0, len=data.length; i<len; i++) {
messages.push({'from' : data[i].from, 'to' : data[i].to[0], 'data' : data[i].data, 'timestamp' : data[i].timestamp});
}
} catch(e) {
var messages = false;
}
EventHandler.trigger('getHistorySuccess', [param, converse, messages]);
},
error : function() {
EventHandler.trigger('getHistoryFailed', [param, converse]);
}
});