background.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. (function () {
  2. var backupStyle = editor.queryCommandValue('background');
  3. window.onload = function () {
  4. initTabs();
  5. initColorSelector();
  6. getImageData(initImagePanel);
  7. };
  8. /* 初始化tab标签 */
  9. function initTabs(){
  10. var tabs = $G('tabHeads').children;
  11. for (var i = 0; i < tabs.length; i++) {
  12. domUtils.on(tabs[i], "click", function (e) {
  13. var target = e.target || e.srcElement;
  14. for (var j = 0; j < tabs.length; j++) {
  15. if(tabs[j] == target){
  16. tabs[j].className = "focus";
  17. $G(tabs[j].getAttribute('data-content-id')).style.display = "block";
  18. }else {
  19. tabs[j].className = "";
  20. $G(tabs[j].getAttribute('data-content-id')).style.display = "none";
  21. }
  22. }
  23. });
  24. }
  25. }
  26. /* 初始化颜色设置 */
  27. function initColorSelector () {
  28. var obj = editor.queryCommandValue('background');
  29. if (obj) {
  30. var color = obj['background-color'],
  31. repeat = obj['background-repeat'] || 'repeat',
  32. image = obj['background-image'] || '',
  33. position = obj['background-position'] || 'center center',
  34. pos = position.split(' '),
  35. x = parseInt(pos[0]) || 0,
  36. y = parseInt(pos[1]) || 0;
  37. if(repeat == 'no-repeat' && (x || y)) repeat = 'self';
  38. image = image.match(/url[\s]*\(([^\)]*)\)/);
  39. image = image ? image[1]:'';
  40. updateFormState('colored', color, image, repeat, x, y);
  41. } else {
  42. updateFormState();
  43. }
  44. var updateHandler = function () {
  45. updateFormState();
  46. updateBackground();
  47. }
  48. domUtils.on($G('nocolorRadio'), 'click', updateBackground);
  49. domUtils.on($G('coloredRadio'), 'click', updateHandler);
  50. domUtils.on($G('url'), 'keyup', function(){
  51. if($G('url').value && $G('alignment').style.display == "none") {
  52. utils.each($G('repeatType').children, function(item){
  53. item.selected = ('repeat' == item.getAttribute('value') ? 'selected':false);
  54. });
  55. }
  56. updateHandler();
  57. });
  58. domUtils.on($G('repeatType'), 'change', updateHandler);
  59. domUtils.on($G('x'), 'keyup', updateBackground);
  60. domUtils.on($G('y'), 'keyup', updateBackground);
  61. initColorPicker();
  62. }
  63. /* 初始化颜色选择器 */
  64. function initColorPicker() {
  65. var me = editor,
  66. cp = $G("colorPicker");
  67. /* 生成颜色选择器ui对象 */
  68. var popup = new UE.ui.Popup({
  69. content: new UE.ui.ColorPicker({
  70. noColorText: me.getLang("clearColor"),
  71. editor: me,
  72. onpickcolor: function (t, color) {
  73. updateFormState('colored', color);
  74. updateBackground();
  75. UE.ui.Popup.postHide();
  76. },
  77. onpicknocolor: function (t, color) {
  78. updateFormState('colored', 'transparent');
  79. updateBackground();
  80. UE.ui.Popup.postHide();
  81. }
  82. }),
  83. editor: me,
  84. onhide: function () {
  85. }
  86. });
  87. /* 设置颜色选择器 */
  88. domUtils.on(cp, "click", function () {
  89. popup.showAnchor(this);
  90. });
  91. domUtils.on(document, 'mousedown', function (evt) {
  92. var el = evt.target || evt.srcElement;
  93. UE.ui.Popup.postHide(el);
  94. });
  95. domUtils.on(window, 'scroll', function () {
  96. UE.ui.Popup.postHide();
  97. });
  98. }
  99. /* 向后台拉取图片列表数据 */
  100. function getImageData(callback) {
  101. ajax.request(editor.options.imageManagerUrl, {
  102. timeout: 100000,
  103. action: 'get',
  104. onsuccess: function (r) {
  105. var data = r.responseText.split('ue_separate_ue');
  106. if(data.length && data[0]=='') data.shift();
  107. if(data.length && data[data.length-1]=='') data.pop();
  108. callback(data.length ? data:lang.noUploadImage);
  109. },
  110. onerror: function () {
  111. callback(lang.imageLoadError);
  112. }
  113. });
  114. }
  115. /* 初始化在线图片列表 */
  116. function initImagePanel(data) {
  117. var imagePanel = $G("imageList");
  118. if(utils.isArray(data) && data) {
  119. utils.each(data, function(value, key){
  120. var img = document.createElement("img"),
  121. div = document.createElement("div");
  122. div.appendChild(img);
  123. div.style.display = "none";
  124. imagePanel.appendChild(div);
  125. domUtils.on(img, 'click', function(e){
  126. var target = e.target || e.srcElement,
  127. nodes = imagePanel.childNodes;
  128. updateFormState('nocolor', null, '');
  129. for (var i = 0, node; node = nodes[i++];) {
  130. if(node.firstChild == target && !node.firstChild.getAttribute("selected")) {
  131. node.firstChild.setAttribute("selected", "true");
  132. node.firstChild.style.cssText = "filter:alpha(Opacity=50);-moz-opacity:0.5;opacity: 0.5;border:2px solid blue;";
  133. updateFormState('colored', null, target.getAttribute("src"), 'repeat');
  134. } else {
  135. node.firstChild.removeAttribute("selected");
  136. node.firstChild.style.cssText = "filter:alpha(Opacity=100);-moz-opacity:1;opacity: 1;border: 2px solid #fff";
  137. }
  138. }
  139. updateBackground();
  140. });
  141. img.onload = function (e) {
  142. this.parentNode.style.display = "";
  143. var w = this.width, h = this.height;
  144. scale(this, 95, 120, 80);
  145. this.title = lang.toggleSelect + w + "X" + h;
  146. };
  147. img.setAttribute(key < 35 ? "src" : "lazy_src", editor.options.imageManagerPath + value.replace(/\s+|\s+/ig, ""));
  148. img.setAttribute("_src", editor.options.imageManagerPath + value.replace(/\s+|\s+/ig, ""));
  149. });
  150. } else {
  151. imagePanel.innerHTML = "&nbsp;&nbsp;" + data;
  152. }
  153. }
  154. /* 图片缩放 */
  155. function scale(img, max, oWidth, oHeight) {
  156. var width = 0, height = 0, percent, ow = img.width || oWidth, oh = img.height || oHeight;
  157. if (ow > max || oh > max) {
  158. if (ow >= oh) {
  159. if (width = ow - max) {
  160. percent = (width / ow).toFixed(2);
  161. img.height = oh - oh * percent;
  162. img.width = max;
  163. }
  164. } else {
  165. if (height = oh - max) {
  166. percent = (height / oh).toFixed(2);
  167. img.width = ow - ow * percent;
  168. img.height = max;
  169. }
  170. }
  171. }
  172. }
  173. /* 更新背景色设置面板 */
  174. function updateFormState (radio, color, url, align, x, y) {
  175. var nocolorRadio = $G('nocolorRadio'),
  176. coloredRadio = $G('coloredRadio');
  177. if(radio) {
  178. nocolorRadio.checked = (radio == 'colored' ? false:'checked');
  179. coloredRadio.checked = (radio == 'colored' ? 'checked':false);
  180. }
  181. if(color) {
  182. domUtils.setStyle($G("colorPicker"), "background-color", color);
  183. }
  184. if(url || url === '') {
  185. $G('url').value = url;
  186. }
  187. if(align) {
  188. utils.each($G('repeatType').children, function(item){
  189. item.selected = (align == item.getAttribute('value') ? 'selected':false);
  190. });
  191. }
  192. if(x || y) {
  193. $G('x').value = parseInt(x) || 0;
  194. $G('y').value = parseInt(y) || 0;
  195. }
  196. $G('alignment').style.display = coloredRadio.checked && $G('url').value ? '':'none';
  197. $G('custom').style.display = coloredRadio.checked && $G('url').value && $G('repeatType').value == 'self' ? '':'none';
  198. }
  199. /* 更新背景颜色 */
  200. function updateBackground () {
  201. if ($G('coloredRadio').checked) {
  202. var color = domUtils.getStyle($G("colorPicker"), "background-color"),
  203. bgimg = $G("url").value,
  204. align = $G("repeatType").value,
  205. backgroundObj = {
  206. "background-repeat": "no-repeat",
  207. "background-position": "center center"
  208. };
  209. if (color) backgroundObj["background-color"] = color;
  210. if (bgimg) backgroundObj["background-image"] = 'url(' + bgimg + ')';
  211. if (align == 'self') {
  212. backgroundObj["background-position"] = $G("x").value + "px " + $G("y").value + "px";
  213. } else if (align == 'repeat-x' || align == 'repeat-y' || align == 'repeat') {
  214. backgroundObj["background-repeat"] = align;
  215. }
  216. editor.execCommand('background', backgroundObj);
  217. } else {
  218. editor.execCommand('background', null);
  219. }
  220. }
  221. dialog.onok = function () {
  222. updateBackground();
  223. editor.fireEvent('saveScene');
  224. };
  225. dialog.oncancel = function () {
  226. editor.execCommand('background', backupStyle);
  227. };
  228. })()