image.ctrl.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. angular.module('kityminderEditor')
  2. .controller('image.ctrl', ['$http', '$scope', '$modalInstance', 'image', 'server', function($http, $scope, $modalInstance, image, server) {
  3. $scope.data = {
  4. list: [],
  5. url: image.url || '',
  6. title: image.title || '',
  7. R_URL: /^https?\:\/\/\w+/
  8. };
  9. setTimeout(function() {
  10. var $imageUrl = $('#image-url');
  11. $imageUrl.focus();
  12. $imageUrl[0].setSelectionRange(0, $scope.data.url.length);
  13. }, 300);
  14. // 搜索图片按钮点击事件
  15. $scope.searchImage = function() {
  16. $scope.list = [];
  17. getImageData()
  18. .success(function(json) {
  19. if(json && json.data) {
  20. for(var i = 0; i < json.data.length; i++) {
  21. if(json.data[i].objURL) {
  22. $scope.list.push({
  23. title: json.data[i].fromPageTitleEnc,
  24. src: json.data[i].middleURL,
  25. url: json.data[i].middleURL
  26. });
  27. }
  28. }
  29. }
  30. })
  31. .error(function() {
  32. });
  33. };
  34. // 选择图片的鼠标点击事件
  35. $scope.selectImage = function($event) {
  36. var targetItem = $('#img-item'+ (this.$index));
  37. var targetImg = $('#img-'+ (this.$index));
  38. targetItem.siblings('.selected').removeClass('selected');
  39. targetItem.addClass('selected');
  40. $scope.data.url = targetImg.attr('src');
  41. $scope.data.title = targetImg.attr('alt');
  42. };
  43. // 自动上传图片,后端需要直接返回图片 URL
  44. $scope.uploadImage = function() {
  45. var fileInput = $('#upload-image');
  46. if (!fileInput.val()) {
  47. return;
  48. }
  49. if (/^.*\.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG)$/.test(fileInput.val())) {
  50. var file = fileInput[0].files[0];
  51. return server.uploadImage(file).then(function (json) {
  52. var resp = json.data;
  53. if (resp.errno === 0) {
  54. $scope.data.url = resp.data.url;
  55. }
  56. });
  57. } else {
  58. alert("后缀只能是 jpg、gif 及 png");
  59. }
  60. };
  61. $scope.shortCut = function(e) {
  62. e.stopPropagation();
  63. if (e.keyCode == 13) {
  64. $scope.ok();
  65. } else if (e.keyCode == 27) {
  66. $scope.cancel();
  67. }
  68. };
  69. $scope.ok = function () {
  70. if($scope.data.R_URL.test($scope.data.url)) {
  71. $modalInstance.close({
  72. url: $scope.data.url,
  73. title: $scope.data.title
  74. });
  75. } else {
  76. $scope.urlPassed = false;
  77. var $imageUrl = $('#image-url');
  78. if ($imageUrl) {
  79. $imageUrl.focus();
  80. $imageUrl[0].setSelectionRange(0, $scope.data.url.length);
  81. }
  82. }
  83. editor.receiver.selectAll();
  84. };
  85. $scope.cancel = function () {
  86. $modalInstance.dismiss('cancel');
  87. editor.receiver.selectAll();
  88. };
  89. function getImageData() {
  90. var key = $scope.data.searchKeyword2;
  91. var currentTime = new Date();
  92. var url = 'http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&fp=result&queryWord='+ key +'&cl=2&lm=-1&ie=utf-8&oe=utf-8&st=-1&ic=0&word='+ key +'&face=0&istype=2&nc=1&pn=60&rn=60&gsm=3c&'+ currentTime.getTime() +'=&callback=JSON_CALLBACK';
  93. return $http.jsonp(url);
  94. }
  95. }]);