jquery.sinaEmotion-1.3.js
4.67 KB
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
(function($){
var target, _emotions = null;
$.fn.sinaEmotion = function(options){
var defaults = {
target: $(this).prev(),
app_id: '1362404091'
};
options = $.extend({}, defaults, options);
var cat_current;
var cat_page;
var emotions = new Array();
var categorys = new Array();
$(this).click(function(event){
if(_emotions) _emotions.hide();
event.stopPropagation();
_emotions = $(this).parent().find('#emotions');
if(!_emotions[0]){
$(this).parent().append('<div id="emotions"></div>');
_emotions = $(this).parent().find('#emotions');
}
_emotions.css({top: $(this)[0].offsetTop + $(this).height() + 12, left: $(this)[0].offsetLeft});
//target = options.target;
target = $(this).prev();
if(_emotions.find('.categorys')[0]){
_emotions.toggle();
return;
}
_emotions.html('<div>正在加载,请稍候...</div>');
_emotions.click(function(event){
event.stopPropagation();
});
$.ajax({
dataType: 'jsonp',
url: 'https://api.weibo.com/2/emotions.json?source=' + options.app_id,
beforeSend: function(){},
error: function(request){
_emotions.html('<div>加载失败</div>');
},
success: function(response){
_emotions.html('<div style="float:right"><a href="javascript:void(0);" id="prev">«</a><a href="javascript:void(0);" id="next">»</a></div><div class="categorys"></div><div class="container"></div><div class="page"></div>');
var data = response.data;
for(var i in data){
if(data[i].category == ''){
data[i].category = '默认';
}
if(emotions[data[i].category] == undefined){
emotions[data[i].category] = new Array();
categorys.push(data[i].category);
}
emotions[data[i].category].push({name: data[i].phrase, icon: data[i].icon});
}
_emotions.find('#prev').click(function(){
showCategorys(cat_page - 1);
});
_emotions.find('#next').click(function(){
showCategorys(cat_page + 1);
});
showCategorys();
showEmotions();
}
});
});
$('body').click(function(){
if(_emotions) _emotions.hide();
});
$.fn.insertText = function(text){
this.each(function() {
if(this.tagName !== 'INPUT' && this.tagName !== 'TEXTAREA') {return;}
if (document.selection) {
this.focus();
var cr = document.selection.createRange();
cr.text = text;
cr.collapse();
cr.select();
}else if (this.selectionStart || this.selectionStart == '0') {
var
start = this.selectionStart,
end = this.selectionEnd;
this.value = this.value.substring(0, start)+ text+ this.value.substring(end, this.value.length);
this.selectionStart = this.selectionEnd = start+text.length;
}else {
this.value += text;
}
});
return this;
}
function showCategorys(){
var page = arguments[0]?arguments[0]:0;
if(page < 0 || page >= categorys.length / 5){
return;
}
_emotions.find('.categorys').html('');
cat_page = page;
for(var i = page * 5; i < (page + 1) * 5 && i < categorys.length; ++i){
_emotions.find('.categorys').append($('<a href="javascript:void(0);">' + categorys[i] + '</a>'));
}
_emotions.find('.categorys a').click(function(){
showEmotions($(this).text());
});
_emotions.find('.categorys a').each(function(){
if($(this).text() == cat_current){
$(this).addClass('current');
}
});
}
function showEmotions(){
var category = arguments[0]?arguments[0]:'默认';
var page = arguments[1]?arguments[1] - 1:0;
_emotions.find('.container').html('');
_emotions.find('.page').html('');
cat_current = category;
for(var i = page * 72; i < (page + 1) * 72 && i < emotions[category].length; ++i){
_emotions.find('.container').append($('<a href="javascript:void(0);" title="' + emotions[category][i].name + '"><img src="' + emotions[category][i].icon + '" alt="' + emotions[category][i].name + '" width="22" height="22" /></a>'));
}
_emotions.find('.container a').click(function(){
target.insertText($(this).attr('title'));
_emotions.hide();
});
for(var i = 1; i < emotions[category].length / 72 + 1; ++i){
_emotions.find('.page').append($('<a href="javascript:void(0);"' + (i == page + 1?' class="current"':'') + '>' + i + '</a>'));
}
_emotions.find('.page a').click(function(){
showEmotions(category, $(this).text());
});
_emotions.find(' .categorys a.current').removeClass('current');
_emotions.find('.container a').each(function(){
if($(this).text() == category){
$(this).addClass('current');
}
});
}
}
})(jQuery);