Authored by 李奇

代码优化

@@ -12,7 +12,10 @@ var $ = require('yoho-jquery'), @@ -12,7 +12,10 @@ var $ = require('yoho-jquery'),
12 emojiMap = require('./emoji-map'), 12 emojiMap = require('./emoji-map'),
13 editArea = require('./edit-area'), 13 editArea = require('./edit-area'),
14 broswer = require('./broswer'), 14 broswer = require('./broswer'),
  15 + cursor = require('./cursor'),
15 send = require('./socket/send'), 16 send = require('./socket/send'),
  17 + edit = require('./socket/edit'),
  18 + util = require('./utility'),
16 socketChat = require('./socket/chat'), 19 socketChat = require('./socket/chat'),
17 socketConf = require('./socket/config'), 20 socketConf = require('./socket/config'),
18 clipPaste = require('./clip-paste'); 21 clipPaste = require('./clip-paste');
@@ -24,7 +27,6 @@ var allRTs, @@ -24,7 +27,6 @@ var allRTs,
24 socketConfCM, 27 socketConfCM,
25 assetsPrefix, 28 assetsPrefix,
26 cursorPosition, 29 cursorPosition,
27 - configDomains,  
28 hasMore = true, 30 hasMore = true,
29 titleInterval, 31 titleInterval,
30 $html = $('html'), 32 $html = $('html'),
@@ -59,13 +61,12 @@ var processInfo = { @@ -59,13 +61,12 @@ var processInfo = {
59 savedEval: false // 是否保存过评论 61 savedEval: false // 是否保存过评论
60 }; 62 };
61 63
62 -var key,  
63 - urls = {  
64 - makeEval: '/evalute/saveEvalute',  
65 - leaveMsg: '/leavemessage/saveLeavemessage',  
66 - msgHistory: '/conversationMessage/pageList',  
67 - evalReason: '/evalute/queryReasonByConversationId'  
68 - }; 64 +var urls = {
  65 + makeEval: '/evalute/saveEvalute',
  66 + leaveMsg: '/leavemessage/saveLeavemessage',
  67 + msgHistory: '/conversationMessage/pageList',
  68 + evalReason: '/evalute/queryReasonByConversationId'
  69 +};
69 70
70 require('bootstrap'); 71 require('bootstrap');
71 require('../common'); 72 require('../common');
@@ -79,11 +80,18 @@ encryptedUid = $encryptedUid.val(); @@ -79,11 +80,18 @@ encryptedUid = $encryptedUid.val();
79 assetsPrefix = $assetsPrefix.val(); 80 assetsPrefix = $assetsPrefix.val();
80 socketConfCM = socketConf.conversationMessage; 81 socketConfCM = socketConf.conversationMessage;
81 socketConfCM.encryptedUid = encryptedUid; 82 socketConfCM.encryptedUid = encryptedUid;
82 -// 原始配置信息用于重新连线  
83 -originConf = JSON.parse(JSON.stringify(socketConf)); 83 +originConf = JSON.parse(JSON.stringify(socketConf)); // 原始配置
  84 +
  85 +
  86 +// url前缀添加
  87 +for (var key in urls) {
  88 + if (urls.hasOwnProperty(key)) {
  89 + urls[key] = gDomains.imCs + urls[key];
  90 + }
  91 +}
84 92
85 // 页面初始化 93 // 页面初始化
86 -function pageInit() { 94 +var _loadPage = function() {
87 /** 95 /**
88 * 添加新的消息 96 * 添加新的消息
89 */ 97 */
@@ -93,48 +101,6 @@ function pageInit() { @@ -93,48 +101,6 @@ function pageInit() {
93 } 101 }
94 102
95 /** 103 /**
96 - * 表情设置  
97 - * @param e  
98 - */  
99 - function setEmoji(e) {  
100 - var i,  
101 - pos,  
102 - start,  
103 - end,  
104 - newVal,  
105 - textDom,  
106 - emojiText,  
107 - tag = $(e.target),  
108 - area = $('.msg-area'),  
109 - val = area.val(),  
110 - len = emojiMap.length,  
111 - emojiId = tag.data('id'),  
112 - comp = $('.emoji-component');  
113 -  
114 - for (i = 0; i < len; i++) {  
115 - if (emojiMap[i].file === emojiId.toString()) {  
116 - emojiText = emojiMap[i].text;  
117 - }  
118 - }  
119 -  
120 - if (cursorPosition) {  
121 - start = val.substring(0, cursorPosition);  
122 - end = val.substring(cursorPosition);  
123 - newVal = [start, emojiText, end].join('');  
124 - } else {  
125 - newVal = [val, emojiText].join('');  
126 - }  
127 -  
128 - comp.hide();  
129 - area.val(newVal);  
130 - cursorPosition += emojiText.length;  
131 -  
132 - textDom = area[0];  
133 - pos = cursorPosition ? cursorPosition : newVal.length;  
134 - broswer.setCursor(textDom, pos);  
135 - }  
136 -  
137 - /**  
138 * 系统通知 104 * 系统通知
139 * @param tip 具体消息 105 * @param tip 具体消息
140 */ 106 */
@@ -967,7 +933,7 @@ function pageInit() { @@ -967,7 +933,7 @@ function pageInit() {
967 dataType: 'json', 933 dataType: 'json',
968 acceptFileTypes: /(\.|\/)(bmp|gif|jpe?g|png)$/i, 934 acceptFileTypes: /(\.|\/)(bmp|gif|jpe?g|png)$/i,
969 maxFileSize: 5000000, // 5M 935 maxFileSize: 5000000, // 5M
970 - url: configDomains.imCs + '/fileManage/uploadFile', 936 + url: gDomains.imCs + '/fileManage/uploadFile',
971 progressall: function(e, data) { 937 progressall: function(e, data) {
972 var progress = parseInt(data.loaded / data.total * 100, 10); 938 var progress = parseInt(data.loaded / data.total * 100, 10);
973 939
@@ -1124,7 +1090,7 @@ function pageInit() { @@ -1124,7 +1090,7 @@ function pageInit() {
1124 }); 1090 });
1125 1091
1126 // 设置表情 1092 // 设置表情
1127 - $document.on('click', '.emoji-component .emoji', setEmoji); 1093 + $document.on('click', '.emoji-component .emoji', edit.setEmoji);
1128 1094
1129 // 表情组件隐藏 1095 // 表情组件隐藏
1130 $document.on('click', function(e) { 1096 $document.on('click', function(e) {
@@ -1160,7 +1126,7 @@ function pageInit() { @@ -1160,7 +1126,7 @@ function pageInit() {
1160 1126
1161 // 失去焦点更新鼠标位置 1127 // 失去焦点更新鼠标位置
1162 $msgArea.on('blur', function() { 1128 $msgArea.on('blur', function() {
1163 - cursorPosition = $(this).getCursorPosition(); 1129 + $(this).recordCursor();
1164 }); 1130 });
1165 1131
1166 // 消息图片放大显示 1132 // 消息图片放大显示
@@ -1194,34 +1160,14 @@ function pageInit() { @@ -1194,34 +1160,14 @@ function pageInit() {
1194 }); 1160 });
1195 } 1161 }
1196 1162
1197 -// 拉取域名信息  
1198 -(function() {  
1199 - $.ajax({  
1200 - type: 'GET',  
1201 - url: '/service/domains',  
1202 - success: function(domains) {  
1203 - configDomains = domains;  
1204 -  
1205 - // url前缀添加  
1206 - for (key in urls) {  
1207 - if (urls.hasOwnProperty(key)) {  
1208 - urls[key] = configDomains.imCs + urls[key];  
1209 - }  
1210 - }  
1211 -  
1212 - // 检查支持性  
1213 - (function() {  
1214 - if (!window.WebSocket) {  
1215 - $('.un-support').show();  
1216 - return false;  
1217 - } else {  
1218 - socketConf.servers.push(configDomains.imSocket);  
1219 - pageInit();  
1220 - }  
1221 - }());  
1222 - }  
1223 - });  
1224 -}()); 1163 +// 检查支持性
  1164 +if (util.isSupport()) {
  1165 + socketConf.servers.push(gDomains.imSocket);
  1166 + _loadPage();
  1167 +} else {
  1168 + $('.un-support').show();
  1169 + return false;
  1170 +}
1225 1171
1226 // 点击刷新提示 1172 // 点击刷新提示
1227 window.onbeforeunload = function() { 1173 window.onbeforeunload = function() {
  1 +/**
  2 + * 光标处理
  3 + * @author: liqi <qi.li@yoho.cn>
  4 + * @date: 2017/02/15
  5 + */
  6 +
  7 +var cursor = {
  8 + posRecord: 0
  9 +};
  10 +
  11 +
  12 +/**
  13 + * $.fn.extend 获取输入框光标位置
  14 + * expample $(selector).recordCursor()
  15 + */
  16 +$.fn.extend({
  17 + recordCursor: function() {
  18 + var pos = 0;
  19 + var $el = $(this).get(0);
  20 +
  21 + if ('selectionStart' in $el) {
  22 + pos = $el.selectionStart;
  23 + } else if ('selection' in document) {
  24 + $el.focus();
  25 + var $sel = document.selection.createRange();
  26 + var $selLength = document.selection.createRange().text.length;
  27 +
  28 + $sel.moveStart('character', -$el.value.length);
  29 + pos = $sel.text.length - $selLength;
  30 + }
  31 + cursor.posRecord = pos;
  32 + }
  33 +})
  34 +
  35 +$.extend(cursor, {
  36 + /**
  37 + * 设置光标位置
  38 + * @param $el
  39 + * @param pos
  40 + */
  41 + setCursor: function($el, pos) {
  42 + if ($el.setSelectionRange) {
  43 + // chrome and firefox support
  44 + $el.setSelectionRange(pos, pos);
  45 + $el.focus();
  46 + } else if ($el.createTextRange) {
  47 + // IE support
  48 + var range = $el.createTextRange();
  49 + range.collapse(true);
  50 + range.moveEnd('character', pos);
  51 + range.moveStart('character', pos);
  52 + range.select();
  53 + }
  54 + },
  55 +
  56 + /**
  57 + * 在已记录光标处插入文本
  58 + * @param text 文本
  59 + */
  60 + insertAtRecordCorsor: function(text) {
  61 + var self = cursor;
  62 + var $area = $('.msg-area');
  63 + var val = $area.val();
  64 + var record = self.posRecord;
  65 +
  66 + if (record || record === 0) {
  67 + var start = val.substring(0, record);
  68 + var end = val.substring(record);
  69 + var newVal = [start, text, end].join('');
  70 + } else {
  71 + var newVal = [val, text].join('');
  72 + }
  73 +
  74 + $area.val(newVal);
  75 + self.posRecord += text.length;
  76 +
  77 + var $el = $area[0];
  78 + var pos = self.posRecord ? self.posRecord : newVal.length;
  79 + this.setCursor($el, pos);
  80 + }
  81 +})
  82 +
  83 +module.exports = cursor;
  84 +
  1 +var $ = require('yoho-jquery');
  2 +var cursor = require('../cursor');
  3 +var broswer = require('../broswer');
  4 +var emMap = require('../emoji-map');
  5 +
  6 +/**
  7 + * 表情设置
  8 + * @param e
  9 + */
  10 +exports.setEmoji = function(e) {
  11 + var emText;
  12 + var tag = $(e.target);
  13 + var emId = tag.data('id');
  14 + var $em = $('.emoji-component');
  15 +
  16 + $.each(emMap, function(i, v) {
  17 + if(v.file === emId + "") {
  18 + emText = v.text;
  19 + }
  20 + });
  21 + cursor.insertAtRecordCorsor(emText);
  22 + $em.hide();
  23 +}
  24 +
  1 +/**
  2 + * 检查是否支持websoket
  3 + */
  4 +function isSupport() {
  5 + return !!window.WebSocket;
  6 +}
  7 +
  8 +module.exports = {
  9 + isSupport: isSupport
  10 +}