growHeight.js 33 KB

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