background.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. (function () {
  2. var onlineImage,
  3. backupStyle = editor.queryCommandValue('background');
  4. window.onload = function () {
  5. initTabs();
  6. initColorSelector();
  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. var contentId = tabs[j].getAttribute('data-content-id');
  18. $G(contentId).style.display = "block";
  19. } else {
  20. tabs[j].className = "";
  21. $G(tabs[j].getAttribute('data-content-id')).style.display = "none";
  22. }
  23. }
  24. });
  25. }
  26. }
  27. /* 初始化颜色设置 */
  28. function initColorSelector() {
  29. var obj = editor.queryCommandValue('background');
  30. if (obj) {
  31. var color = obj['background-color'],
  32. repeat = obj['background-repeat'] || 'repeat',
  33. image = obj['background-image'] || '',
  34. position = obj['background-position'] || 'center center',
  35. pos = position.split(' '),
  36. x = parseInt(pos[0]) || 0,
  37. y = parseInt(pos[1]) || 0;
  38. if (repeat == 'no-repeat' && (x || y)) repeat = 'self';
  39. image = image.match(/url[\s]*\(([^\)]*)\)/);
  40. image = image ? image[1] : '';
  41. updateFormState('colored', color, image, repeat, x, y);
  42. } else {
  43. updateFormState();
  44. }
  45. var updateHandler = function () {
  46. updateFormState();
  47. updateBackground();
  48. }
  49. domUtils.on($G('nocolorRadio'), 'click', updateBackground);
  50. domUtils.on($G('coloredRadio'), 'click', updateHandler);
  51. domUtils.on($G('url'), 'keyup', function () {
  52. if ($G('url').value && $G('alignment').style.display == "none") {
  53. utils.each($G('repeatType').children, function (item) {
  54. item.selected = ('repeat' == item.getAttribute('value') ? 'selected' : false);
  55. });
  56. }
  57. updateHandler();
  58. });
  59. domUtils.on($G('repeatType'), 'change', updateHandler);
  60. domUtils.on($G('x'), 'keyup', updateBackground);
  61. domUtils.on($G('y'), 'keyup', updateBackground);
  62. initColorPicker();
  63. }
  64. /* 初始化颜色选择器 */
  65. function initColorPicker() {
  66. var me = editor,
  67. cp = $G("colorPicker");
  68. /* 生成颜色选择器ui对象 */
  69. var popup = new UE.ui.Popup({
  70. content: new UE.ui.ColorPicker({
  71. noColorText: me.getLang("clearColor"),
  72. editor: me,
  73. onpickcolor: function (t, color) {
  74. updateFormState('colored', color);
  75. updateBackground();
  76. UE.ui.Popup.postHide();
  77. },
  78. onpicknocolor: function (t, color) {
  79. updateFormState('colored', 'transparent');
  80. updateBackground();
  81. UE.ui.Popup.postHide();
  82. }
  83. }),
  84. editor: me,
  85. onhide: function () {
  86. }
  87. });
  88. /* 设置颜色选择器 */
  89. domUtils.on(cp, "click", function () {
  90. popup.showAnchor(this);
  91. });
  92. domUtils.on(document, 'mousedown', function (evt) {
  93. var el = evt.target || evt.srcElement;
  94. UE.ui.Popup.postHide(el);
  95. });
  96. domUtils.on(window, 'scroll', function () {
  97. UE.ui.Popup.postHide();
  98. });
  99. }
  100. /* 更新背景色设置面板 */
  101. function updateFormState(radio, color, url, align, x, y) {
  102. var nocolorRadio = $G('nocolorRadio'),
  103. coloredRadio = $G('coloredRadio');
  104. if (radio) {
  105. nocolorRadio.checked = (radio == 'colored' ? false : 'checked');
  106. coloredRadio.checked = (radio == 'colored' ? 'checked' : false);
  107. }
  108. if (color) {
  109. domUtils.setStyle($G("colorPicker"), "background-color", color);
  110. }
  111. if (url && /^\//.test(url)) {
  112. var a = document.createElement('a');
  113. a.href = url;
  114. browser.ie && (a.href = a.href);
  115. url = browser.ie ? a.href : (a.protocol + '//' + a.host + a.pathname + a.search + a.hash);
  116. }
  117. if (url || url === '') {
  118. $G('url').value = url;
  119. }
  120. if (align) {
  121. utils.each($G('repeatType').children, function (item) {
  122. item.selected = (align == item.getAttribute('value') ? 'selected' : false);
  123. });
  124. }
  125. if (x || y) {
  126. $G('x').value = parseInt(x) || 0;
  127. $G('y').value = parseInt(y) || 0;
  128. }
  129. $G('alignment').style.display = coloredRadio.checked && $G('url').value ? '' : 'none';
  130. $G('custom').style.display = coloredRadio.checked && $G('url').value && $G('repeatType').value == 'self' ? '' : 'none';
  131. }
  132. /* 更新背景颜色 */
  133. function updateBackground() {
  134. if ($G('coloredRadio').checked) {
  135. var color = domUtils.getStyle($G("colorPicker"), "background-color"),
  136. bgimg = $G("url").value,
  137. align = $G("repeatType").value,
  138. backgroundObj = {
  139. "background-repeat": "no-repeat",
  140. "background-position": "center center"
  141. };
  142. if (color) backgroundObj["background-color"] = color;
  143. if (bgimg) backgroundObj["background-image"] = 'url(' + bgimg + ')';
  144. if (align == 'self') {
  145. backgroundObj["background-position"] = $G("x").value + "px " + $G("y").value + "px";
  146. } else if (align == 'repeat-x' || align == 'repeat-y' || align == 'repeat') {
  147. backgroundObj["background-repeat"] = align;
  148. }
  149. editor.execCommand('background', backgroundObj);
  150. } else {
  151. editor.execCommand('background', null);
  152. }
  153. }
  154. /* 在线图片 */
  155. function OnlineImage(target) {
  156. this.container = utils.isString(target) ? document.getElementById(target) : target;
  157. this.init();
  158. }
  159. OnlineImage.prototype = {
  160. init: function () {
  161. this.reset();
  162. this.initEvents();
  163. },
  164. /* 初始化容器 */
  165. initContainer: function () {
  166. this.container.innerHTML = '';
  167. this.list = document.createElement('ul');
  168. this.clearFloat = document.createElement('li');
  169. domUtils.addClass(this.list, 'list');
  170. domUtils.addClass(this.clearFloat, 'clearFloat');
  171. this.list.id = 'imageListUl';
  172. this.list.appendChild(this.clearFloat);
  173. this.container.appendChild(this.list);
  174. },
  175. /* 初始化滚动事件,滚动到地步自动拉取数据 */
  176. initEvents: function () {
  177. var _this = this;
  178. /* 滚动拉取图片 */
  179. domUtils.on($G('imageList'), 'scroll', function (e) {
  180. var panel = this;
  181. if (panel.scrollHeight - (panel.offsetHeight + panel.scrollTop) < 10) {
  182. _this.getImageData();
  183. }
  184. });
  185. /* 选中图片 */
  186. domUtils.on(this.container, 'click', function (e) {
  187. var target = e.target || e.srcElement,
  188. li = target.parentNode,
  189. nodes = $G('imageListUl').childNodes;
  190. if (li.tagName.toLowerCase() == 'li') {
  191. updateFormState('nocolor', null, '');
  192. for (var i = 0, node; node = nodes[i++];) {
  193. if (node == li && !domUtils.hasClass(node, 'selected')) {
  194. domUtils.addClass(node, 'selected');
  195. updateFormState('colored', null, li.firstChild.getAttribute("_src"), 'repeat');
  196. } else {
  197. domUtils.removeClasses(node, 'selected');
  198. }
  199. }
  200. updateBackground();
  201. }
  202. });
  203. },
  204. /* 初始化第一次的数据 */
  205. initData: function () {
  206. /* 拉取数据需要使用的值 */
  207. this.state = 0;
  208. this.listSize = editor.getOpt('imageManagerListSize');
  209. this.listIndex = 0;
  210. this.listEnd = false;
  211. /* 第一次拉取数据 */
  212. this.getImageData();
  213. },
  214. /* 重置界面 */
  215. reset: function () {
  216. this.initContainer();
  217. this.initData();
  218. },
  219. /* 向后台拉取图片列表数据 */
  220. getImageData: function () {
  221. var _this = this;
  222. if (!_this.listEnd && !this.isLoadingData) {
  223. this.isLoadingData = true;
  224. var url = editor.getActionUrl(editor.getOpt('imageManagerActionName')),
  225. isJsonp = utils.isCrossDomainUrl(url);
  226. ajax.request(url, {
  227. 'timeout': 100000,
  228. 'dataType': isJsonp ? 'jsonp' : '',
  229. 'data': utils.extend({
  230. start: this.listIndex,
  231. size: this.listSize
  232. }, editor.queryCommandValue('serverparam')),
  233. 'headers': editor.options.serverHeaders || {},
  234. 'method': 'get',
  235. 'onsuccess': function (r) {
  236. try {
  237. var json = isJsonp ? r : eval('(' + r.responseText + ')');
  238. if (json.state == 'SUCCESS') {
  239. _this.pushData(json.list);
  240. _this.listIndex = parseInt(json.start) + parseInt(json.list.length);
  241. if (_this.listIndex >= json.total) {
  242. _this.listEnd = true;
  243. }
  244. _this.isLoadingData = false;
  245. }
  246. } catch (e) {
  247. if (r.responseText.indexOf('ue_separate_ue') != -1) {
  248. var list = r.responseText.split(r.responseText);
  249. _this.pushData(list);
  250. _this.listIndex = parseInt(list.length);
  251. _this.listEnd = true;
  252. _this.isLoadingData = false;
  253. }
  254. }
  255. },
  256. 'onerror': function () {
  257. _this.isLoadingData = false;
  258. }
  259. });
  260. }
  261. },
  262. /* 添加图片到列表界面上 */
  263. pushData: function (list) {
  264. var i, item, img, icon, _this = this,
  265. urlPrefix = editor.getOpt('imageManagerUrlPrefix');
  266. for (i = 0; i < list.length; i++) {
  267. if (list[i] && list[i].url) {
  268. item = document.createElement('li');
  269. img = document.createElement('img');
  270. icon = document.createElement('span');
  271. domUtils.on(img, 'load', (function (image) {
  272. return function () {
  273. _this.scale(image, image.parentNode.offsetWidth, image.parentNode.offsetHeight);
  274. }
  275. })(img));
  276. img.width = 113;
  277. img.setAttribute('src', urlPrefix + list[i].url + (list[i].url.indexOf('?') == -1 ? '?noCache=' : '&noCache=') + (+new Date()).toString(36));
  278. img.setAttribute('_src', urlPrefix + list[i].url);
  279. domUtils.addClass(icon, 'icon');
  280. item.appendChild(img);
  281. item.appendChild(icon);
  282. this.list.insertBefore(item, this.clearFloat);
  283. }
  284. }
  285. },
  286. /* 改变图片大小 */
  287. scale: function (img, w, h, type) {
  288. var ow = img.width,
  289. oh = img.height;
  290. if (type == 'justify') {
  291. if (ow >= oh) {
  292. img.width = w;
  293. img.height = h * oh / ow;
  294. img.style.marginLeft = '-' + parseInt((img.width - w) / 2) + 'px';
  295. } else {
  296. img.width = w * ow / oh;
  297. img.height = h;
  298. img.style.marginTop = '-' + parseInt((img.height - h) / 2) + 'px';
  299. }
  300. } else {
  301. if (ow >= oh) {
  302. img.width = w * ow / oh;
  303. img.height = h;
  304. img.style.marginLeft = '-' + parseInt((img.width - w) / 2) + 'px';
  305. } else {
  306. img.width = w;
  307. img.height = h * oh / ow;
  308. img.style.marginTop = '-' + parseInt((img.height - h) / 2) + 'px';
  309. }
  310. }
  311. },
  312. getInsertList: function () {
  313. var i, lis = this.list.children, list = [], align = getAlign();
  314. for (i = 0; i < lis.length; i++) {
  315. if (domUtils.hasClass(lis[i], 'selected')) {
  316. var img = lis[i].firstChild,
  317. src = img.getAttribute('_src');
  318. list.push({
  319. src: src,
  320. _src: src,
  321. floatStyle: align
  322. });
  323. }
  324. }
  325. return list;
  326. }
  327. };
  328. dialog.onok = function () {
  329. updateBackground();
  330. editor.fireEvent('saveScene');
  331. };
  332. dialog.oncancel = function () {
  333. editor.execCommand('background', backupStyle);
  334. };
  335. })();