growHeight.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. window.growHeightList = window.growHeightList || {};
  2. function getGrowHeight(id, maxHeight, buttons, type, acceptEnter) {
  3. var gh = growHeightList[id];
  4. if (!gh) {
  5. /* 再改,GrowhTagComper.doCompTag( 里,maxHeight 为 null 时,wd.edit.initGrowHigh( 的参数会被格式化为 'null'。Lin
  6. if (maxHeight != null) {
  7. */
  8. /* 再改回,对老的 ccp、objp,会导致不出现下拉列表(只有一个空框)。Lin
  9. * objectPicker.js 里的 init(initparam) 里的 GrowHeightInstance=window.growHeightList[thisid] 为空,导致抛出空指针
  10. * 相应的,GrowhTagComper.doCompTag( 里 maxHeight 为 null 时,wd.edit.initGrowHigh( 的参数要改为 null -- 不能是 'null'
  11. if (maxHeight != null && maxHeight !== 'null') {
  12. */ if (maxHeight != null) {
  13. gh = new GrowHeight(id, maxHeight, buttons, type, acceptEnter);
  14. } else {
  15. console.info("找不到GrowHeight:" + id);
  16. }
  17. }
  18. return gh;
  19. }
  20. var GrowHeight = GrowHeight = function (id, maxHeight, buttons, type, acceptEnter) {
  21. var ele = document.getElementById(id);
  22. if (ele) {
  23. this.showPadding = 0;
  24. this.name = id;
  25. this.id = id;
  26. this.maxHeight = parseFloat(maxHeight) || 300;
  27. this.type = type;
  28. this.acceptEnter = new Boolean(acceptEnter) == true;
  29. this.srcElement = ele;
  30. this.fj = buttons.fj;
  31. this.init();
  32. window.growHeightList[id] = this;
  33. } else {
  34. console.log("找不到元素:" + id);
  35. }
  36. }
  37. GrowHeight.prototype.getName = function () {
  38. return this.name;
  39. }
  40. GrowHeight.prototype.val = function (value) {
  41. if (this.container.tagName.toLowerCase() == "textarea") {
  42. if (arguments.length == 0) {
  43. value = this.input.value;
  44. } else {
  45. this.container.value = value;
  46. this.onInput();
  47. }
  48. } else {
  49. if (arguments.length == 0) {
  50. value = this.input.value;
  51. } else {
  52. this.container.innerHTML = value;
  53. this.onInput();
  54. }
  55. }
  56. if (this.isFocus != true) {
  57. this.container.initDot && this.container.initDot();
  58. }
  59. return value;
  60. }
  61. GrowHeight.prototype.setOnInput = function (callback) {
  62. this.onInputCallback = callback;
  63. }
  64. GrowHeight.prototype.setOnfocus = function (callback) {
  65. this.onFocusCallback = callback;
  66. }
  67. GrowHeight.prototype.setOnblur = function (callback) {
  68. this.onBlurCallback = callback;
  69. }
  70. GrowHeight.prototype.show = function () {
  71. if (this.shadow)
  72. return;
  73. //缓存旧数据
  74. var ori = this.oriInfo = {
  75. wrapper: this.wrapper.style.cssText,
  76. container: this.container.style.cssText,
  77. parent_: this.wrapper.parentNode.style.cssText
  78. };
  79. var e = $HIDDENOVERFLOW(this.wrapper);
  80. var eb = e.getBoundingClientRect();
  81. var pB = this.wrapper.parentNode.getBoundingClientRect();
  82. var bound = this.wrapper.getBoundingClientRect();
  83. //创建站位空格
  84. var shadow = this.shadow = this.wrapper.cloneNode();
  85. shadow.id = "shadow_" + (this.wrapper.name || this.wrapper.id || "");
  86. shadow.name = "";
  87. shadow.style.display="inline-block";
  88. shadow.style.overflowY = "hidden";
  89. shadow.style.visibility = "hidden";
  90. var csstxt = this.wrapper.style.cssText;
  91. var csss=csstxt.split(";");
  92. var hasCssWidth=false,hasCssHeight=false;
  93. for (var ci=0;ci<csss.length;ci++){
  94. if(csss[ci].startsWith("width:")){
  95. hasCssWidth=true;
  96. }else if(csss[ci].startsWith("height:")){
  97. hasCssHeight=true;
  98. }
  99. }
  100. if (!hasCssWidth) {
  101. $(shadow).css("width", $W(this.wrapper));
  102. }
  103. if (!hasCssHeight) {
  104. $(shadow).css("height", $H(this.wrapper));
  105. }
  106. // var w = this.wrapper.style.width,
  107. // h = this.wrapper.style.height;
  108. // if (w == "" || !w.startsWith("calc")) {
  109. // w = $W(this.wrapper) + "px";
  110. // }
  111. // if (h == "" || !h.startsWith("calc")) {
  112. // h = $H(this.wrapper) + "px";
  113. // }
  114. // console.log(w, h);
  115. // $(shadow).css({
  116. // width: w,
  117. // height: h
  118. // })
  119. // shadow.style.display = "inline-block";
  120. //先浮出来以免影响坐标计算
  121. this.wrapper.parentNode.style.position = "relative";
  122. this.wrapper.style.position = "absolute";
  123. this.wrapper.parentNode.insertBefore(shadow, this.wrapper.nextSibling);
  124. $(this.wrapper).addClass("input-div-selected");
  125. // this.wrapper.style.overflow = "hidden";
  126. this.wrapper.style.width = bound.width + "px";
  127. this.wrapper.style.height = "unset";
  128. // var height_ = $REALHEIGHT(this.wrapper);
  129. var marginTop=parseInt($(this.wrapper).css("margin-top"))||0;
  130. var marginBottom=parseInt($(this.wrapper).css("margin-bottom"))||0;
  131. this.wrapper.style.background = "white";
  132. this.wrapper.style.left = bound.left - pB.left - $ML(this.wrapper) - 1 + "px";
  133. this.wrapper.style.zIndex = 1;
  134. //判断够不够位置,调整最大高度
  135. var maxHeight_ = this.maxHeight;
  136. if (bound.top + maxHeight_ < eb.bottom) {
  137. this.wrapper.style.top = bound.top - pB.top - 1-marginTop + "px";
  138. this.wrapper.style.bottom = "";
  139. } else { //如果向下不够位置展开,尝试向上,并且判断尽可能的放大
  140. //如果向上高度大于向下高度
  141. var b_ = eb.bottom - bound.bottom; //底部距离
  142. var t_ = bound.top - eb.top; //顶部距离
  143. maxHeight_ = Math.min(Math.max(b_, t_) + bound.height - $PB(this.wrapper) - $PT(this.wrapper), maxHeight_);
  144. this.wrapper.style.bottom = pB.bottom - bound.bottom - $MB(this.wrapper)-marginBottom + "px";
  145. this.wrapper.style.top = "";
  146. }
  147. this.wrapper.style.maxHeight = maxHeight_ + "px";
  148. this.updateWrapperPosition();
  149. this.updateContainerHeight();
  150. if (this.cmsBtn) {
  151. this.cmsBtn.updateButtonSize();
  152. }
  153. loadSmallScorll($(this.wrapper).find(".smallScrollbar"));
  154. }
  155. GrowHeight.prototype.updateWrapperPosition = function () {
  156. if (this.shadow) {
  157. var s = this.shadow.getBoundingClientRect(),
  158. w = this.wrapper.getBoundingClientRect(),
  159. wb = parseFloat(this.wrapper.style.bottom),
  160. wt = parseFloat(this.wrapper.style.top),
  161. left = parseFloat(this.wrapper.style.left);
  162. this.wrapper.style.left = left + s.left - w.left + "px";
  163. }
  164. }
  165. GrowHeight.prototype.updateContainerHeight = function () {
  166. var h = $minHeight100(this.container);
  167. if (this.shadow) { //
  168. var pd = this.showPadding;
  169. if (pd > 0) {
  170. this.wrapper.style.left = parseFloat(this.wrapper.style.left) - pd + "px"; //x左移
  171. this.wrapper.style.width = $REALWIDTH(this.shadow) + pd * 2 + "px"; //y右移
  172. this.wrapper.style.padding = pd + "px";
  173. var b = this.wrapper.style.bottom;
  174. if (b != "") {
  175. this.wrapper.style.bottom = parseFloat(b) - pd + "px";
  176. } else {
  177. b = this.wrapper.style.top;
  178. this.wrapper.style.top = parseFloat(b) - pd + "px";
  179. }
  180. var maxH = $MAH(this.wrapper) /* this.maxHeight */ + pd;
  181. // var se=$HIDDENOVERFLOW(this.wrapper);
  182. //计算maxheight
  183. this.wrapper.style.maxHeight = maxH + "px";
  184. }
  185. this.container.style.height = h + "px";
  186. var sh = this.container.scrollHeight,
  187. mh = $MAH(this.wrapper) - ($ISBOX(this.wrapper) ? ($PT(this.wrapper) + $PB(this.wrapper)) : 0);
  188. h = Math.min(sh, mh);
  189. this.container.style.overflow = "hidden";
  190. this.container.scrollTop = this.lastScrollTop;
  191. } else {}
  192. this.container.style.height = h + "px";
  193. }
  194. GrowHeight.prototype.hide = function () {
  195. try{
  196. $(this.wrapper).removeClass("input-div-selected");
  197. $(this.wrapper).addClass("input-s");
  198. $(this.wrapper).find(".smallScrollbar").getNiceScroll().remove();
  199. if (this.shadow) {
  200. if (this.shadow.parentNode != this.wrapper.parentNode)
  201. this.shadow.parentNode.insertBefore(this.wrapper, this.shadow);
  202. this.shadow.parentNode.removeChild(this.shadow);
  203. this.shadow = null;
  204. }
  205. var ori = this.oriInfo;
  206. if (ori) {
  207. this.wrapper.style.cssText = ori.wrapper;
  208. this.container.style.cssText = ori.container;
  209. this.wrapper.parentNode.style.cssText = ori.parent_;
  210. this.oriInfo = null;
  211. }
  212. if (this.cmsBtn) {
  213. //更新CMS按钮的位置
  214. this.cmsBtn.updateButtonSize();
  215. }
  216. }
  217. catch(e){
  218. console.log(e)
  219. }
  220. }
  221. GrowHeight.prototype.initAsDiv = function () {
  222. if (this.inited === true)
  223. return;
  224. function _focus() {
  225. this.GrowHeight.show();
  226. this.focus();
  227. this.addEventListener("blur", _blur);
  228. this.GrowHeight.onFocus();
  229. // this.innerHTML = this.GrowHeight.input.value;
  230. }
  231. function _blur() {
  232. this.GrowHeight.hide();
  233. this.removeEventListener("blur", _blur);
  234. this.GrowHeight.onBlur();
  235. this.GrowHeight.updateInputValue();
  236. }
  237. // this.onInputCallback = function (container) {
  238. // this.updateInputValue();
  239. // }
  240. var onDivInput = function (e) {
  241. if (!this.GrowHeight.acceptEnter) {
  242. var et = e || window.event;
  243. var keycode = et.charCode || et.keyCode;
  244. if (keycode == 13) {
  245. if (window.event) {
  246. window.event.returnValue = false;
  247. } else {
  248. e.preventDefault(); //for firefox
  249. }
  250. return;
  251. }
  252. }
  253. this.GrowHeight.onInput();
  254. }
  255. this.initDot = null;
  256. this.container = this.srcElement;
  257. this.container.style.height = $height100(this.container) + "px";
  258. this.container.style.overflow = "hidden";
  259. this.container.setAttribute("contenteditable", "true");
  260. this.container.addEventListener("focus", _focus);
  261. var inputEventNames = ["input", "propertychange", "keydown", "keyup", "DOMSubtreeModified"];
  262. for (var iiii = 0; iiii < inputEventNames.length; iiii++) {
  263. this.container.addEventListener(inputEventNames[iiii], onDivInput);
  264. }
  265. this.input = document.createElement("textarea");
  266. this.input.style.display = "none";
  267. this.input.name = this.id;
  268. this.container.parentNode.appendChild(this.input);
  269. }
  270. GrowHeight.prototype.initPlay = function () {
  271. if (this.inited === true)
  272. return;
  273. this.showPadding = 6;
  274. function onmouseenter() {
  275. this.GrowHeight.val(this.GrowHeight.srcText);
  276. this.GrowHeight.show();
  277. this.oriResize = this.style.resize;
  278. if (this.GrowHeight.cmsBtn)
  279. this.GrowHeight.cmsBtn.updateButtonSize();
  280. this.addEventListener("mouseleave", onmouseleave);
  281. }
  282. function onmouseleave(event) {
  283. if (this.clicked || (event.ctrlKey && event.altKey))
  284. return;
  285. this.style.resize = this.oriResize || "";
  286. this.GrowHeight.val(this.GrowHeight.viewText);
  287. this.removeEventListener("mouseleave", onmouseleave);
  288. this.GrowHeight.hide();
  289. }
  290. function onclick_() {
  291. this.clicked = !(this.clicked || false);
  292. if (this.clicked) {
  293. this.oriOutline = this.style.outline;
  294. this.oriResize = this.style.resize;
  295. this.style.outline = "1px blue auto";
  296. this.style.resize = "both";
  297. this.GrowHeight.onFocus();
  298. } else {
  299. this.style.outline = this.oriOutline;
  300. this.style.resize = this.oriResize;
  301. this.GrowHeight.onBlur();
  302. }
  303. }
  304. this.container = this.srcElement;
  305. // this.container.initDot = function () {
  306. // this.innerText = this.GrowHeight.srcText;
  307. // this.GrowHeight.viewText = wd.display.ellipsisContent(this, $maxHeight100(this));
  308. // }
  309. this.container.style.minHeight = this.minHeight + "px";
  310. //....
  311. this.srcText = this.container.innerText;
  312. // this.innerHTML = "";
  313. var ses = this.srcElement.style;
  314. var od = {
  315. wrapper: this.wrapper.style.cssText,
  316. container: this.container.style.cssText
  317. };
  318. var initHeight = $maxHeight100(this.container)
  319. //平铺开,强制换行
  320. // ses.visibility = "hidden";
  321. // ses.height = "";
  322. ses.wordBreak = "break-all";
  323. ses.wordWrap = "break-word";
  324. //弄成最大宽度,若么有,忽略
  325. this.wrapper.style.width = $MAWS(this.srcElement, 0) + "px";
  326. ses.width = $W(this.wrapper);
  327. var th = $MIH(this.container) || this.minHeight,
  328. h = $H(this.container);
  329. if (h > th) {
  330. var lh = $LH(this.container);
  331. if (lh > th) {
  332. console.info("注意,行高已经超过元素高度,导致元素被撑大")
  333. }
  334. // this.container.initDot();
  335. this.container.innerText = this.srcText;
  336. this.viewText = wd.display.ellipsisContent(this.container, initHeight);
  337. this.wrapper.addEventListener("mouseenter", onmouseenter);
  338. this.wrapper.addEventListener("dblclick", onclick_)
  339. }
  340. this.wrapper.style.cssText = od.wrapper;
  341. this.container.style.cssText = od.container;
  342. this.input = document.createElement("input");
  343. this.input.value = this.wrapper.innerHTML;
  344. this.input.type = "hidden";
  345. this.wrapper.parentNode.insertBefore(this.input, this.wrapper);
  346. }
  347. GrowHeight.prototype.onInput = function () {
  348. this.lastScrollTop = this.container.scrollTop;
  349. this.updateContainerHeight();
  350. if (this.cmsBtn) {
  351. //更新CMS按钮的位置
  352. this.cmsBtn.updateButtonSize();
  353. }
  354. this.updateInputValue && this.updateInputValue();
  355. this.onInputCallback && this.onInputCallback.call(this, this.container);
  356. }
  357. GrowHeight.prototype.onFocus = function () { //
  358. {
  359. var textDom = this.container;
  360. if (textDom.setSelectionRange) {
  361. // IE Support
  362. textDom.focus();
  363. textDom.setSelectionRange(this.cursorPos, this.cursorPos);
  364. } else if (textDom.createTextRange) {
  365. // Firefox support
  366. var range = textDom.createTextRange();
  367. range.collapse(true);
  368. range.moveEnd('character', this.cursorPos);
  369. range.moveStart('character', this.cursorPos);
  370. range.select();
  371. }
  372. this.isFocus = true;
  373. }
  374. if (this.cmsBtn) {
  375. //更新CMS按钮的位置
  376. this.cmsBtn.updateButtonSize();
  377. }
  378. this.onFocusCallback && this.onFocusCallback.call(this.GrowHeight, this);
  379. }
  380. GrowHeight.prototype.onBlur = function () { //
  381. {
  382. var textDom = this.container;
  383. if (document.selection) {
  384. // IE Support
  385. // 有bug,先注释了,IE会不断获取焦点
  386. // textDom.focus();
  387. var selectRange = document.selection.createRange();
  388. selectRange.moveStart('character', -textDom.value.length);
  389. this.cursorPos = selectRange.text.length;
  390. } else if (textDom.selectionStart || textDom.selectionStart == '0') {
  391. // Firefox support
  392. this.cursorPos = textDom.selectionStart;
  393. }
  394. this.isFocus = false;
  395. }
  396. if (this.cmsBtn) {
  397. //更新CMS按钮的位置
  398. this.cmsBtn.updateButtonSize();
  399. }
  400. this.onBlurCallback && this.onBlurCallback.call(this.GrowHeight, this);
  401. }
  402. GrowHeight.prototype.updateInputValue = function () {
  403. function getText() {
  404. var nn = this.nodeName;
  405. if (nn == "TEXTAREA" || nn == "INPUT") {
  406. return this.value;
  407. } else if (nn == "DIV") {
  408. return this.innerHTML;
  409. } else {
  410. return this.innerText;
  411. }
  412. }
  413. if (this.input && this.container) {
  414. var value = getText.call(this.container);
  415. if (this.acceptEnter || this.type > 1) { //接受回车,或者是播放
  416. this.input.value = value;
  417. } else {
  418. if (this.container.nodeName == 'DIV') {
  419. value = value.replace('<br>', "");
  420. }
  421. value = value.replace(/\n/g, '');
  422. this.input.value = value;
  423. }
  424. }
  425. }
  426. GrowHeight.prototype.initAsInput = function () {
  427. if (this.inited === true)
  428. return;
  429. var gh = this;
  430. var editorOninput = function (e) {
  431. if (!this.GrowHeight.acceptEnter) {
  432. var et = e || window.event;
  433. var keycode = et.charCode || et.keyCode;
  434. if (keycode == 13) {
  435. if (window.event) {
  436. window.event.returnValue = false;
  437. } else {
  438. e.preventDefault(); //for firefox
  439. }
  440. return;
  441. }
  442. }
  443. this.GrowHeight.onInput();
  444. this.GrowHeight.updateInputValue();
  445. }
  446. // this.onInputCallback = function (container) {
  447. // this.updateInputValue();
  448. // }
  449. function editorOnfocus() {
  450. this.isFocus = true;
  451. //浮出来
  452. this.value = this.GrowHeight.input.value;
  453. this.GrowHeight.show();
  454. this.focus();
  455. this.addEventListener("blur", editorOnblur);
  456. this.GrowHeight.onFocus();
  457. editorOninput.call(this);
  458. }
  459. function editorOnblur() {
  460. this.isFocus = false;
  461. //弄回去
  462. this.GrowHeight.updateInputValue();
  463. this.GrowHeight.hide();
  464. this.removeEventListener("blur", editorOnblur);
  465. this.GrowHeight.onBlur();
  466. this.initDot();
  467. }
  468. var container = this.container = document.createElement("textarea");
  469. this.wrapper.insertBefore(this.container, this.srcElement);
  470. //placeholder支持
  471. if (this.srcElement.getAttribute("placeholder"))
  472. container.setAttribute("placeholder", this.srcElement.getAttribute("placeholder"));
  473. container.GrowHeight = this;
  474. //克隆样式
  475. container.className = "form-text";
  476. container.style.width = "100%";
  477. container.style.paddingLeft = "5px";
  478. container.style.paddingTop = "5px";
  479. container.style.resize = "none";
  480. container.style.overflowY = "hidden";
  481. container.style.height = $minHeight100(this.container) + "px";
  482. container.style.minHeight = $minHeight100(this.container) + "px";
  483. var stn = []; //["border", "border-radius"];
  484. for (var i = 0; i < stn.length; i++) {
  485. container.style[stn[i]] = $CSS(this.srcElement, stn[i]);
  486. }
  487. if (this.srcElement.getAttribute("placeholder"))
  488. this.container.setAttribute("placeholder", this.srcElement.getAttribute("placeholder"));
  489. var inputEventNames = ["input", "propertychange", "keydown", "keyup"];
  490. for (var i = 0; i < inputEventNames.length; i++) {
  491. (function (evname) {
  492. container.addEventListener(evname, editorOninput);
  493. })(inputEventNames[i]);
  494. }
  495. container.addEventListener("focus", editorOnfocus);
  496. container.initDot = function () {
  497. wd.display.ellipsisContent(this, $maxHeight100(this));
  498. }
  499. this.input = document.createElement("textarea");
  500. this.input.name = this.id;
  501. // this.input.style.cssText = container.style.cssText;
  502. this.input.style.display = "none";
  503. this.input.GrowHeight = this;
  504. this.wrapper.insertBefore(this.input, this.container);
  505. this.val(this.srcElement.innerHTML);
  506. this.wrapper.removeChild(this.srcElement);
  507. this.updateContainerHeight();
  508. }
  509. GrowHeight.prototype.getInputElement = function () {
  510. return this.container;
  511. }
  512. GrowHeight.prototype.getInput = function () {
  513. return this.container;
  514. }
  515. GrowHeight.prototype.initPlayHtml = function () {
  516. if (this.inited === true)
  517. return;
  518. this.showPadding = 6;
  519. var container = this.container = this.srcElement,
  520. that = this;
  521. this.minHeight = $minHeight100(container);
  522. container.style.height = $maxHeight100(container) + "px";
  523. container.style.overflowY = "auto";
  524. function enter(event) {
  525. this.GrowHeight.show();
  526. this.GrowHeight.wrapper.style.overflow = "hidden";
  527. }
  528. function leave(event) {
  529. this.GrowHeight.hide();
  530. }
  531. this.wrapper.addEventListener("mouseenter", enter);
  532. this.wrapper.addEventListener("mouseleave", leave);
  533. }
  534. GrowHeight.prototype.getCmsBtn = function (mode) {
  535. if (this.fj == null || (mode == "play" && (this.fj.value == null || this.fj.value == "")))
  536. return;
  537. if(mode=="edit"&&this.fj.value == ""){
  538. this.fj.jlztm=1;
  539. }
  540. var fj = this.fj;
  541. var attachment = document.createElement("span");
  542. var attachmentText = document.createTextNode("附件");
  543. attachment.appendChild(attachmentText);
  544. this.wrapper.insertBefore(attachment, this.container.nextSibling);
  545. attachment.className = 'icon-attachment builtinButton';
  546. //上浮。找到目标的样式修改当前的样式名
  547. if ($(attachment).parents("abc").length > 0) {
  548. attachment.className = "123456789";
  549. }
  550. var fjidHidden = document.querySelector("[name='" + fj.key + "']");
  551. if (!fjidHidden) {
  552. fjidHidden = document.createElement("input");
  553. fjidHidden.type = "hidden";
  554. fjidHidden.name = fj.key;
  555. fjidHidden.value = fj.value;
  556. attachment.style.display = "none";
  557. attachment.parentNode.insertBefore(fjidHidden, attachment);
  558. }
  559. var editor = attachment.editor = this.container;
  560. var oriTop = $MT(attachment),
  561. oriBottom = $MB(attachment),
  562. oriLeft = $ML(attachment);
  563. attachment.updateButtonSize = function () {
  564. var editor = attachment.editor;
  565. editor.style.resize = "none";
  566. editor.style.width = $INNERWIDTH(editor.parentNode) - $ML(editor) - $MR(editor) - ($ISBOX(editor) ? 0 : ($BOXWIDTH(editor))) - $REALWIDTH(attachment) - $ML(attachment) - $MR(attachment) + "px";
  567. var editor = attachment.editor;
  568. attachment.style.display = "none";
  569. attachment.style.height = $INNERHEIGHT(attachment.parentNode) - oriTop - oriBottom + "px";
  570. attachment.style.verticalAlign = "top";
  571. attachment.style.lineHeight = $INNERHEIGHT(attachment.parentNode) - oriTop - oriBottom + 22 + "px";
  572. attachment.style.display = "inline-block";
  573. attachment.style.position = '';
  574. if ($DSP(editor).indexOf("inline") > -1) { //行内元素
  575. } else { //块元素,浮动在他身边
  576. editor.style.display = "inline-block";
  577. }
  578. }
  579. //编辑框去掉附件按钮的宽度
  580. function updateCount() {
  581. var fjidInput = document.getElementsByName(fj.key)[0];
  582. if (fjidInput == null)
  583. return;
  584. var fjid = fjidInput.value;
  585. if (fjid != null && fjid != "")
  586. $.ajax({
  587. type: "post",
  588. url: "/service?ssServ=wrCcmsList", // =getSubNrCount",。Lin
  589. data: {
  590. nrid: "T-" + fjid,
  591. jlztm: fj.jlztm
  592. },
  593. dataType: "json", // 增加,统一 Ajax 返回标准 -- .ssCode、.ssMsg、.ssData。Lin
  594. success: function (data) {
  595. /* 改,增加错误处理 -- 统一 Ajax 返回标准 -- .ssCode、.ssMsg、.ssData。Lin
  596. * 去掉 JSON.parse(,改为用 Map<String, Object> 传输
  597. var names = JSON.parse(data);
  598. */
  599. if (data.ssCode != 0) {
  600. alert(data.ssMsg);
  601. return;
  602. }
  603. var names = data.ssData;
  604. var count = names.length;
  605. //if (count > 0) {
  606. var num = attachment.querySelector(".icon-num");
  607. if (!num) {
  608. num = document.createElement("span");
  609. num.className = "icon-num";
  610. num.style.float = "right";
  611. num.style.position = "absolute";
  612. num.style.right = "2px";
  613. num.style.top = "2px";
  614. attachment.appendChild(num);
  615. }
  616. num.innerText = count;
  617. if (count == 0) {
  618. $(num).hide();
  619. } else {
  620. $(num).show();
  621. }
  622. var datas = [];
  623. for (var iii = 0; iii < names.length; iii++) {
  624. datas.push({
  625. name: iii,
  626. value: names[iii]
  627. })
  628. }
  629. $(attachment).off("mouseenter").on("mouseenter", function () {
  630. var menuelement = wd.display.initCmsMenu("cms" + fjid, fj.jlztm, datas);
  631. if (menuelement) {
  632. menuelement.showAt(attachment);
  633. $(attachment).off("mouseleave").on("mouseleave", function () {
  634. menuelement.hide();
  635. });
  636. }
  637. });
  638. },
  639. error: function (data) {
  640. console.log(data);
  641. }
  642. });
  643. };
  644. if ("play" == mode) {
  645. attachment.addEventListener("click", function (e) {
  646. var fn = window[fj.key + "fjPlay"];
  647. fn && fn.call(this);
  648. e.stopPropagation()
  649. });
  650. } else if ("edit" == mode) {
  651. var fnName = fj.key + "CallbackName";
  652. var fnName = window[fnName] = fj.key + "mouseover";
  653. window[fnName] = updateCount;
  654. attachment.addEventListener("mouseenter", window[fnName]);
  655. attachment.addEventListener("click", function (e) {
  656. var fn = window[fj.key + "fjEdit"];
  657. fn && fn.call(this);
  658. e.stopPropagation()
  659. });
  660. }
  661. attachment.updateButtonSize();
  662. editor.initDot && editor.initDot();
  663. updateCount();
  664. return attachment;
  665. }
  666. GrowHeight.prototype.getElement = function () {
  667. return this.wrapper;
  668. }
  669. GrowHeight.prototype.init = function () {
  670. if (!this.inited) {
  671. var styleAttrNames = {
  672. "width": "width",
  673. "height": "height"
  674. };
  675. for (var key in styleAttrNames) {
  676. var attrV = this.srcElement.getAttribute(key);
  677. if (attrV != null) {
  678. var styleName = styleAttrNames[key];
  679. this.srcElement.style[styleName] = attrV;
  680. }
  681. this.srcElement.removeAttribute(key);
  682. }
  683. var h = $REALHEIGHT(this.srcElement);
  684. var lh = $LH(this.srcElement);
  685. this.minHeight = Math.max(lh || 28, h);
  686. this.oriWidth = $REALWIDTH(this.srcElement);
  687. this.wrapper = document.createElement("div");
  688. this.wrapper.style.cssText = this.srcElement.style.cssText;
  689. this.wrapper.style.minHeight = this.minHeight + "px";
  690. this.wrapper.className = this.srcElement.className;
  691. this.srcElement.GrowHeight = this;
  692. this.srcElement.className = "";
  693. this.srcElement.style.width = "";
  694. this.srcElement.style.maxWidth = "";
  695. this.srcElement.style.cssText = "";
  696. this.srcElement.parentNode.insertBefore(this.wrapper, this.srcElement);
  697. this.wrapper.appendChild(this.srcElement);
  698. this.wrapper.GrowHeight = this;
  699. this.wrapper.setAttribute("growHeight", this.name);
  700. this.id = this.srcElement.id;
  701. switch (this.type) {
  702. case 0:
  703. this.wrapper.className += " input-div";
  704. this.initAsInput();
  705. this.cmsBtn = this.getCmsBtn("edit");
  706. break;
  707. case 1:
  708. this.wrapper.className += " input-div";
  709. this.initAsDiv();
  710. this.cmsBtn = this.getCmsBtn("edit");
  711. break;
  712. case 2:
  713. this.initPlayHtml();
  714. this.cmsBtn = this.getCmsBtn("play");
  715. break;
  716. case 3:
  717. default:
  718. this.initPlay();
  719. this.cmsBtn = this.getCmsBtn("play");
  720. break;
  721. }
  722. if (this.container) {
  723. var that = this;
  724. this.container.id = this.id;
  725. this.container.className += " smallScrollbar";
  726. var passiveSupported = false;
  727. try {
  728. var options = Object.defineProperty({}, "passive", {
  729. get: function () {
  730. passiveSupported = true;
  731. }
  732. });
  733. window.addEventListener("test", null, options);
  734. } catch (err) {}
  735. this.container.addEventListener("mousewheel", function (e) {
  736. that.lastScrollTop = that.container.scrollTop;
  737. }, passiveSupported ? {
  738. passive: true
  739. }
  740. : false);
  741. this.container.addEventListener('paste', function (e) {
  742. console.log("paste")
  743. if (!this.GrowHeight.acceptEnter) {
  744. var vvv = (e.clipboardData || window.clipboardData).getData('text/plain').replace(/\n/g, "");
  745. this.focus();
  746. if (document.all) {
  747. var r = document.selection.createRange();
  748. document.selection.empty();
  749. r.text = vvv;
  750. r.collapse();
  751. r.select();
  752. } else {
  753. var newstart = this.selectionStart + vvv.length;
  754. var pfx = this.value.substr(0, this.selectionStart);
  755. var sfx = this.value.substring(this.selectionEnd);
  756. this.value = pfx + vvv + sfx;
  757. this.selectionStart = newstart;
  758. this.selectionEnd = newstart;
  759. }
  760. e.preventDefault();
  761. e.stopPropagation();
  762. return false;
  763. this.GrowHeight.onInput();
  764. }
  765. });
  766. }
  767. this.inited = true;
  768. this.hide();
  769. }
  770. // loadSmallScorll($(this.wrapper).find(".smallScrollbar")); // 初始化伪滚动条
  771. }
  772. function $ISBOX(element) {
  773. return $CSS(element, "box-sizing") == "border-box";
  774. }
  775. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  776. function $PT(element) {
  777. $ISBOX(element);
  778. return $NUMOFCSS(element, "padding-top", 0);
  779. }
  780. function $PR(element) {
  781. return $NUMOFCSS(element, "padding-right", 0);
  782. }
  783. function $PB(element) {
  784. return $NUMOFCSS(element, "padding-bottom", 0);
  785. }
  786. function $PL(element) {
  787. return $NUMOFCSS(element, "padding-left", 0);
  788. }
  789. function $CSS(element, name) {
  790. if (typeof(window.getComputedStyle) == 'undefined') {
  791. return element.currentStyle[name];
  792. }
  793. return window.getComputedStyle(element)[name];
  794. }
  795. function $DSP(element) {
  796. return $CSS(element, "display");
  797. }
  798. function $NUMOFCSS(element, name, def) {
  799. var v = parseFloat($CSS(element, name));
  800. v = isNaN(v) ? def : v;
  801. return v;
  802. }
  803. function $W(element) {
  804. return $NUMOFCSS(element, "width");
  805. }
  806. function $H(element) {
  807. return $NUMOFCSS(element, "height");
  808. }
  809. function $R(element) {
  810. return $NUMOFCSS(element, "right");
  811. }
  812. function $MAH(element) {
  813. return $NUMOFCSS(element, "max-height");
  814. }
  815. function $MIH(element) {
  816. return $NUMOFCSS(element, "min-height");
  817. }
  818. function $MT(element) {
  819. return $NUMOFCSS(element, "margin-top", 0);
  820. }
  821. function $MB(element) {
  822. return $NUMOFCSS(element, "margin-bottom", 0);
  823. }
  824. function $ML(element) {
  825. return $NUMOFCSS(element, "margin-left", 0);
  826. }
  827. function $MR(element) {
  828. return $NUMOFCSS(element, "margin-right", 0);
  829. }
  830. function $BL(element) {
  831. return $NUMOFCSS(element, "border-left", 0);
  832. }
  833. function $BR(element) {
  834. return $NUMOFCSS(element, "border-right", 0);
  835. }
  836. function $BOXWIDTH(element) {
  837. return $PL(element) + $PR(element) + $BL(element) + $BR(element);
  838. }
  839. function $BOXHEIGHT(element) {
  840. return $PT(element) + $PB(element) + $BT(element) + $BB(element);
  841. }
  842. function $MAWS(element, def) {
  843. var va = $CSS(element, "max-width");
  844. va = isNaN(parseFloat(va)) ? $CSS(element, "width") : va;
  845. return isNaN(parseFloat(va)) ? def : va;
  846. }
  847. function $REALHEIGHT(element) {
  848. var b = $ISBOX(element);
  849. var height = $NUMOFCSS(element, "height");
  850. if (height == null || typeof(height) == 'undefined') {
  851. //兼容IE8
  852. console.log("兼容IE8");
  853. height = 35;
  854. }
  855. if (!b) {
  856. height += $BOXHEIGHT(element);
  857. }
  858. return height;
  859. }
  860. function $REALWIDTH(element) {
  861. var b = $ISBOX(element);
  862. var width = $NUMOFCSS(element, "width");
  863. if (!b) {
  864. width += $BOXWIDTH(element);
  865. }
  866. return width;
  867. }
  868. function $INNERWIDTH(element) {
  869. var b = $ISBOX(element),
  870. width = $NUMOFCSS(element, "width");
  871. if (b) {
  872. width -= $BOXWIDTH(element);
  873. }
  874. return width;
  875. }
  876. function $INNERHEIGHT(element) {
  877. var b = $ISBOX(element),
  878. height = $NUMOFCSS(element, "height");
  879. if (height == null || typeof(height) == 'undefined') {
  880. //兼容IE8
  881. height = 38
  882. }
  883. if (b) {
  884. height -= $BOXHEIGHT(element);
  885. }
  886. return height;
  887. }
  888. function $LH(element, default_) {
  889. return $NUMOFCSS(element, "line-height", default_);
  890. }
  891. function $HIDDENOVERFLOW(element) {
  892. var h = [];
  893. var a = [];
  894. var p = element;
  895. while (p.parentNode && p.parentNode.nodeType == 1) {
  896. p = p.parentNode;
  897. if (p.parentNode.nodeType != 1)
  898. break;
  899. var ofy = $CSS(p, "overflow-y");
  900. if ((ofy.indexOf("auto") > -1 || ofy.indexOf("scroll") > -1) && p.scrollHeight != p.clientHeight) {
  901. //滚动高度等于元素高度的就算了
  902. a.push(p);
  903. } else if (ofy == "hidden") {
  904. h.push(p);
  905. var eb = element.getBoundingClientRect();
  906. var pb = p.getBoundingClientRect();
  907. var ph = $height100(p),
  908. eh = $maxHeight100(element) - pb.top + eb.top;
  909. if (ph < eh) {
  910. return p;
  911. }
  912. }
  913. }
  914. p = h.length > 0 ? h[0] : (a.length > 0 ? a[0] : document.body.parentNode);
  915. return p;
  916. }
  917. function $BT(element) {
  918. return $NUMOFCSS(element, "border-top", 0);
  919. }
  920. function $BB(element) {
  921. return $NUMOFCSS(element, "border-bottom", 0);
  922. }
  923. function debugLog() {
  924. event && event.ctrlKey && console.trace.apply(this, arguments);
  925. }
  926. function $innerHeight(element, height) {
  927. var p = element.parentNode;
  928. if ($ISBOX(p)) { //父级是box,要减掉padding,border
  929. height -= $BOXHEIGHT(p);
  930. }
  931. return height;
  932. }
  933. function $height100(element) {
  934. return $innerHeight(element, $INNERHEIGHT(element.parentNode));
  935. }
  936. function $minHeight100(element) {
  937. return $innerHeight(element, element.GrowHeight.minHeight);
  938. }
  939. function $maxHeight100(element) {
  940. return $innerHeight(element, $INNERHEIGHT(element.parentNode));
  941. }
  942. function $scrollHeight100(element) {
  943. return $innerHeight(element, element.scrollHeight);
  944. }