currentmessage.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. (function() {
  2. 'use strict';
  3. angular
  4. .module('appMessage')
  5. .directive('ngCurrentMsg', CurrentMsg);
  6. CurrentMsg.inject = ['$location', '$anchorScroll', '$timeout'];
  7. function CurrentMsg($location, $anchorScroll, $timeout) {
  8. var directive = {
  9. link: link,
  10. restrict: 'A',
  11. scope: {
  12. msg: '=ngCurrentMsg',
  13. appContent: '=ngAppContent'
  14. }
  15. };
  16. return directive;
  17. function link(scope, element, attrs) {
  18. var gotoBottom = function() {
  19. $location.hash("bottom");
  20. $anchorScroll();
  21. };
  22. var extend = function(dest, source) {
  23. for (var key in source) {
  24. if (source.hasOwnProperty(key)) {
  25. dest[key] = source[key];
  26. }
  27. }
  28. return dest;
  29. };
  30. var toColor = function(num) {
  31. var color = num.toString(16);
  32. while (color.length < 6) {
  33. color = '0' + color;
  34. }
  35. return color;
  36. }
  37. var replaceSystemFace = function(content) {
  38. //匹配表情
  39. var g = (/\/\:([\w]+)/).exec(content);
  40. while (g !== null) {
  41. scope.appContent.faceConfig.sys.forEach(function(element) {
  42. content = content.replace(element.shortcut, '<img style="height:56px" src="local://' + scope.appContent.directory.base + element.image + '">');
  43. }, this);
  44. g = (/\/\:([\w]+)/).exec(content);
  45. }
  46. //匹配截图
  47. var g = (/\[image\-src=\"([\w\W]+?)\"\]/i).exec(content);
  48. while (g !== null) {
  49. content = content.replace(g[0], function() {
  50. var html = '<a href="#" class="thumbnail"' + '>' +
  51. '<img style="max-width:270px;max-height:250px;" src="local://' + scope.appContent.directory.received + g[1] + '.JPG' + '" alt="" >' +
  52. '</a>';
  53. loadImg("local://" + scope.appContent.directory.received + g[1] + ".JPG", addImg);
  54. function loadImg(url, callback) {
  55. var img = new Image();
  56. img.onload = function() {
  57. img.onload = null;
  58. callback(img);
  59. }
  60. img.src = url;
  61. //img.attr("defaulturl", "../images/img.png");
  62. }
  63. function addImg(img) {
  64. //$(img).appendTo($(".image"));
  65. gotoBottom();
  66. }
  67. return html;
  68. });
  69. g = (/\[image\-src=\"([\w\W]+?)\"\]/i).exec(content);
  70. }
  71. //匹配网址
  72. var i = 0;
  73. var m = content.match(/(((((http|ftp|https|HTTP|FTP|HTTPS):\/\/)[\w\-_]+)|www|WWW|[0-9]+)(\.(?!gif|jpg|png|mp3)[\w\-_]+)+([\w\-\.,@?^=%:/~\+#]*[\w\-\@?^=%/~\+#\r\n]))/ig);
  74. console.log(m);
  75. if (m == null)
  76. return content;
  77. content = content.replace(/(((((http|ftp|https|HTTP|FTP|HTTPS):\/\/)[\w\-_]+)|www|WWW|[0-9]+)(\.(?!gif|jpg|png|mp3)[\w\-_]+)+([\w\-\.,@?^=%:/~\+#]*[\w\-\@?^=%/~\+#\r\n]))/ig, function() {
  78. var html = '';
  79. var g = (/((((http|ftp|https|HTTP|FTP|HTTPS):\/\/)[\w\-_]+)(\.[\w\-_]+)+([\w\-\.,@?^=%:/~\+#]*[\w\-\@?^=%/~\+#\r\n]))/ig).exec(m[i]);
  80. if (g == null)
  81. html = '<a target="_blank" href="http://' + m[i] + '">' +
  82. m[i] +
  83. '</a>';
  84. else
  85. html = '<a target="_blank" href="' + m[i] + '">' +
  86. m[i] +
  87. '</a>';
  88. i = i + 1;
  89. return html;
  90. });
  91. return content;
  92. };
  93. var replaceImage = function(content) {}
  94. var html = '';
  95. var message = document.getElementById("message");
  96. // 消息类型([1,文本消息],[2,图片消息],[3,文件消息], [4,位置消息],[5,语音消息])
  97. if (scope.appContent === undefined || scope.appContent.faceConfig === undefined || scope.appContent.faceConfig.sys === undefined) {
  98. html = '<span class="text-message">' + scope.msg.content + '</span>';
  99. } else if (scope.msg.contentType == 1) {
  100. //var style = { style: "normal", weight: "normal" };
  101. var style = JSON.parse(scope.msg.style);
  102. style = extend({
  103. style: 'normal',
  104. weight: 'normal',
  105. size: 9,
  106. family: '微软雅黑',
  107. color: 0
  108. }, style);
  109. html = '<span class="text-message"' + ' style="font:' + style.style + ' ' + style.weight + ' ' + style.size + 'pt' + ' ' + style.family + ';color:#' + toColor(style.color) + '">' +
  110. replaceSystemFace(scope.msg.content) + '</span>';
  111. } else if (scope.msg.contentType == 2) {
  112. html = '<span class="text-message">' +
  113. '<a href="#" class="thumbnail" >' +
  114. '<img src="' + scope.msg.content + '" alt="通用的占位符缩略图" >' +
  115. '</a>' +
  116. '</span>';
  117. } else if (scope.msg.contentType == 3) {
  118. element.css({
  119. width: '275px'
  120. });
  121. //element.addClass('file-message');
  122. var reg = /\?[\W\w]*/;
  123. var fileName = decodeURIComponent(reg.exec(scope.msg.content).toString().substr(1));
  124. html = '<div class="file-message">' +
  125. '<img src="../img/unkown.png" onError= "this.src = \'../img/file/unkown.png\'">' +
  126. '<span style="float:left; margin:5px 0px 0px 0px">文件名:' + fileName + '<br/>大小:' + scope.msg.length + '</span>' +
  127. '<div class="p-t-sm text-right">' +
  128. '<a class="text-primary" role="button"><span>打开</span></a>&nbsp&nbsp' +
  129. '<a class="text-primary" role="button"><span>打开文件夹</span></a>' +
  130. '</div>' +
  131. '</div>';
  132. } else if (scope.msg.contentType == 4) {
  133. html = '<span class="text-message">' +
  134. '<iframe class="map" border=0 src="' + scope.msg.content + '" frameBorder=no></iframe>' +
  135. '</span>';
  136. } else if (scope.msg.contentType == 5) {
  137. html = '<span class="text-message">' +
  138. '<audio src="' + scope.msg.content + '" controls="controls">' +
  139. '</audio>' +
  140. '</span>';
  141. }
  142. console.log(html);
  143. var newElement = angular.element(html);
  144. element.append(newElement);
  145. // $(function() {
  146. // $("img.lazy").lazyload();
  147. // });
  148. message.addEventListener('load', gotoBottom());
  149. }
  150. }
  151. })();