PageRenderTime 25ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/res/js/dev/pages/common/insertEmotions.js

https://gitlab.com/wuhang2003/phpwind
JavaScript | 235 lines | 155 code | 49 blank | 31 comment | 22 complexity | b706bddcd813471b696113addb575b7d MD5 | raw file
  1. /**
  2. * PHPWind PAGE JS
  3. * @Copyright Copyright 2011, phpwind.com
  4. * @Descript: 前台-表情插入
  5. * @Author : linhao87@gmail.com
  6. * @Depend : core.js、jquery.js(1.7 or later), global.js
  7. * $Id$
  8. */
  9. /**
  10. * PHPWind util Library
  11. * @Copyright 愚人码头
  12. * @Descript: 光标位置插入代码
  13. * @Author : 愚人码头 http://www.css88.com/archives/3627
  14. * $Id: jquery.dateSelect.js 3985 2012-02-06 07:38:25Z hao.lin $
  15. */
  16. ;(function ( $, window, document, undefined ) {
  17. $.fn.extend({
  18. rangeInsert: function(myValue,t){
  19. var $t=$(this)[0];
  20. if (document.selection) {//ie
  21. this.focus();
  22. var sel = document.selection.createRange();
  23. sel.text = myValue;
  24. this.focus();
  25. sel.moveStart ('character', -l);
  26. var wee = sel.text.length;
  27. if(arguments.length == 2){
  28. var l = $t.value.length;
  29. sel.moveEnd("character", wee+t );
  30. t<=0?sel.moveStart("character",wee-2*t-myValue.length):sel.moveStart("character",wee-t-myValue.length);
  31. sel.select();
  32. }
  33. } else if ($t.selectionStart || $t.selectionStart == '0') {
  34. var startPos = $t.selectionStart;
  35. var endPos = $t.selectionEnd;
  36. var scrollTop = $t.scrollTop;
  37. $t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
  38. this.focus();
  39. $t.selectionStart = startPos + myValue.length;
  40. $t.selectionEnd = startPos + myValue.length;
  41. $t.scrollTop = scrollTop;
  42. if(arguments.length == 2){
  43. $t.setSelectionRange(startPos-t,$t.selectionEnd+t);
  44. this.focus();
  45. }
  46. }
  47. else {
  48. this.value += myValue;
  49. this.focus();
  50. }
  51. }
  52. });
  53. })(jQuery, window ,document);
  54. var emotions_temp = '<div id="J_emotions_pop" style="z-index:11;position:absolute;" class="core_menu pop_show_mini">\
  55. <div class="core_arrow_top" style="left:0;"><em></em><span></span></div><a href="#" id="J_emotions_close" class="pop_close">关闭</a>\
  56. <div id="J_emotions_menu"></div>\
  57. <div class="ct" id="J_emotions_pl"><div class="pop_loading"></div></div>\
  58. </div>',
  59. page_size = 30, //单页表情数
  60. emo_data;
  61. function insertEmotions(elem, input, wrap) {
  62. var emotions_pop = $('#J_emotions_pop');
  63. if(emotions_pop.length) {
  64. if(wrap) {
  65. //移入容器里,定位由页面写
  66. emotions_pop.appendTo(wrap);
  67. }else{
  68. //移入body里
  69. emotions_pop.appendTo('body');
  70. }
  71. emotionsPos(elem, emotions_pop, wrap);
  72. }else{
  73. if(wrap) {
  74. wrap.append(emotions_temp);
  75. }else{
  76. $('body').append(emotions_temp);
  77. }
  78. var emotions_pop = $('#J_emotions_pop'),
  79. emotions_menu = $('#J_emotions_menu'),
  80. emotions_pl = $('#J_emotions_pl');
  81. //定位
  82. emotionsPos(elem, emotions_pop, wrap);
  83. $.getJSON(GV.URL.EMOTIONS, function(data){
  84. try{
  85. if(data.state == 'success') {
  86. var nav_arr = [],
  87. index = 0;
  88. emo_data = data.data;
  89. emotions_pl.html('');
  90. //循环读取菜单和表情
  91. $.each(data.data,function(i, o){
  92. index++;
  93. nav_arr.push('<li class="'+ (index === 1 ? 'current' : '') +'"><a href="">'+ o.category +'</a></li>');
  94. var emotion_arr = [],
  95. page_count = Math.ceil(o['emotion'].length/page_size);
  96. $.each(o.emotion, function(i, o){
  97. emotion_arr.push('<li><a href="#" class="J_emotions_item" data-sign="'+ o.sign +'"><img '+ (index === 1 ? 'src=\"'+o.url+'\"' : 'data-src=\"'+o.url+'\"') +'></a></li>');
  98. });
  99. //翻页写入
  100. if(page_count > 1) {
  101. emotions_pl.append('<div style="'+ (index === 1 ? '' : 'display:none') +'"><ul class="cc">'+ emotionsShowPage( 1, i) +'</ul></div>');
  102. var page = [];
  103. for(var j = 1; j <= page_count; j++) {
  104. page.push('<a href="javascript:;" class="'+ ( j===1 ? 'current':'' ) +' J_emotions_page" data-index="'+ i +'">'+ j +'</a>');
  105. }
  106. emotions_pl.children('div').eq(i).append('<div class="show_page J_emo_page">'+ page.join('') +'</div>');
  107. }else{
  108. //表情写入
  109. emotions_pl.append('<div style="'+ (index === 1 ? '' : 'display:none') +'"><ul class="cc">'+ emotion_arr.join('') +'</ul></div>');
  110. }
  111. });
  112. //点击页码
  113. $('.J_emo_page').on('click', 'a.J_emotions_page', function(e){
  114. e.preventDefault();
  115. var $this = $(this);
  116. $this.parent().prev('ul').html(emotionsShowPage( parseInt(this.innerHTML), $this.data('index')));
  117. $this.addClass('current').siblings().removeClass('current');
  118. });
  119. //菜单写入
  120. emotions_menu.prepend('<div class="hd"><ul class="cc">'+ nav_arr.join('') +'</ul></div>');
  121. //点击菜单
  122. emotions_menu.on('click', 'a', function(e){
  123. e.preventDefault();
  124. var container = emotions_pl.children().eq($(this).parent().index());
  125. $(this).parent().addClass('current').siblings().removeClass('current');
  126. container.show().siblings().hide();
  127. emotionsShowImg(container);
  128. });
  129. //关闭
  130. $('#J_emotions_close').on('click', function(e){
  131. e.preventDefault();
  132. emotions_pop.hide();
  133. });
  134. }else if(data.state == 'fail'){
  135. Wind.Util.resultTips({
  136. error : true,
  137. msg : data.message
  138. });
  139. }
  140. }catch(e) {
  141. $.error(e);
  142. }
  143. });
  144. }
  145. //点击表情
  146. $('#J_emotions_pl').off('click').on('click', 'a.J_emotions_item', function(e){
  147. e.preventDefault();
  148. //jquery.insertContent ie7 data('sign') 有时获取不到
  149. input.rangeInsert(this.getAttribute('data-sign'));
  150. $('#J_emotions_pop').hide();
  151. });
  152. }
  153. //显示当前页
  154. function emotionsShowPage(index, i){
  155. var data = emo_data[i]['emotion'];
  156. var len = (index*page_size > data.length ? data.length : index*page_size),
  157. arr = [];
  158. for(var i = (index-1)*page_size; i <= len - 1; i++) {
  159. arr.push('<li><a href="#" class="J_emotions_item" data-sign="'+ data[i].sign +'"><img src="'+data[i].url+'"></a></li>');
  160. }
  161. return arr.join('');
  162. }
  163. //图片src写入
  164. function emotionsShowImg(wrap){
  165. var imgs = wrap.find('img');
  166. if(imgs.data('src')) {
  167. imgs.attr('src', function () {
  168. return $(this).data('src');
  169. }).data('src', '');
  170. }
  171. }
  172. //表情弹窗定位
  173. function emotionsPos(elem, pop, wrap){
  174. if(wrap) {
  175. //容器内计算边距
  176. pop.css({
  177. left : elem.offset().left - wrap.offset().left - 30,
  178. top : elem.offset().top - wrap.offset().top + elem.height() + 5
  179. }).show();;
  180. }else{
  181. pop.css({
  182. left : elem.offset().left - 25,
  183. top : elem.offset().top + elem.outerHeight() + 15
  184. }).show();;
  185. }
  186. }