custom_new.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /**
  2. * 浏览器动态化效果及事件类
  3. *
  4. *
  5. * @author xiongbing
  6. * @date 2013-12-18
  7. * @version 1.0
  8. *
  9. */
  10. //-----------------------------------------------------------------------------
  11. /*判断浏览器*/
  12. var isIE = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
  13. var $J = jQuery.noConflict();
  14. //-----------------------------------------------------------------------------
  15. $J(function() {
  16. /**
  17. * 网站页面对象
  18. *
  19. * @class Page
  20. * @constructor
  21. * @param {DOMDocument}document
  22. */
  23. var Page = function(elem) {
  24. /**
  25. * 当前对象
  26. *
  27. * @property elem
  28. * @private
  29. */
  30. this.elem = elem;
  31. this.common=Page.common;
  32. };
  33. Page.common = {
  34. /**
  35. * 去除字符串头尾空白字符
  36. *
  37. * @method trim
  38. * @static
  39. * @param {String}str
  40. * @return {String}
  41. */
  42. trim : function(str) {
  43. return str.replace(/^\s*/, "").replace(/\s*$/, "");
  44. },
  45. isFunction : function(obj) {
  46. return Object.prototype.toString.call(obj) === "[object Function]";
  47. },
  48. /**
  49. * 获取一个元素相对于页面左上角的位置
  50. * @param elem
  51. * @return object {x:, y: }
  52. */
  53. findPos : function(elem) {
  54. var offset = {
  55. x:0,
  56. y:0
  57. };
  58. if (!!document.documentElement.getBoundingClientRect()) {//支持此方法的浏览器可以直接获取位置
  59. offset.x = elem.getBoundingClientRect().left + this.scroll().left;
  60. offset.y = elem.getBoundingClientRect().top + this.scroll().top;
  61. } else {
  62. while (elem) {
  63. offset.x += elem.offsetLeft;
  64. offset.y += elem.offsetTop;
  65. elem = elem.offsetParent;
  66. }
  67. }
  68. return offset;
  69. },
  70. /**
  71. * 返回数组或对象中第一个值为 val 对象的 index 或 key
  72. * @param obj 对象或数组
  73. * @param val 待检测值
  74. */
  75. indexOf : function indexOf(obj, val) {
  76. if (obj.indexOf) {
  77. return obj.indexOf(val);
  78. } else {
  79. var result = -1;
  80. this.each(obj, function(idx) {
  81. if (this[idx] === val) {
  82. result = idx;
  83. return false;
  84. }
  85. });
  86. return result;
  87. }
  88. },
  89. each : function (object, callback, context) {
  90. if (object === undefined || object === null) {
  91. return;
  92. }
  93. if (object.length === undefined || this.isFunction(object)) {
  94. for (var name in object) {
  95. if (object.hasOwnProperty(name)) { //只有此元素本身属性调用回调
  96. if (callback.call(context || object[name], name, object[name]) === false) {
  97. break;
  98. }
  99. }
  100. }
  101. } else {
  102. for (var i = 0; i < object.length; i++) {
  103. if (callback.call(context || object, i, object[i]) === false) {
  104. break;
  105. }
  106. }
  107. }
  108. return object;
  109. },
  110. /**
  111. * 设置或获取元素样式
  112. *
  113. * @method css
  114. * @static
  115. * @param {Object}elem
  116. * @param {Object}styles
  117. * @return {Object}
  118. */
  119. css : function() {
  120. /**
  121. * 获取一个元素的样式
  122. * @param elem
  123. * @param styleName
  124. */
  125. var getStyle = function(elem, styleName) {
  126. var value = '';
  127. if (styleName == 'float') {
  128. document.defaultView ? styleName = 'float'/*cssFloat*/ : styleName = 'styleFloat';
  129. }
  130. if (elem.style[styleName]) {//内联样式
  131. value = elem.style[styleName];
  132. } else if (elem.currentStyle) {//IE
  133. value = elem.currentStyle[styleName];
  134. } else if (document.defaultView && document.defaultView.getComputedStyle) {//W3 DOM
  135. styleName = styleName.replace(/([A-Z])/g, '-$1').toLowerCase();
  136. var s = document.defaultView.getComputedStyle(elem, '');
  137. value = s && s.getPropertyValue(styleName);
  138. } else { //other,for example, Safari
  139. value = null;
  140. }
  141. //处理width 和 height 出现 auto 或是百分比宽度 的情况
  142. if ((value == "auto" || value.indexOf('%') !== -1) && ('width' === styleName.toLowerCase() || 'height' === styleName.toLowerCase()) && elem.style.display != "none" && value.indexOf('%') !== -1) {
  143. value = elem["offset" + styleName.charAt(0).toUpperCase() + styleName.substring(1).toLowerCase()] + "px";
  144. }
  145. if (styleName == "opacity") {
  146. try {
  147. value = elem.filters['DXImageTransform.Microsoft.Alpha'].opacity;
  148. value = value / 100;
  149. } catch(e) {
  150. try {
  151. value = elem.filters('alpha').opacity;
  152. } catch(err) {
  153. }
  154. }
  155. }
  156. return value;
  157. };
  158. return function(elem, styles) {
  159. if (typeof styles === 'string') {
  160. return getStyle(elem, styles);
  161. } else {
  162. this.each(styles, function(key, value) {
  163. elem.style[key] = value;
  164. });
  165. }
  166. };
  167. }(),
  168. scroll:function() {
  169. return {
  170. left : document.documentElement.scrollLeft + document.body.scrollLeft,
  171. top : document.documentElement.scrollTop + document.body.scrollTop
  172. }
  173. }
  174. };
  175. Page.prototype={
  176. /**
  177. *判断是否存在额外的标识
  178. *优先级为id/class/tagname依次下降
  179. *①如果id存在并可以确定唯一arr[0]='{id}',否则arr[0]='false'
  180. *②如果①不成立,class存在并可以确定唯一arr[1]='{class}',否则arr[1]='false'
  181. *③如果②不成立,arr[2]='{tagname[index]}'
  182. *
  183. *@param ele 传入节点对象
  184. *@return arr[] 返回数组
  185. */
  186. 'getMark':function (ele){
  187. var html = $J(ele).html();
  188. var arr = ['false','false','false'];
  189. var isBreak=false;
  190. //查看是否存在id,并判断是否可以唯一定位
  191. if(!isBreak){
  192. var id=$J(ele).attr("id");
  193. if(id!=null&&id!=''&&id!=undefined){
  194. if(html==$J('#'+id).html()){
  195. arr[0]='#'+id;
  196. isBreak=true;
  197. }
  198. }
  199. }
  200. //查看是否存在class,并判断是否可以唯一定位
  201. if(!isBreak){
  202. var css=$J(ele).attr("class");
  203. if(css!=null&&css!=''&&css!=undefined&&css.indexOf(' ')==-1){
  204. if(html==$J('.'+css).html()){
  205. arr[1]='.'+css;
  206. isBreak=true;
  207. }
  208. }
  209. }
  210. //前两个条件都不符合则返回该元素标签及所在节点级别中的索引
  211. if(!isBreak){
  212. var tagName=ele.tagName;
  213. var curObj = $J(ele);
  214. var preObj = curObj.prevAll();
  215. var curIndex = preObj.filter(tagName).length;
  216. var prevIndex=0;
  217. var domIndex=0;
  218. preObj.each(function(){
  219. prevIndex=prevIndex+$J(this).find(tagName).length;
  220. });
  221. domIndex=curIndex +prevIndex;
  222. arr[2]=tagName.toLowerCase()+':eq('+domIndex+')';
  223. }
  224. return arr;
  225. },
  226. /**
  227. *获取选定对象所在dom中的符合jquery选择器的路径
  228. *
  229. *@param ele 传入节点对象
  230. *@param nodepath 节点路径,初始为''
  231. *@return str 返回节点路径
  232. */
  233. 'getPath':function (ele,nodepath){
  234. var tagName = ele.tagName;
  235. if(tagName!=undefined&&tagName.toLowerCase!='html'&&tagName.toLowerCase!='body'){
  236. var arr = this.getMark(ele);
  237. var mark = false;
  238. var currnpath='';
  239. var id=arr[0];
  240. var css=arr[1];
  241. var tName=arr[2];
  242. if(id!='false'){
  243. currnpath=id;
  244. mark=true;
  245. }
  246. if(!mark&&css!='false'){
  247. currnpath=css;
  248. mark=true;
  249. }
  250. if(!mark){
  251. currnpath=tName;
  252. }
  253. nodepath = currnpath+' '+nodepath;
  254. if(!mark){
  255. var pars = $J(ele).parent();
  256. nodepath=this.getPath(pars[0],nodepath);
  257. }else{
  258. return nodepath;
  259. }
  260. }
  261. return nodepath;
  262. },
  263. /**
  264. *获取选定对象所在dom中的上下节点路径
  265. *
  266. *@param ele 传入节点对象
  267. *@return str 返回本节点为中心的上下路径
  268. */
  269. 'relation':function (ele){
  270. var nodepath='';
  271. var currTagName = ele.tagName.toLowerCase();
  272. var parsTag = $J(ele).parent();
  273. var childTag = $J(ele).children();
  274. if(parsTag!=null&&parsTag!=undefined&&parsTag.length>0){
  275. var qppath = this.getPath(parsTag[0],'');
  276. //nodepath = "<a href='javascript:resize(\""+qppath+"\");' style='color:blue' title='父节点'>"+parsTag[0].tagName.toLowerCase() + "</a>.";
  277. nodepath = "<em n='"+qppath+"' style='CURSOR: pointer;color:blue' title='父节点'>"+parsTag[0].tagName.toLowerCase() + "</em>.";
  278. }
  279. var qcpath = this.getPath(ele,'');
  280. nodepath = nodepath + "<em n='"+qcpath+"' style='CURSOR: pointer;color:red' title='当前选中节点'>" + currTagName + "</em>";
  281. //nodepath = nodepath + "<em style='color:red' title='当前选中节点'>" + currTagName + "</em>";
  282. if(childTag!=null&&childTag!=undefined&&childTag.length>0){
  283. var qcnpath = this.getPath(childTag[0],'');
  284. //nodepath = nodepath + ".<a href='javascript:resize(\""+qcnpath+"\");' style='color:blue' title='子节点'>" + childTag[0].tagName.toLowerCase() + "</a>";
  285. nodepath = nodepath + ".<em n='"+qcnpath+"' style='CURSOR: pointer;color:blue' title='子节点'>" + childTag[0].tagName.toLowerCase() + "</em>";
  286. }
  287. return nodepath;
  288. },
  289. 'resize':function(ele) {
  290. $J(ele).trigger("mouseover");
  291. },
  292. 'creatDiv' : function(id,width,height,left,top,html,cssText){
  293. var curr = this;
  294. menuobj=this.elem;
  295. var div = document.createElement('div');
  296. div.id = id;
  297. if(!cssText){
  298. var cssText = 'position:absolute;filter:alpha(opacity=90);background-color:#ffce73;opacity:0.9;z-index:100;text-align:center; font-weight:bold;line-height:18px;font-size:16px;';
  299. }
  300. cssText += 'height:'+height+'px;';
  301. cssText += 'width:'+width+'px;';
  302. cssText += 'left:'+left+'px;';
  303. cssText += 'top:'+top+'px;';
  304. div.style.cssText = cssText;
  305. div.innerHTML=html;
  306. /*if(html==''){
  307. //添加双击事件及右键菜单
  308. $J(div).dblclick(function(event){dbclick(curr.elem,event);});
  309. $J(div).contextMenu(menu1,{theme:'vista'});
  310. }*/
  311. return div;
  312. },
  313. 'removeDiv': function (id){
  314. var div = document.getElementById(id);
  315. if(div){
  316. document.body.removeChild(div);
  317. }
  318. },
  319. 'removeClipDiv':function(){
  320. for (var i = 0 ; i < 5 ; i++){
  321. this.removeDiv('yShade' + i);
  322. }
  323. this.shadeStatu = false;
  324. },
  325. 'createClipDiv':function(color){
  326. if(this){
  327. var main = this;
  328. //对象的左上角坐标
  329. var y = Math.abs(main.common.findPos(main.elem).y);
  330. var x = Math.abs(main.common.findPos(main.elem).x);
  331. //对象的宽高
  332. var mwidth = main.elem.scrollWidth;
  333. var mheight = main.elem.scrollHeight;
  334. //当前电脑页面宽高
  335. var dwidth = document.documentElement.scrollWidth;
  336. var dheight = document.documentElement.scrollHeight;
  337. this.removeClipDiv();
  338. var shadeArr = [];
  339. //body不100%框的布尔状态
  340. var isFullWidth = (document.body.scrollWidth == document.body.offsetWidth);
  341. /*
  342. var blueText = 'position:absolute;border:5px solid rgb(0, 154, 226);border:5px solid rgba(0, 154, 226,1.0);-webkit-border-radius:5px;-moz-border-radius:5px;-khtml-border-radius:5px;z-index:100;';
  343. var redText = 'position:absolute;border:5px solid rgb(208, 28, 29);border:5px solid rgba(208, 28, 29,1.0);-webkit-border-radius:5px;-moz-border-radius:5px;-khtml-border-radius:5px;z-index:100;';
  344. */
  345. var blueText = 'position:absolute; background-color:rgb(89, 207, 248); background-color:rgba(89, 207, 248,0.7);z-index:100;font-size:5px;';
  346. var redText = 'position:absolute;background-color:rgb(252, 113, 123);background-color:rgba(252, 113, 123,0.7);z-index:100;font-size:5px;';
  347. var cssText = blueText;
  348. if(color=='red'){
  349. cssText = redText;
  350. }
  351. var _leftTemp = (document.body.offsetWidth - document.documentElement.scrollWidth)/2 ;
  352. //var html=main.elem.tagName;
  353. //var html=this.getPath(main.elem,'');//alert(html);
  354. var html=this.relation(main.elem);
  355. var xTips=x-5;
  356. var yTips=y-21;
  357. if(y==0){
  358. yTips=y+mheight+5;
  359. }
  360. shadeArr[0] = this.creatDiv('yShade0',mwidth+10,16,xTips,yTips,html);
  361. //shadeArr[1] = this.creatDiv('yShade1',mwidth,mheight,x-5+_leftTemp,y-5,'',cssText);
  362. shadeArr[1] = this.creatDiv('yShade1',mwidth+10,5,x-5,y-5,'',cssText);
  363. shadeArr[2] = this.creatDiv('yShade2',mwidth+10,5,x-5,y+mheight,'',cssText);
  364. shadeArr[3] = this.creatDiv('yShade3',5,mheight,x-5,y,'',cssText);
  365. shadeArr[4] = this.creatDiv('yShade4',5,mheight,mwidth+x,y,'',cssText);
  366. for (var i = 0,count = shadeArr.length ; i < count ; i++){
  367. document.body.appendChild(shadeArr[i]);
  368. }
  369. }
  370. this.shadeStatu = true;
  371. }
  372. };
  373. //-----------------------------------------------------------------------------
  374. /*节点路径中的单击事件*/
  375. function resize(nodepath) {
  376. $J(nodepath).trigger("mouseover");
  377. }
  378. //-----------------------------------------------------------------------------
  379. /*双击事件*/
  380. function dbclick(nodepath){
  381. var elem = $J(nodepath)[0];
  382. var tagName = elem.tagName;
  383. var objid = $J(elem).attr("objid");
  384. if(objid!=undefined){
  385. var tagType = "span";
  386. if(tagName.toLowerCase() == "span"){
  387. tagType = "span";
  388. }else if(tagName.toLowerCase() == "a"){
  389. tagType = "alink";
  390. }else if(tagName.toLowerCase() == "table" || tagName.toLowerCase() == "div" || tagName.toLowerCase() == "ul" || tagName.toLowerCase() == "dl" ){
  391. tagType = "list";
  392. }else if(tagName.toLowerCase() == "img"){
  393. tagType = "pic&w="+this.width+"&h="+this.height;
  394. }else{
  395. return false;
  396. }
  397. window.parent.showDialog('p_setproperty','选择标签',"setparam/p_setproperty.jsp?objid="+objid+"&type="+tagType,902,565);
  398. //openFile1("setparam/p_setproperty.jsp?objid="+objid+"&type="+tagType,"615","1002");
  399. }
  400. //event.stopPropagation();
  401. }
  402. //-----------------------------------------------------------------------------
  403. function bindClick(){
  404. //定义setTimeout执行方法
  405. var TimeFn = null;
  406. $J("[n]").click(function () {
  407. var n='';
  408. n = $J(this).attr("n");
  409. // 取消上次延时未执行的方法
  410. clearTimeout(TimeFn);
  411. //执行延时
  412. TimeFn = setTimeout(function(){
  413. //do function在此处写单击事件要执行的代码
  414. resize(n);
  415. },300);
  416. });
  417. $J("[n]").dblclick(function () {
  418. // 取消上次延时未执行的方法
  419. clearTimeout(TimeFn);
  420. //双击事件的执行代码
  421. dbclick($J(this).attr("n"));
  422. });
  423. }
  424. //-----------------------------------------------------------------------------
  425. /*取消A链接*/
  426. $J("a").click(function() {
  427. return false;
  428. });
  429. //-----------------------------------------------------------------------------
  430. /*鼠标悬停区域效果*/
  431. //$J("table").add("div").add("ul").add("span").add("a").add("font").add("img").bind("mouseover",function(event){
  432. $J("table").add("div").add("span").add("font").add("a").add("dd").add("dt").add("img").bind("mouseover",function(event){
  433. event.stopPropagation();
  434. var obj = this;
  435. var tagName = this.tagName;
  436. var objid = $J(this).attr("objid");
  437. if($J(obj).attr("cmstag") && $J(obj).attr("cmstag")!=""){
  438. var page1=new Page(obj);
  439. page1.removeClipDiv();
  440. page1.createClipDiv('red');
  441. }/*else if(1==1){
  442. }*/else{
  443. var page1=new Page(obj);
  444. page1.removeClipDiv();
  445. page1.createClipDiv('blue');
  446. }
  447. bindClick();
  448. event.stopPropagation();
  449. });
  450. //-----------------------------------------------------------------------------
  451. /*双击事件*/
  452. //$J("table").add("div").add("ul").add("span").add("a").add("dd").add("dt").add("img").dblclick(function(event){
  453. $J("table").add("div").add("span").add("font").add("a").add("dd").add("dt").add("img").dblclick(function(event){
  454. var obj = this;
  455. var tagName = this.tagName;
  456. var objid = $J(this).attr("objid");
  457. //var _workbench=$("#workbench",document.body);
  458. //var parName=_workbench.find("[objid='"+objid+"']").parent(".selected");
  459. //alert(parName);
  460. /*console.info(obj);
  461. console.info(obj.closest("dd"));
  462. var dd = obj.closest("dd"); */
  463. //console.info(obj.closest("dd").parent().closest("div").find("a"));
  464. //alert(_workbench.find("[objid='"+objid+"']").closest("li"));
  465. /*if(partagName.indexOf("li")>=0||){
  466. }*/
  467. if(objid!=undefined){
  468. /*var tagType = "span";
  469. if(tagName.toLowerCase() == "a"){
  470. tagType = "alink";
  471. }else if(tagName.toLowerCase() == "table" || tagName.toLowerCase() == "ul" || tagName.toLowerCase() == "dl" ){
  472. tagType = "list";
  473. }else if(tagName.toLowerCase() == "div"){
  474. tagType = "content";
  475. }else if(tagName.toLowerCase() == "img"){
  476. tagType = "pic&w="+this.width+"&h="+this.height;
  477. }*/
  478. var tagType=tagName.toLowerCase();
  479. var objid = $J(this).attr("objid");
  480. window.parent.showDialog('p_setproperty','选择标签',"/myconsole/dynamic/selectLabel?objID="+objid+"&labelType="+tagType,900,565);
  481. //openFile1("setparam/p_setproperty.jsp?objid="+objid+"&type="+tagType,"615","1002");
  482. }
  483. event.stopPropagation();
  484. });
  485. //-----------------------------------------------------------------------------
  486. /*添加菜单*/
  487. function setImg(obj){
  488. var objid = $J(obj).attr("objid");
  489. window.parent.showDialog('p_setproperty','选择标签',"setparam/p_setproperty.jsp?objid="+objid+"&type=pic&w="+obj.width+"&h="+obj.height,902,565);
  490. //openFile1("setparam/p_setproperty.jsp?objid="+objid+"&type=pic&w="+obj.width+"&h="+obj.height,"615","1002");
  491. }
  492. function editHTML(obj){
  493. var objid = $J(obj).attr("objid");
  494. openFile1("p_fileedit_part.jsp?objid="+objid,"680","980");
  495. }
  496. function clearHTML(obj){
  497. var objid = $J(obj).attr("objid");
  498. window.open("setparam/p_clearparam.jsp?objid="+objid);
  499. if(obj.tagName.toLowerCase()=="img"){
  500. var aTag = $J(obj).parent();
  501. if($J(aTag).attr("tagName").toLowerCase()=="a"&&$J(aTag).attr("tag")){
  502. if(confirm("是否需要清除外层链接动态化标签?")){
  503. window.open("setparam/p_clearparam.jsp?objid="+$J(aTag).attr("objid"));
  504. }
  505. }
  506. }
  507. }
  508. //-----------------------------------------------------------------------------
  509. });