MTooltips.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. var MTooltips = new Class({
  2. Extends: MWF.widget.Common,
  3. Implements: [Options, Events],
  4. options : {
  5. style : "", //如果有style,就加载 style/css.wcss
  6. axis: "y", //箭头在x轴还是y轴上展现
  7. position : { //node 固定的位置
  8. x : "auto", //x轴上left center right, auto 系统自动计算
  9. y : "auto" //y 轴上top middle bottom, auto 系统自动计算
  10. },
  11. priorityOfAuto :{
  12. x : [ "center", "right", "left" ], //当position x 为 auto 时候的优先级
  13. y : [ "middle", "bottom", "top" ] //当position y 为 auto 时候的优先级
  14. },
  15. offset : {
  16. x : 0,
  17. y : 0
  18. },
  19. isFitToContainer : true, //当position x 不为 auto, y 不为 auto 的时候,自动设置偏移量,使tooltip不超过容器的可见范围
  20. event : "mouseenter", //事件类型,有target 时有效, mouseenter对应mouseleave,click 对应 container 的 click
  21. hiddenDelay : 200, //ms , 有target 且 事件类型为 mouseenter 时有效
  22. displayDelay : 0, //ms , 有target 且事件类型为 mouseenter 时有效
  23. hasArrow : true,
  24. isAutoShow : true,
  25. isAutoHide : true,
  26. hasMask : true,
  27. hasCloseAction : false,
  28. hideByClickBody : false,
  29. overflow : "hidden" //弹出框高宽超过container的时候怎么处理,hidden 表示超过的隐藏,scroll 表示超过的时候显示滚动条
  30. },
  31. initialize : function( container, target, app, data, options, targetCoordinates ){
  32. //可以传入target 或者 targetCoordinates,两种选一
  33. //传入target,表示触发tooltip的节点,本类根据 this.options.event 自动绑定target的事件
  34. //传入targetCoordinates,表示 出发tooltip的位置,本类不绑定触发事件
  35. if( options ){
  36. this.setOptions(options);
  37. }
  38. this.container = container;
  39. this.target = target;
  40. this.targetCoordinates = targetCoordinates;
  41. this.app = app;
  42. if(app)this.lp = app.lp;
  43. this.data = data;
  44. this.path = "/x_component_Template/$MTooltips/";
  45. if( this.target ){
  46. this.setTargetEvents();
  47. }
  48. this.fireEvent("postInitialize",[this]);
  49. },
  50. setTargetEvents : function(){
  51. if( this.options.event == "click" ){
  52. if( this.options.isAutoShow ){
  53. this.targetClickFun = function( ev ){
  54. if( this.status === "display" ){
  55. this.hide();
  56. }else{
  57. this.load();
  58. }
  59. ev.stopPropagation();
  60. }.bind(this);
  61. this.target.addEvents({
  62. "mousedown" : function(ev){
  63. ev.stopPropagation();
  64. },
  65. "click": this.targetClickFun
  66. });
  67. }
  68. }else{
  69. if( this.options.isAutoHide || this.options.isAutoShow ){
  70. this.targetMouseenterFun = function(){
  71. if( this.timer_hide ){
  72. clearTimeout(this.timer_hide);
  73. }
  74. }.bind(this);
  75. this.target.addEvents({
  76. "mouseenter": this.targetMouseenterFun
  77. });
  78. }
  79. if( this.options.isAutoShow ){
  80. this.targetMouseenterFun2 = function(){
  81. if( this.status != "display" ){
  82. this.timer_show = setTimeout( this.load.bind(this),this.options.displayDelay );
  83. }
  84. }.bind(this);
  85. this.target.addEvents({
  86. "mouseenter": this.targetMouseenterFun2
  87. });
  88. }
  89. if( this.options.isAutoHide || this.options.isAutoShow ){
  90. this.targetMouseleaveFun = function(){
  91. if( this.timer_show ){
  92. clearTimeout(this.timer_show);
  93. }
  94. }.bind(this);
  95. this.target.addEvents({
  96. "mouseleave" : this.targetMouseleaveFun
  97. });
  98. }
  99. if( this.options.isAutoHide ){
  100. this.targetMouseleaveFun2 = function(){
  101. if( this.status == "display" ){
  102. this.timer_hide = setTimeout( this.hide.bind(this),this.options.hiddenDelay );
  103. }
  104. }.bind(this);
  105. this.target.addEvents({
  106. "mouseleave" : this.targetMouseleaveFun2
  107. });
  108. }
  109. }
  110. },
  111. load: function(){
  112. this.fireEvent("queryLoad",[this]);
  113. if( this.isEnable() ){
  114. if( this.node ){
  115. this.show();
  116. }else{
  117. this.create();
  118. }
  119. if( this.options.event == "click" ) {
  120. if( this.options.isAutoHide ){
  121. if( !this.options.hasMask ){
  122. this.containerMousedownFun = function(e){
  123. if( this.status === "display" ){
  124. this.hide();
  125. }
  126. e.stopPropagation();
  127. }.bind(this);
  128. this.container.addEvent("mousedown", this.containerMousedownFun )
  129. }
  130. }
  131. if( this.options.hideByClickBody ){
  132. this.bodyMousedownFun = function(e){
  133. if( this.status === "display" ){
  134. this.hide();
  135. }
  136. e.stopPropagation();
  137. }.bind(this);
  138. $(document.body).addEvent("mousedown", this.bodyMousedownFun )
  139. }
  140. }
  141. }
  142. this.fireEvent("postLoad",[this]);
  143. },
  144. hide: function(){
  145. if( this.node ){
  146. this.node.setStyle("display","none");
  147. this.status = "hidden";
  148. if( this.maskNode ){
  149. this.maskNode.setStyle("display","none");
  150. }
  151. if( this.containerMousedownFun ){
  152. this.container.removeEvent("mousedown", this.containerMousedownFun );
  153. this.containerMousedownFun = null;
  154. }
  155. this.fireEvent("hide",[this]);
  156. }
  157. },
  158. show: function(){
  159. this.status = "display";
  160. if( this.maskNode ){
  161. this.maskNode.setStyle("display","");
  162. }
  163. this.node.setStyle("display","");
  164. this.setCoondinates();
  165. this.fireEvent("show",[this]);
  166. },
  167. create: function(){
  168. this.status = "display";
  169. this.fireEvent("queryCreate",[this]);
  170. this.loadStyle();
  171. this.node = new Element("div.tooltipNode", {
  172. styles : this.nodeStyles
  173. }).inject( this.container );
  174. if( this.contentNode ){
  175. this.contentNode.inject( this.node );
  176. }else{
  177. this.contentNode = new Element("div",{
  178. styles : this.contentStyles
  179. }).inject( this.node );
  180. this.contentNode.set("html", this._getHtml() );
  181. }
  182. this._customNode( this.node, this.contentNode );
  183. if( this.options.hasArrow ){
  184. this.arrowNode = new Element("div.arrowNode", {
  185. "styles": this.arrowStyles
  186. }
  187. ).inject(this.node);
  188. }
  189. if( this.options.hasCloseAction ){
  190. this.closeActionNode = new Element("div", {
  191. styles : this.closeActionStyles,
  192. events : {
  193. click : function(){ this.hide() }.bind(this)
  194. }
  195. }).inject( this.node );
  196. }
  197. this._loadCustom( function(){
  198. this.setCoondinates();
  199. }.bind(this));
  200. if( this.options.event == "click" ) {
  201. if( this.options.isAutoHide ){
  202. if( this.options.hasMask ){
  203. this.maskNode = new Element("div.maskNode", {
  204. "styles": this.maskStyles,
  205. "events": {
  206. "mouseover": function (e) {
  207. e.stopPropagation();
  208. },
  209. "mouseout": function (e) {
  210. e.stopPropagation();
  211. },
  212. "click": function (e) {
  213. this.hide();
  214. e.stopPropagation();
  215. }.bind(this)
  216. }
  217. }).inject( this.container );
  218. }
  219. if( this.app ){
  220. this.hideFun_resize = this.hide.bind(this);
  221. this.app.addEvent( "resize" , this.hideFun_resize );
  222. }
  223. }
  224. }else{
  225. if( this.options.isAutoHide || this.options.isAutoShow ){
  226. this.node.addEvents({
  227. "mouseenter": function(){
  228. if( this.timer_hide )clearTimeout(this.timer_hide);
  229. }.bind(this)
  230. });
  231. }
  232. if( this.options.isAutoHide ){
  233. this.node.addEvents({
  234. "mouseleave" : function(){
  235. this.timer_hide = setTimeout( this.hide.bind(this),this.options.hiddenDelay );
  236. }.bind(this)
  237. });
  238. }
  239. }
  240. //this.target.addEvent( "mouseleave", function(){
  241. // this.timer_hide = setTimeout( this.hide.bind(this), this.options.HiddenDelay );
  242. //}.bind(this));
  243. this.fireEvent("postCreate",[this]);
  244. },
  245. loadStyle : function(){
  246. if( this.options.style ){
  247. this.cssPath = this.path+this.options.style+"/css.wcss";
  248. this._loadCss();
  249. }
  250. this.nodeStyles = {
  251. "font-size" : "12px",
  252. "position" : "absolute",
  253. "max-width" : "500px",
  254. "min-width" : "260px",
  255. "z-index" : "11",
  256. "background-color" : "#fff",
  257. "padding" : "10px",
  258. "border-radius" : "8px",
  259. "box-shadow": "0 0 18px 0 #999999",
  260. "-webkit-user-select": "text",
  261. "-moz-user-select": "text"
  262. };
  263. if( this.options.nodeStyles ){ //兼容之前在options里设置nodeStyles
  264. this.nodeStyles = Object.merge( this.nodeStyles, this.options.nodeStyles );
  265. }else if( this.css && this.css.nodeStyles ){
  266. this.nodeStyles = this.css.nodeStyles
  267. }
  268. if( this.css && this.css.contentStyles ){
  269. this.contentStyles = this.css.contentStyles;
  270. }else{
  271. this.contentStyles = {
  272. "width" : "100%",
  273. "height" : "100%"
  274. }
  275. }
  276. if( this.options.hasArrow ){
  277. if( this.css && this.css.arrowStyles ){
  278. this.arrowStyles = this.css.arrowStyles
  279. }else{
  280. this.arrowStyles = {
  281. "width": this.options.axis == "x" ? "9px" : "17px",
  282. "height" : this.options.axis == "x" ? "17px" : "9px",
  283. "position":"absolute",
  284. "background" : "no-repeat url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAAlCAYAAACgc9J8AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAP9JREFUeNq01oENhCAMBdCWuIgTuP8WOoGj9GhSDaeUfjiuCSEm+iKFVllESGNdVzrPk55xHIfewHnItm1MrVDMG/u+i0aeyWZp3R9CJRaBIWRB5YUHItAL80AEqmI1EIFcrAQ1rwjUxEoQgULsAue+2ayc/W83p5+z6RXggOO1WQGhrsFXP/Oip58tAaQTZ4QMbEbyEB2GCKEBdtpmru4NsC4aHg8PLJ/3lim2xDv02jYDz1kNQsGEQgiYeqAQBPuZQF1jFKqBYTn1RLK1D4+v3E3NGfgNkJlfdKi0XrUZgc1OWyt0Dwz/z37tGhA20s+WR4t+1iBbzTJyaD8CDAB7WgNSzh/AnwAAAABJRU5ErkJggg==)"
  285. }
  286. }
  287. }
  288. if( this.options.event == "click" && this.options.isAutoHide ) {
  289. if( this.css && this.css.maskStyles ){
  290. this.maskStyles = this.css.maskStyles;
  291. }else{
  292. this.maskStyles = {
  293. "width": "100%",
  294. "height": "100%",
  295. "opacity": 0,
  296. "position": "absolute",
  297. "background-color": "#fff",
  298. "top": "0px",
  299. "left": "0px"
  300. }
  301. }
  302. }
  303. if( this.options.hasCloseAction ){
  304. if( this.css && this.css.closeActionStyles ){
  305. this.closeActionStyles = this.css.closeActionStyles
  306. }else{
  307. this.closeActionStyles = {
  308. "width": "24px",
  309. "height" : "24px",
  310. "position" : "absolute",
  311. "top": "0px",
  312. "right" : "0px",
  313. "background": "url(/x_component_Template/$MTooltips/default/icon/off_gray.png) no-repeat center center",
  314. "cursor": "pointer"
  315. }
  316. }
  317. }
  318. },
  319. isEnable : function(){
  320. return !this.disable;
  321. },
  322. setCoondinates : function(){
  323. if( this.options.axis == "x" ){
  324. this.setCoondinates_x();
  325. }else{
  326. this.setCoondinates_y();
  327. }
  328. },
  329. setCoondinates_x : function(){
  330. var targetCoondinates = this.target ? this.target.getCoordinates( this.container ) : this.targetCoordinates ;
  331. var node = this.node;
  332. if( this.resetWidth ){
  333. node.setStyles({
  334. overflow : "visible",
  335. width : "auto"
  336. });
  337. if(this.arrowNode)this.arrowNode.setStyle("display","");
  338. this.resetWidth = false;
  339. }
  340. var containerScroll = this.container.getScroll();
  341. var containerSize = this.container.getSize();
  342. var nodeSize = node.getSize();
  343. var left;
  344. var arrowX, arrowY;
  345. var offsetX = (parseFloat(this.options.offset.x).toString() !== "NaN") ? parseFloat(this.options.offset.x) : 0;
  346. offsetX += this.options.hasArrow ? 10 : 0;
  347. if( this.options.position.x == "left" ) {
  348. left = targetCoondinates.left - nodeSize.x - offsetX;
  349. this.positionX = "left";
  350. arrowX = "right";
  351. }else if( this.options.position.x == "right" ){
  352. left = targetCoondinates.right + offsetX;
  353. this.positionX = "right";
  354. arrowX = "left";
  355. }else{
  356. var priorityOfAuto = this.options.priorityOfAuto;
  357. if( priorityOfAuto && priorityOfAuto.x ){
  358. for( var i=0; i<priorityOfAuto.x.length; i++ ){
  359. if( priorityOfAuto.x[i] == "left" ){
  360. if( targetCoondinates.left - containerScroll.x > containerSize.x - targetCoondinates.right){
  361. left = targetCoondinates.left - nodeSize.x - offsetX;
  362. this.positionX = "left";
  363. arrowX = "right";
  364. break;
  365. }
  366. }
  367. if( priorityOfAuto.x[i] == "right" ){
  368. if( containerSize.x + containerScroll.x - targetCoondinates.right > nodeSize.x ){
  369. left = targetCoondinates.right + offsetX;
  370. this.positionX = "right";
  371. arrowX = "left";
  372. break;
  373. }
  374. }
  375. }
  376. }
  377. if( !left ){
  378. if( targetCoondinates.left - containerScroll.x > containerSize.x - targetCoondinates.right){
  379. left = targetCoondinates.left - nodeSize.x - offsetX;
  380. this.positionX = "left";
  381. arrowX = "right";
  382. }else{
  383. left = targetCoondinates.right + offsetX;
  384. this.positionX = "right";
  385. arrowX = "left";
  386. }
  387. }
  388. }
  389. var top;
  390. if( this.options.position.y == "middle" ){
  391. top = targetCoondinates.top + (targetCoondinates.height/2) - ( nodeSize.y / 2 ) ;
  392. this.positionY = "middle";
  393. arrowY = "middle";
  394. }else if( this.options.position.y == "top" ){
  395. top = targetCoondinates.bottom - nodeSize.y;
  396. this.positionY = "top";
  397. arrowY = "bottom";
  398. }else if( this.options.position.y == "bottom" ){
  399. top = targetCoondinates.top;
  400. this.positionY = "bottom";
  401. arrowY = "top";
  402. }else{
  403. var priorityOfAuto = this.options.priorityOfAuto;
  404. if( priorityOfAuto && priorityOfAuto.y ){
  405. for( var i=0; i<priorityOfAuto.y.length; i++ ){
  406. if( priorityOfAuto.y[i] == "middle" ){
  407. if( targetCoondinates.top + (targetCoondinates.height/2) - ( nodeSize.y / 2 ) > containerScroll.y &&
  408. targetCoondinates.bottom - (targetCoondinates.height/2) + ( nodeSize.y / 2 ) - containerScroll.y < containerSize.y ){
  409. top = targetCoondinates.top + (targetCoondinates.height/2) - ( nodeSize.y / 2 ) ;
  410. this.positionY = "middle";
  411. arrowY = "middle";
  412. break;
  413. }
  414. }
  415. if( priorityOfAuto.y[i] == "top" ){
  416. if( targetCoondinates.top - containerScroll.y > containerSize.y - targetCoondinates.bottom ){
  417. top = targetCoondinates.bottom - nodeSize.y;
  418. this.positionY = "top";
  419. arrowY = "bottom";
  420. break;
  421. }
  422. }
  423. if( priorityOfAuto.y[i] == "bottom" ){
  424. if( containerSize.y + containerScroll.y - targetCoondinates.bottom > nodeSize.y ){
  425. top = targetCoondinates.top;
  426. this.positionY = "bottom";
  427. arrowY = "top";
  428. break;
  429. }
  430. }
  431. }
  432. }
  433. if( !top ){
  434. if( targetCoondinates.top + (targetCoondinates.height/2) - ( nodeSize.y / 2 ) > containerScroll.y &&
  435. targetCoondinates.bottom - (targetCoondinates.height/2) + ( nodeSize.y / 2 ) - containerScroll.y < containerSize.y ){
  436. top = targetCoondinates.top + (targetCoondinates.height/2) - ( nodeSize.y / 2 ) ;
  437. this.positionY = "middle";
  438. arrowY = "middle";
  439. } else if( targetCoondinates.top - containerScroll.y > containerSize.y - targetCoondinates.bottom ){
  440. top = targetCoondinates.bottom - nodeSize.y;
  441. this.positionY = "top";
  442. arrowY = "bottom";
  443. }else{
  444. top = targetCoondinates.top;
  445. this.positionY = "bottom";
  446. arrowY = "top";
  447. }
  448. }
  449. }
  450. var arrowOffsetY = 0;
  451. if( this.options.isFitToContainer ){
  452. if( top < containerScroll.y ){
  453. arrowOffsetY = containerScroll.y - top;
  454. top = containerScroll.y;
  455. }else if( top + nodeSize.y > containerSize.y + containerScroll.y ){
  456. arrowOffsetY = containerSize.y + containerScroll.y - top - nodeSize.y;
  457. top = containerSize.y + containerScroll.y - nodeSize.y;
  458. }
  459. }
  460. if( this.options.overflow == "scroll" ){
  461. if( left < 0 ){
  462. node.setStyles({
  463. "overflow" : "auto",
  464. "width" : nodeSize.x + left - offsetX
  465. });
  466. this.resetWidth = true;
  467. left = 0
  468. }else if( left + nodeSize.x > containerSize.x + containerScroll.x ){
  469. node.setStyles({
  470. "overflow" : "auto",
  471. "width" : Math.abs( containerSize.x + containerScroll.x - left + offsetX )
  472. });
  473. left = left - offsetX;
  474. this.resetWidth = true;
  475. }
  476. }
  477. if( this.resetWidth ){
  478. if( this.arrowNode )this.arrowNode.setStyle("display","none");
  479. }else if( this.options.hasArrow && this.arrowNode ) {
  480. if (arrowX == "left") {
  481. this.arrowNode.setStyles({
  482. "left": "-8px",
  483. "right": "auto",
  484. "background-position": "0px 0px"
  485. });
  486. } else {
  487. this.arrowNode.setStyles({
  488. "left": "auto",
  489. "right": "-8px",
  490. "background-position": "-11px 0px"
  491. });
  492. }
  493. var ah = this.arrowNode.getSize().y / 2;
  494. //var th = targetCoondinates.height / 2 - ah;
  495. var h = Math.min(targetCoondinates.height, nodeSize.y) / 2 - ah;
  496. var radiusDv = 0; //圆角和箭头偏移量的差值
  497. var radius = 0;
  498. if (arrowY == "middle") {
  499. this.arrowNode.setStyles({
  500. "top": (nodeSize.y / 2 - ah) - arrowOffsetY + "px",
  501. "bottom": "auto"
  502. })
  503. } else if (arrowY == "top") {
  504. radius = this.node.getStyle("border-top-" + arrowX + "-radius");
  505. radius = radius ? parseInt(radius) : 0;
  506. if (radius > h) {
  507. radiusDv = radius - h;
  508. }
  509. this.arrowNode.setStyles({
  510. "top": h + radiusDv - arrowOffsetY + "px",
  511. "bottom": "auto"
  512. })
  513. } else {
  514. radius = this.node.getStyle("border-bottom-" + arrowX + "-radius");
  515. radius = radius ? parseInt(radius) : 0;
  516. if (radius > h) {
  517. radiusDv = radius - h;
  518. }
  519. this.arrowNode.setStyles({
  520. "top": "auto",
  521. "bottom": h + radiusDv + arrowOffsetY + "px"
  522. })
  523. }
  524. var t = top;
  525. if (radiusDv) {
  526. if (arrowY == "top") {
  527. t = t - radiusDv;
  528. } else if (arrowY == "bottom") {
  529. t = t + radiusDv;
  530. }
  531. }
  532. }
  533. node.setStyles({
  534. "left" : left,
  535. "top" : t || top
  536. });
  537. this.fireEvent( "postSetCoondinates", [arrowX, arrowY] );
  538. },
  539. setCoondinates_y : function(){
  540. var targetCoondinates = this.target ? this.target.getCoordinates( this.container ) : this.targetCoordinates ;
  541. var node = this.node;
  542. if( this.resetHeight ){
  543. node.setStyles({
  544. overflow : "visible",
  545. height : "auto"
  546. });
  547. if(this.arrowNode)this.arrowNode.setStyle("display","");
  548. this.resetHeight = false;
  549. }
  550. var containerScroll = this.container.getScroll();
  551. var containerSize = this.container.getSize();
  552. var nodeSize = node.getSize();
  553. var top;
  554. var arrowX, arrowY;
  555. var offsetY = (parseFloat(this.options.offset.y).toString() !== "NaN") ? parseFloat(this.options.offset.y) : 0;
  556. offsetY += this.options.hasArrow ? 10 : 0;
  557. if( this.options.position.y == "top" ){
  558. top = targetCoondinates.top - nodeSize.y - offsetY;
  559. this.positionY = "top";
  560. arrowY = "bottom";
  561. }else if( this.options.position.y == "bottom" ){
  562. top = targetCoondinates.bottom + offsetY;
  563. this.positionY = "bottom";
  564. arrowY = "top";
  565. }else{
  566. var priorityOfAuto = this.options.priorityOfAuto;
  567. if( priorityOfAuto && priorityOfAuto.y ){
  568. for( var i=0; i<priorityOfAuto.y.length; i++ ){
  569. if( priorityOfAuto.y[i] == "top" ){
  570. if( targetCoondinates.top - containerScroll.y > containerSize.y - targetCoondinates.bottom ){
  571. top = targetCoondinates.top - nodeSize.y - offsetY;
  572. this.positionY = "top";
  573. arrowY = "bottom";
  574. break;
  575. }
  576. }
  577. if( priorityOfAuto.y[i] == "bottom" ){
  578. if( containerSize.y + containerScroll.y - targetCoondinates.bottom > nodeSize.y ){
  579. top = targetCoondinates.bottom + offsetY;
  580. this.positionY = "bottom";
  581. arrowY = "top";
  582. break;
  583. }
  584. }
  585. }
  586. }
  587. if( !top ){
  588. if( targetCoondinates.top - containerScroll.y > containerSize.y - targetCoondinates.bottom){
  589. top = targetCoondinates.top - nodeSize.y - offsetY;
  590. this.positionY = "top";
  591. arrowY = "bottom";
  592. }else{
  593. top = targetCoondinates.bottom + offsetY;
  594. this.positionY = "bottom";
  595. arrowY = "top";
  596. }
  597. }
  598. }
  599. var left;
  600. if( this.options.position.x == "center" ){
  601. left = targetCoondinates.left + (targetCoondinates.width/2) - ( nodeSize.x / 2 ) ;
  602. this.positionX = "center";
  603. arrowX = "center";
  604. }else if( this.options.position.x == "left" ){
  605. left = targetCoondinates.right - nodeSize.x;
  606. this.positionX = "left";
  607. arrowX = "right";
  608. }else if( this.options.position.x == "right" ){
  609. left = targetCoondinates.left;
  610. this.positionX = "right";
  611. arrowX = "left";
  612. }else{
  613. var priorityOfAuto = this.options.priorityOfAuto;
  614. if( priorityOfAuto && priorityOfAuto.x ){
  615. for( var i=0; i<priorityOfAuto.x.length; i++ ){
  616. if( priorityOfAuto.x[i] == "center" ){
  617. if( targetCoondinates.left + (targetCoondinates.width/2) - ( nodeSize.x / 2 ) > containerScroll.x &&
  618. targetCoondinates.right - (targetCoondinates.width/2) + ( nodeSize.x / 2 ) - containerScroll.x < containerSize.x ){
  619. left = targetCoondinates.left + (targetCoondinates.width/2) - ( nodeSize.x / 2 ) ;
  620. this.positionX = "center";
  621. arrowX = "center";
  622. break;
  623. }
  624. }
  625. if( priorityOfAuto.x[i] == "left" ){
  626. if( targetCoondinates.left - containerScroll.x > containerSize.x - targetCoondinates.right){
  627. left = targetCoondinates.right - nodeSize.x;
  628. this.positionX = "left";
  629. arrowX = "right";
  630. break;
  631. }
  632. }
  633. if( priorityOfAuto.x[i] == "right" ){
  634. if( containerSize.x + containerScroll.x - targetCoondinates.right > nodeSize.x ){
  635. left = targetCoondinates.left;
  636. this.positionX = "right";
  637. arrowX = "left";
  638. break;
  639. }
  640. }
  641. }
  642. }
  643. if( !left ){
  644. if( targetCoondinates.left + (targetCoondinates.width/2) - ( nodeSize.x / 2 ) > containerScroll.x &&
  645. targetCoondinates.right - (targetCoondinates.width/2) + ( nodeSize.x / 2 ) - containerScroll.x < containerSize.x ){
  646. left = targetCoondinates.left + (targetCoondinates.width/2) - ( nodeSize.x / 2 ) ;
  647. this.positionX = "center";
  648. arrowX = "center";
  649. } else if( targetCoondinates.left - containerScroll.x > containerSize.x - targetCoondinates.right ){
  650. left = targetCoondinates.right - nodeSize.x;
  651. this.positionX = "left";
  652. arrowX = "right";
  653. }else{
  654. left = targetCoondinates.left;
  655. this.positionX = "right";
  656. arrowX = "left";
  657. }
  658. }
  659. }
  660. var arrowOffsetX = 0;
  661. if( this.options.isFitToContainer ){
  662. if( left < containerScroll.x ){
  663. arrowOffsetX = containerScroll.x - left;
  664. left = containerScroll.x;
  665. }else if( left + nodeSize.x > containerSize.x + containerScroll.x ){
  666. arrowOffsetX = containerSize.x + containerScroll.x - left - nodeSize.x;
  667. left = containerSize.x + containerScroll.x - nodeSize.x;
  668. }
  669. }
  670. if( this.options.overflow == "scroll" ){
  671. if( top < 0 ){
  672. node.setStyles({
  673. "overflow" : "auto",
  674. "height" : nodeSize.y + top - offsetY
  675. });
  676. this.resetHeight = true;
  677. top = 0
  678. }else if( top + nodeSize.y > containerSize.y + containerScroll.y ){
  679. node.setStyles({
  680. "overflow" : "auto",
  681. "height" : Math.abs( containerSize.y + containerScroll.y - top + offsetY )
  682. });
  683. top = top - offsetY;
  684. this.resetHeight = true;
  685. }
  686. }
  687. if( this.resetHeight ){
  688. if( this.arrowNode )this.arrowNode.setStyle("display","none");
  689. }else if( this.options.hasArrow && this.arrowNode ){
  690. if( arrowY == "top" ){
  691. this.arrowNode.setStyles( {
  692. "top" : "-8px",
  693. "bottom" : "auto",
  694. "background-position": "0px -18px"
  695. });
  696. }else{
  697. this.arrowNode.setStyles( {
  698. "top" : "auto",
  699. "bottom" : "-8px",
  700. "background-position": "0px -28px"
  701. });
  702. }
  703. var aw = this.arrowNode.getSize().x / 2 ;
  704. //var tw = targetCoondinates.width / 2 - aw;
  705. var w = Math.min( targetCoondinates.width , nodeSize.x )/ 2 - aw;
  706. var radiusDv = 0; //圆角和箭头偏移量的差值
  707. var radius = 0; //圆角值
  708. if( arrowX == "center" ) {
  709. this.arrowNode.setStyles({
  710. "left": (nodeSize.x/2 - aw - arrowOffsetX )+"px",
  711. "right": "auto"
  712. })
  713. }else if( arrowX == "left" ){
  714. radius = this.node.getStyle("border-"+arrowY+"-left-radius");
  715. radius = radius ? parseInt( radius ) : 0;
  716. if( radius > w ){
  717. radiusDv = radius - w;
  718. }
  719. this.arrowNode.setStyles({
  720. "left" : w + radiusDv - arrowOffsetX + "px",
  721. "right" : "auto"
  722. })
  723. }else{
  724. radius = this.node.getStyle("border-" + arrowY + "-right-radius");
  725. radius = radius ? parseInt(radius) : 0;
  726. if( radius > w ){
  727. radiusDv = radius - w;
  728. }
  729. this.arrowNode.setStyles({
  730. "left" : "auto",
  731. "right" : w + radiusDv + arrowOffsetX +"px"
  732. })
  733. }
  734. var l = left;
  735. if( radiusDv ){
  736. if( arrowX == "left" ){
  737. l = l - radiusDv;
  738. }else if( arrowX == "right" ){
  739. l = l + radiusDv;
  740. }
  741. }
  742. }
  743. node.setStyles({
  744. "left" : l || left,
  745. "top" : top
  746. });
  747. this.fireEvent( "postSetCoondinates", [arrowX, arrowY] );
  748. },
  749. setPosition : function(){
  750. if( this.options.axis == "x" ){
  751. this.setPosition_x();
  752. }else{
  753. this.setPosition_y();
  754. }
  755. },
  756. setPosition_x : function(){
  757. var top, left;
  758. var targetCoondinates = this.target ? this.target.getCoordinates( this.container ) : this.targetCoordinates ;
  759. var node = this.node;
  760. var nodeSize = node.getSize();
  761. var offsetX = (parseFloat(this.options.offset.x).toString() !== "NaN") ? parseFloat(this.options.offset.x) : 0;
  762. offsetX += this.options.hasArrow ? 10 : 0;
  763. if( this.positionX === "left" ){
  764. left = targetCoondinates.left - nodeSize.x - offsetX;
  765. }else if( this.positionX === "bottom" ){
  766. left = targetCoondinates.right + offsetX;
  767. }
  768. if( this.positionY === "top" ){
  769. top = targetCoondinates.top - nodeSize.y;
  770. }else if( this.positionY === "bottom" ){
  771. top = targetCoondinates.bottom;
  772. }else if( this.positionX === "middle" ){
  773. top = targetCoondinates.top + (targetCoondinates.height/2) - ( nodeSize.y / 2 )
  774. }
  775. node.setStyles({
  776. "left" : left,
  777. "top" : top
  778. });
  779. },
  780. setPosition_y : function(){
  781. var top, left;
  782. var targetCoondinates = this.target ? this.target.getCoordinates( this.container ) : this.targetCoordinates ;
  783. var node = this.node;
  784. var nodeSize = node.getSize();
  785. var offsetY = (parseFloat(this.options.offset.y).toString() !== "NaN") ? parseFloat(this.options.offset.y) : 0;
  786. offsetY += this.options.hasArrow ? 10 : 0;
  787. if( this.positionY === "top" ){
  788. top = targetCoondinates.top - nodeSize.y - offsetY;
  789. }else if( this.positionY === "bottom" ){
  790. top = targetCoondinates.bottom + offsetY;
  791. }
  792. if( this.positionX === "left" ){
  793. left = targetCoondinates.left - nodeSize.x;
  794. }else if( this.positionX === "right" ){
  795. left = targetCoondinates.right;
  796. }else if( this.positionX === "center" ){
  797. left = targetCoondinates.left + (targetCoondinates.width/2) - ( nodeSize.x / 2 )
  798. }
  799. node.setStyles({
  800. "left" : left,
  801. "top" : top
  802. });
  803. },
  804. destroy: function(){
  805. //if( this.options.event == "click" && this.node ){
  806. // this.container.removeEvent("mousedown",this.hideFun );
  807. //}
  808. if( this.options.event == "click" && this.app && this.hideFun_resize ){
  809. this.app.removeEvent("resize",this.hideFun_resize );
  810. }
  811. if( this.targetClickFun )this.target.removeEvent( "click", this.targetClickFun );
  812. if( this.targetMouseenterFun )this.target.removeEvent( "mouseenter", this.targetMouseenterFun );
  813. if( this.targetMouseenterFun2 )this.target.removeEvent( "mouseenter", this.targetMouseenterFun2 );
  814. if( this.targetMouseleaveFun )this.target.removeEvent( "mouseleave", this.targetMouseleaveFun );
  815. if( this.targetMouseleaveFun2 )this.target.removeEvent( "mouseleave", this.targetMouseleaveFun2 );
  816. if( this.node ){
  817. this.node.destroy();
  818. this.node = null;
  819. }
  820. this.fireEvent("destroy",[this]);
  821. MWF.release(this);
  822. },
  823. _getHtml : function(){
  824. //var data = this.data;
  825. //var titleStyle = "font-size:14px;color:#333";
  826. //var valueStyle = "font-size:14px;color:#666;padding-right:20px";
  827. //var persons = [];
  828. //data.invitePersonList.each( function( p ){
  829. // persons.push(p.split("@")[0] )
  830. //}.bind(this));
  831. //
  832. //var html =
  833. // "<div style='overflow: hidden;padding:15px 20px 20px 10px;height:16px;line-height:16px;'>" +
  834. // " <div style='font-size: 12px;color:#666; float: right'>"+ this.lp.applyPerson +":" + data.applicant.split("@")[0] +"</div>" +
  835. // " <div style='font-size: 16px;color:#333;float: left'>"+ this.lp.meetingDetail +"</div>"+
  836. // "</div>"+
  837. // "<div style='font-size: 18px;color:#333;padding:0px 10px 15px 20px;'>"+ data.subject +"</div>"+
  838. // "<div style='height:1px;margin:0px 20px;border-bottom:1px solid #ccc;'></div>"+
  839. // "<table width='100%' bordr='0' cellpadding='7' cellspacing='0' style='margin:13px 13px 13px 13px;'>" +
  840. // "<tr><td style='"+titleStyle+"' width='70'>"+this.lp.beginTime+":</td>" +
  841. // " <td style='"+valueStyle+"'>" + data.startTime + "</td></tr>" +
  842. // "<tr><td style='"+titleStyle+"'>"+this.lp.endTime+":</td>" +
  843. // " <td style='"+valueStyle+"'>" + data.completedTime + "</td></tr>" +
  844. // "<tr><td style='"+titleStyle+"'>"+this.lp.selectRoom +":</td>" +
  845. // " <td style='"+valueStyle+"' item='meetingRoom'></td></tr>" +
  846. // "<tr><td style='"+titleStyle+"'>"+this.lp.invitePerson2+":</td>" +
  847. // " <td style='"+valueStyle+"' item='invitePerson'>"+persons.join(",")+"</td></tr>" +
  848. // "<tr><td style='"+titleStyle+"'>"+this.lp.meetingDescription+":</td>" +
  849. // " <td style='"+valueStyle+"'>"+ data.description +"</td></tr>" +
  850. // "<tr><td style='"+titleStyle+"'>"+this.lp.meetingAttachment+":</td>" +
  851. // " <td style='"+valueStyle+"' item='attachment'></td></tr>"+
  852. // "</table>";
  853. return "";
  854. },
  855. _customNode : function( node, contentNode ){
  856. },
  857. _setContent : function( contentNode ){
  858. this.contentNode = contentNode;
  859. if( this.node ){
  860. this.node.empty();
  861. this.contentNode.inject( this.node );
  862. }
  863. },
  864. _loadCustom : function( callback ){
  865. if(callback)callback();
  866. }
  867. });