Authored by 李奇

代码优化

... ... @@ -12,7 +12,10 @@ var $ = require('yoho-jquery'),
emojiMap = require('./emoji-map'),
editArea = require('./edit-area'),
broswer = require('./broswer'),
cursor = require('./cursor'),
send = require('./socket/send'),
edit = require('./socket/edit'),
util = require('./utility'),
socketChat = require('./socket/chat'),
socketConf = require('./socket/config'),
clipPaste = require('./clip-paste');
... ... @@ -24,7 +27,6 @@ var allRTs,
socketConfCM,
assetsPrefix,
cursorPosition,
configDomains,
hasMore = true,
titleInterval,
$html = $('html'),
... ... @@ -59,13 +61,12 @@ var processInfo = {
savedEval: false // 是否保存过评论
};
var key,
urls = {
makeEval: '/evalute/saveEvalute',
leaveMsg: '/leavemessage/saveLeavemessage',
msgHistory: '/conversationMessage/pageList',
evalReason: '/evalute/queryReasonByConversationId'
};
var urls = {
makeEval: '/evalute/saveEvalute',
leaveMsg: '/leavemessage/saveLeavemessage',
msgHistory: '/conversationMessage/pageList',
evalReason: '/evalute/queryReasonByConversationId'
};
require('bootstrap');
require('../common');
... ... @@ -79,11 +80,18 @@ encryptedUid = $encryptedUid.val();
assetsPrefix = $assetsPrefix.val();
socketConfCM = socketConf.conversationMessage;
socketConfCM.encryptedUid = encryptedUid;
// 原始配置信息用于重新连线
originConf = JSON.parse(JSON.stringify(socketConf));
originConf = JSON.parse(JSON.stringify(socketConf)); // 原始配置
// url前缀添加
for (var key in urls) {
if (urls.hasOwnProperty(key)) {
urls[key] = gDomains.imCs + urls[key];
}
}
// 页面初始化
function pageInit() {
var _loadPage = function() {
/**
* 添加新的消息
*/
... ... @@ -93,48 +101,6 @@ function pageInit() {
}
/**
* 表情设置
* @param e
*/
function setEmoji(e) {
var i,
pos,
start,
end,
newVal,
textDom,
emojiText,
tag = $(e.target),
area = $('.msg-area'),
val = area.val(),
len = emojiMap.length,
emojiId = tag.data('id'),
comp = $('.emoji-component');
for (i = 0; i < len; i++) {
if (emojiMap[i].file === emojiId.toString()) {
emojiText = emojiMap[i].text;
}
}
if (cursorPosition) {
start = val.substring(0, cursorPosition);
end = val.substring(cursorPosition);
newVal = [start, emojiText, end].join('');
} else {
newVal = [val, emojiText].join('');
}
comp.hide();
area.val(newVal);
cursorPosition += emojiText.length;
textDom = area[0];
pos = cursorPosition ? cursorPosition : newVal.length;
broswer.setCursor(textDom, pos);
}
/**
* 系统通知
* @param tip 具体消息
*/
... ... @@ -967,7 +933,7 @@ function pageInit() {
dataType: 'json',
acceptFileTypes: /(\.|\/)(bmp|gif|jpe?g|png)$/i,
maxFileSize: 5000000, // 5M
url: configDomains.imCs + '/fileManage/uploadFile',
url: gDomains.imCs + '/fileManage/uploadFile',
progressall: function(e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
... ... @@ -1124,7 +1090,7 @@ function pageInit() {
});
// 设置表情
$document.on('click', '.emoji-component .emoji', setEmoji);
$document.on('click', '.emoji-component .emoji', edit.setEmoji);
// 表情组件隐藏
$document.on('click', function(e) {
... ... @@ -1160,7 +1126,7 @@ function pageInit() {
// 失去焦点更新鼠标位置
$msgArea.on('blur', function() {
cursorPosition = $(this).getCursorPosition();
$(this).recordCursor();
});
// 消息图片放大显示
... ... @@ -1194,34 +1160,14 @@ function pageInit() {
});
}
// 拉取域名信息
(function() {
$.ajax({
type: 'GET',
url: '/service/domains',
success: function(domains) {
configDomains = domains;
// url前缀添加
for (key in urls) {
if (urls.hasOwnProperty(key)) {
urls[key] = configDomains.imCs + urls[key];
}
}
// 检查支持性
(function() {
if (!window.WebSocket) {
$('.un-support').show();
return false;
} else {
socketConf.servers.push(configDomains.imSocket);
pageInit();
}
}());
}
});
}());
// 检查支持性
if (util.isSupport()) {
socketConf.servers.push(gDomains.imSocket);
_loadPage();
} else {
$('.un-support').show();
return false;
}
// 点击刷新提示
window.onbeforeunload = function() {
... ...
/**
* 光标处理
* @author: liqi <qi.li@yoho.cn>
* @date: 2017/02/15
*/
var cursor = {
posRecord: 0
};
/**
* $.fn.extend 获取输入框光标位置
* expample $(selector).recordCursor()
*/
$.fn.extend({
recordCursor: function() {
var pos = 0;
var $el = $(this).get(0);
if ('selectionStart' in $el) {
pos = $el.selectionStart;
} else if ('selection' in document) {
$el.focus();
var $sel = document.selection.createRange();
var $selLength = document.selection.createRange().text.length;
$sel.moveStart('character', -$el.value.length);
pos = $sel.text.length - $selLength;
}
cursor.posRecord = pos;
}
})
$.extend(cursor, {
/**
* 设置光标位置
* @param $el
* @param pos
*/
setCursor: function($el, pos) {
if ($el.setSelectionRange) {
// chrome and firefox support
$el.setSelectionRange(pos, pos);
$el.focus();
} else if ($el.createTextRange) {
// IE support
var range = $el.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
},
/**
* 在已记录光标处插入文本
* @param text 文本
*/
insertAtRecordCorsor: function(text) {
var self = cursor;
var $area = $('.msg-area');
var val = $area.val();
var record = self.posRecord;
if (record || record === 0) {
var start = val.substring(0, record);
var end = val.substring(record);
var newVal = [start, text, end].join('');
} else {
var newVal = [val, text].join('');
}
$area.val(newVal);
self.posRecord += text.length;
var $el = $area[0];
var pos = self.posRecord ? self.posRecord : newVal.length;
this.setCursor($el, pos);
}
})
module.exports = cursor;
... ...
var $ = require('yoho-jquery');
var cursor = require('../cursor');
var broswer = require('../broswer');
var emMap = require('../emoji-map');
/**
* 表情设置
* @param e
*/
exports.setEmoji = function(e) {
var emText;
var tag = $(e.target);
var emId = tag.data('id');
var $em = $('.emoji-component');
$.each(emMap, function(i, v) {
if(v.file === emId + "") {
emText = v.text;
}
});
cursor.insertAtRecordCorsor(emText);
$em.hide();
}
... ...
/**
* 检查是否支持websoket
*/
function isSupport() {
return !!window.WebSocket;
}
module.exports = {
isSupport: isSupport
}
\ No newline at end of file
... ...