MTooltips.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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.fireEvent("loadStyle",[this]);
  172. this.node = new Element("div.tooltipNode", {
  173. styles : this.nodeStyles
  174. }).inject( this.container );
  175. if( this.contentNode ){
  176. this.contentNode.inject( this.node );
  177. }else{
  178. this.contentNode = new Element("div",{
  179. styles : this.contentStyles
  180. }).inject( this.node );
  181. this.contentNode.set("html", this._getHtml() );
  182. }
  183. this._customNode( this.node, this.contentNode );
  184. if( this.options.hasArrow ){
  185. this.arrowNode = new Element("div.arrowNode", {
  186. "styles": this.arrowStyles
  187. }
  188. ).inject(this.node);
  189. }
  190. if( this.options.hasCloseAction ){
  191. this.closeActionNode = new Element("div", {
  192. styles : this.closeActionStyles,
  193. events : {
  194. click : function(){ this.hide() }.bind(this)
  195. }
  196. }).inject( this.node );
  197. }
  198. this._loadCustom( function(){
  199. this.setCoondinates();
  200. }.bind(this));
  201. if( this.options.event == "click" ) {
  202. if( this.options.isAutoHide ){
  203. if( this.options.hasMask ){
  204. this.maskNode = new Element("div.maskNode", {
  205. "styles": this.maskStyles,
  206. "events": {
  207. "mouseover": function (e) {
  208. e.stopPropagation();
  209. },
  210. "mouseout": function (e) {
  211. e.stopPropagation();
  212. },
  213. "click": function (e) {
  214. this.hide();
  215. e.stopPropagation();
  216. }.bind(this)
  217. }
  218. }).inject( this.container );
  219. }
  220. if( this.app ){
  221. this.hideFun_resize = this.hide.bind(this);
  222. this.app.addEvent( "resize" , this.hideFun_resize );
  223. }
  224. }
  225. }else{
  226. if( this.options.isAutoHide || this.options.isAutoShow ){
  227. this.node.addEvents({
  228. "mouseenter": function(){
  229. if( this.timer_hide )clearTimeout(this.timer_hide);
  230. }.bind(this)
  231. });
  232. }
  233. if( this.options.isAutoHide ){
  234. this.node.addEvents({
  235. "mouseleave" : function(){
  236. this.timer_hide = setTimeout( this.hide.bind(this),this.options.hiddenDelay );
  237. }.bind(this)
  238. });
  239. }
  240. }
  241. //this.target.addEvent( "mouseleave", function(){
  242. // this.timer_hide = setTimeout( this.hide.bind(this), this.options.HiddenDelay );
  243. //}.bind(this));
  244. this.fireEvent("postCreate",[this]);
  245. },
  246. loadStyle : function(){
  247. if( this.options.style ){
  248. this.cssPath = this.path+this.options.style+"/css.wcss";
  249. this._loadCss();
  250. }
  251. this.nodeStyles = {
  252. "font-size" : "12px",
  253. "position" : "absolute",
  254. "max-width" : "500px",
  255. "min-width" : "260px",
  256. "z-index" : "11",
  257. "background-color" : "#fff",
  258. "padding" : "10px",
  259. "border-radius" : "8px",
  260. "box-shadow": "0 0 18px 0 #999999",
  261. "-webkit-user-select": "text",
  262. "-moz-user-select": "text"
  263. };
  264. if( this.options.nodeStyles ){ //兼容之前在options里设置nodeStyles
  265. this.nodeStyles = Object.merge( this.nodeStyles, this.options.nodeStyles );
  266. }else if( this.css && this.css.nodeStyles ){
  267. this.nodeStyles = this.css.nodeStyles
  268. }
  269. if( this.css && this.css.contentStyles ){
  270. this.contentStyles = this.css.contentStyles;
  271. }else{
  272. this.contentStyles = {
  273. "width" : "100%",
  274. "height" : "100%"
  275. }
  276. }
  277. if( this.options.hasArrow ){
  278. if( this.css && this.css.arrowStyles ){
  279. this.arrowStyles = this.css.arrowStyles
  280. }else{
  281. this.arrowStyles = {
  282. "width": this.options.axis == "x" ? "9px" : "17px",
  283. "height" : this.options.axis == "x" ? "17px" : "9px",
  284. "position":"absolute",
  285. "background" : "no-repeat url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAAlCAYAAACgc9J8AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAP9JREFUeNq01oENhCAMBdCWuIgTuP8WOoGj9GhSDaeUfjiuCSEm+iKFVllESGNdVzrPk55xHIfewHnItm1MrVDMG/u+i0aeyWZp3R9CJRaBIWRB5YUHItAL80AEqmI1EIFcrAQ1rwjUxEoQgULsAue+2ayc/W83p5+z6RXggOO1WQGhrsFXP/Oip58tAaQTZ4QMbEbyEB2GCKEBdtpmru4NsC4aHg8PLJ/3lim2xDv02jYDz1kNQsGEQgiYeqAQBPuZQF1jFKqBYTn1RLK1D4+v3E3NGfgNkJlfdKi0XrUZgc1OWyt0Dwz/z37tGhA20s+WR4t+1iBbzTJyaD8CDAB7WgNSzh/AnwAAAABJRU5ErkJggg==)"
  286. }
  287. }
  288. }
  289. if( this.options.event == "click" && this.options.isAutoHide ) {
  290. if( this.css && this.css.maskStyles ){
  291. this.maskStyles = this.css.maskStyles;
  292. }else{
  293. this.maskStyles = {
  294. "width": "100%",
  295. "height": "100%",
  296. "opacity": 0,
  297. "position": "absolute",
  298. "background-color": "#fff",
  299. "top": "0px",
  300. "left": "0px"
  301. }
  302. }
  303. }
  304. if( this.options.hasCloseAction ){
  305. if( this.css && this.css.closeActionStyles ){
  306. this.closeActionStyles = this.css.closeActionStyles
  307. }else{
  308. this.closeActionStyles = {
  309. "width": "24px",
  310. "height" : "24px",
  311. "position" : "absolute",
  312. "top": "0px",
  313. "right" : "0px",
  314. "background": "url(../x_component_Template/$MTooltips/default/icon/off_gray.png) no-repeat center center",
  315. "cursor": "pointer"
  316. }
  317. }
  318. }
  319. },
  320. isEnable : function(){
  321. return !this.disable;
  322. },
  323. setCoondinates : function(){
  324. if( this.options.axis == "x" ){
  325. this.setCoondinates_x();
  326. }else{
  327. this.setCoondinates_y();
  328. }
  329. },
  330. setCoondinates_x : function(){
  331. var targetCoondinates = this.target ? this.target.getCoordinates( this.container ) : this.targetCoordinates ;
  332. var node = this.node;
  333. if( this.resetWidth ){
  334. node.setStyles({
  335. overflow : "visible",
  336. width : "auto"
  337. });
  338. if(this.arrowNode)this.arrowNode.setStyle("display","");
  339. this.resetWidth = false;
  340. }
  341. var containerScroll = this.container.getScroll();
  342. var containerSize = this.container.getSize();
  343. var nodeSize = node.getSize();
  344. var left;
  345. var arrowX, arrowY;
  346. var offsetX = (parseFloat(this.options.offset.x).toString() !== "NaN") ? parseFloat(this.options.offset.x) : 0;
  347. offsetX += this.options.hasArrow ? 10 : 0;
  348. if( this.options.position.x == "left" ) {
  349. left = targetCoondinates.left - nodeSize.x - offsetX;
  350. this.positionX = "left";
  351. arrowX = "right";
  352. }else if( this.options.position.x == "right" ){
  353. left = targetCoondinates.right + offsetX;
  354. this.positionX = "right";
  355. arrowX = "left";
  356. }else{
  357. var priorityOfAuto = this.options.priorityOfAuto;
  358. if( priorityOfAuto && priorityOfAuto.x ){
  359. for( var i=0; i<priorityOfAuto.x.length; i++ ){
  360. if( priorityOfAuto.x[i] == "left" ){
  361. if( targetCoondinates.left - containerScroll.x > containerSize.x - targetCoondinates.right){
  362. left = targetCoondinates.left - nodeSize.x - offsetX;
  363. this.positionX = "left";
  364. arrowX = "right";
  365. break;
  366. }
  367. }
  368. if( priorityOfAuto.x[i] == "right" ){
  369. if( containerSize.x + containerScroll.x - targetCoondinates.right > nodeSize.x ){
  370. left = targetCoondinates.right + offsetX;
  371. this.positionX = "right";
  372. arrowX = "left";
  373. break;
  374. }
  375. }
  376. }
  377. }
  378. if( !left ){
  379. if( targetCoondinates.left - containerScroll.x > containerSize.x - targetCoondinates.right){
  380. left = targetCoondinates.left - nodeSize.x - offsetX;
  381. this.positionX = "left";
  382. arrowX = "right";
  383. }else{
  384. left = targetCoondinates.right + offsetX;
  385. this.positionX = "right";
  386. arrowX = "left";
  387. }
  388. }
  389. }
  390. var top;
  391. if( this.options.position.y == "middle" ){
  392. top = targetCoondinates.top + (targetCoondinates.height/2) - ( nodeSize.y / 2 ) ;
  393. this.positionY = "middle";
  394. arrowY = "middle";
  395. }else if( this.options.position.y == "top" ){
  396. top = targetCoondinates.bottom - nodeSize.y;
  397. this.positionY = "top";
  398. arrowY = "bottom";
  399. }else if( this.options.position.y == "bottom" ){
  400. top = targetCoondinates.top;
  401. this.positionY = "bottom";
  402. arrowY = "top";
  403. }else{
  404. var priorityOfAuto = this.options.priorityOfAuto;
  405. if( priorityOfAuto && priorityOfAuto.y ){
  406. for( var i=0; i<priorityOfAuto.y.length; i++ ){
  407. if( priorityOfAuto.y[i] == "middle" ){
  408. if( targetCoondinates.top + (targetCoondinates.height/2) - ( nodeSize.y / 2 ) > containerScroll.y &&
  409. targetCoondinates.bottom - (targetCoondinates.height/2) + ( nodeSize.y / 2 ) - containerScroll.y < containerSize.y ){
  410. top = targetCoondinates.top + (targetCoondinates.height/2) - ( nodeSize.y / 2 ) ;
  411. this.positionY = "middle";
  412. arrowY = "middle";
  413. break;
  414. }
  415. }
  416. if( priorityOfAuto.y[i] == "top" ){
  417. if( targetCoondinates.top - containerScroll.y > containerSize.y - targetCoondinates.bottom ){
  418. top = targetCoondinates.bottom - nodeSize.y;
  419. this.positionY = "top";
  420. arrowY = "bottom";
  421. break;
  422. }
  423. }
  424. if( priorityOfAuto.y[i] == "bottom" ){
  425. if( containerSize.y + containerScroll.y - targetCoondinates.bottom > nodeSize.y ){
  426. top = targetCoondinates.top;
  427. this.positionY = "bottom";
  428. arrowY = "top";
  429. break;
  430. }
  431. }
  432. }
  433. }
  434. if( !top ){
  435. if( targetCoondinates.top + (targetCoondinates.height/2) - ( nodeSize.y / 2 ) > containerScroll.y &&
  436. targetCoondinates.bottom - (targetCoondinates.height/2) + ( nodeSize.y / 2 ) - containerScroll.y < containerSize.y ){
  437. top = targetCoondinates.top + (targetCoondinates.height/2) - ( nodeSize.y / 2 ) ;
  438. this.positionY = "middle";
  439. arrowY = "middle";
  440. } else if( targetCoondinates.top - containerScroll.y > containerSize.y - targetCoondinates.bottom ){
  441. top = targetCoondinates.bottom - nodeSize.y;
  442. this.positionY = "top";
  443. arrowY = "bottom";
  444. }else{
  445. top = targetCoondinates.top;
  446. this.positionY = "bottom";
  447. arrowY = "top";
  448. }
  449. }
  450. }
  451. var arrowOffsetY = 0;
  452. if( this.options.isFitToContainer ){
  453. if( top < containerScroll.y ){
  454. arrowOffsetY = containerScroll.y - top;
  455. top = containerScroll.y;
  456. }else if( top + nodeSize.y > containerSize.y + containerScroll.y ){
  457. arrowOffsetY = containerSize.y + containerScroll.y - top - nodeSize.y;
  458. top = containerSize.y + containerScroll.y - nodeSize.y;
  459. }
  460. }
  461. if( this.options.overflow == "scroll" ){
  462. if( left < 0 ){
  463. node.setStyles({
  464. "overflow" : "auto",
  465. "width" : nodeSize.x + left - offsetX
  466. });
  467. this.resetWidth = true;
  468. left = 0
  469. }else if( left + nodeSize.x > containerSize.x + containerScroll.x ){
  470. node.setStyles({
  471. "overflow" : "auto",
  472. "width" : Math.abs( containerSize.x + containerScroll.x - left + offsetX )
  473. });
  474. left = left - offsetX;
  475. this.resetWidth = true;
  476. }
  477. }
  478. if( this.resetWidth ){
  479. if( this.arrowNode )this.arrowNode.setStyle("display","none");
  480. }else if( this.options.hasArrow && this.arrowNode ) {
  481. if (arrowX == "left") {
  482. this.arrowNode.setStyles({
  483. "left": "-8px",
  484. "right": "auto",
  485. "background-position": "0px 0px"
  486. });
  487. } else {
  488. this.arrowNode.setStyles({
  489. "left": "auto",
  490. "right": "-8px",
  491. "background-position": "-11px 0px"
  492. });
  493. }
  494. var ah = this.arrowNode.getSize().y / 2;
  495. //var th = targetCoondinates.height / 2 - ah;
  496. var h = Math.min(targetCoondinates.height, nodeSize.y) / 2 - ah;
  497. var radiusDv = 0; //圆角和箭头偏移量的差值
  498. var radius = 0;
  499. if (arrowY == "middle") {
  500. this.arrowNode.setStyles({
  501. "top": (nodeSize.y / 2 - ah) - arrowOffsetY + "px",
  502. "bottom": "auto"
  503. })
  504. } else if (arrowY == "top") {
  505. radius = this.node.getStyle("border-top-" + arrowX + "-radius");
  506. radius = radius ? parseInt(radius) : 0;
  507. if (radius > h) {
  508. radiusDv = radius - h;
  509. }
  510. this.arrowNode.setStyles({
  511. "top": h + radiusDv - arrowOffsetY + "px",
  512. "bottom": "auto"
  513. })
  514. } else {
  515. radius = this.node.getStyle("border-bottom-" + arrowX + "-radius");
  516. radius = radius ? parseInt(radius) : 0;
  517. if (radius > h) {
  518. radiusDv = radius - h;
  519. }
  520. this.arrowNode.setStyles({
  521. "top": "auto",
  522. "bottom": h + radiusDv + arrowOffsetY + "px"
  523. })
  524. }
  525. var t = top;
  526. if (radiusDv) {
  527. if (arrowY == "top") {
  528. t = t - radiusDv;
  529. } else if (arrowY == "bottom") {
  530. t = t + radiusDv;
  531. }
  532. }
  533. }
  534. node.setStyles({
  535. "left" : left,
  536. "top" : t || top
  537. });
  538. this.fireEvent( "postSetCoondinates", [arrowX, arrowY] );
  539. },
  540. setCoondinates_y : function(){
  541. var targetCoondinates = this.target ? this.target.getCoordinates( this.container ) : this.targetCoordinates ;
  542. var node = this.node;
  543. if( this.resetHeight ){
  544. node.setStyles({
  545. overflow : "visible",
  546. height : "auto"
  547. });
  548. if(this.arrowNode)this.arrowNode.setStyle("display","");
  549. this.resetHeight = false;
  550. }
  551. var containerScroll = this.container.getScroll();
  552. var containerSize = this.container.getSize();
  553. var nodeSize = node.getSize();
  554. var top;
  555. var arrowX, arrowY;
  556. var offsetY = (parseFloat(this.options.offset.y).toString() !== "NaN") ? parseFloat(this.options.offset.y) : 0;
  557. offsetY += this.options.hasArrow ? 10 : 0;
  558. if( this.options.position.y == "top" ){
  559. top = targetCoondinates.top - nodeSize.y - offsetY;
  560. this.positionY = "top";
  561. arrowY = "bottom";
  562. }else if( this.options.position.y == "bottom" ){
  563. top = targetCoondinates.bottom + offsetY;
  564. this.positionY = "bottom";
  565. arrowY = "top";
  566. }else{
  567. var priorityOfAuto = this.options.priorityOfAuto;
  568. if( priorityOfAuto && priorityOfAuto.y ){
  569. for( var i=0; i<priorityOfAuto.y.length; i++ ){
  570. if( priorityOfAuto.y[i] == "top" ){
  571. if( targetCoondinates.top - containerScroll.y > containerSize.y - targetCoondinates.bottom ){
  572. top = targetCoondinates.top - nodeSize.y - offsetY;
  573. this.positionY = "top";
  574. arrowY = "bottom";
  575. break;
  576. }
  577. }
  578. if( priorityOfAuto.y[i] == "bottom" ){
  579. if( containerSize.y + containerScroll.y - targetCoondinates.bottom > nodeSize.y ){
  580. top = targetCoondinates.bottom + offsetY;
  581. this.positionY = "bottom";
  582. arrowY = "top";
  583. break;
  584. }
  585. }
  586. }
  587. }
  588. if( !top ){
  589. if( targetCoondinates.top - containerScroll.y > containerSize.y - targetCoondinates.bottom){
  590. top = targetCoondinates.top - nodeSize.y - offsetY;
  591. this.positionY = "top";
  592. arrowY = "bottom";
  593. }else{
  594. top = targetCoondinates.bottom + offsetY;
  595. this.positionY = "bottom";
  596. arrowY = "top";
  597. }
  598. }
  599. }
  600. var left;
  601. if( this.options.position.x == "center" ){
  602. left = targetCoondinates.left + (targetCoondinates.width/2) - ( nodeSize.x / 2 ) ;
  603. this.positionX = "center";
  604. arrowX = "center";
  605. }else if( this.options.position.x == "left" ){
  606. left = targetCoondinates.right - nodeSize.x;
  607. this.positionX = "left";
  608. arrowX = "right";
  609. }else if( this.options.position.x == "right" ){
  610. left = targetCoondinates.left;
  611. this.positionX = "right";
  612. arrowX = "left";
  613. }else{
  614. var priorityOfAuto = this.options.priorityOfAuto;
  615. if( priorityOfAuto && priorityOfAuto.x ){
  616. for( var i=0; i<priorityOfAuto.x.length; i++ ){
  617. if( priorityOfAuto.x[i] == "center" ){
  618. if( targetCoondinates.left + (targetCoondinates.width/2) - ( nodeSize.x / 2 ) > containerScroll.x &&
  619. targetCoondinates.right - (targetCoondinates.width/2) + ( nodeSize.x / 2 ) - containerScroll.x < containerSize.x ){
  620. left = targetCoondinates.left + (targetCoondinates.width/2) - ( nodeSize.x / 2 ) ;
  621. this.positionX = "center";
  622. arrowX = "center";
  623. break;
  624. }
  625. }
  626. if( priorityOfAuto.x[i] == "left" ){
  627. if( targetCoondinates.left - containerScroll.x > containerSize.x - targetCoondinates.right){
  628. left = targetCoondinates.right - nodeSize.x;
  629. this.positionX = "left";
  630. arrowX = "right";
  631. break;
  632. }
  633. }
  634. if( priorityOfAuto.x[i] == "right" ){
  635. if( containerSize.x + containerScroll.x - targetCoondinates.right > nodeSize.x ){
  636. left = targetCoondinates.left;
  637. this.positionX = "right";
  638. arrowX = "left";
  639. break;
  640. }
  641. }
  642. }
  643. }
  644. if( !left ){
  645. if( targetCoondinates.left + (targetCoondinates.width/2) - ( nodeSize.x / 2 ) > containerScroll.x &&
  646. targetCoondinates.right - (targetCoondinates.width/2) + ( nodeSize.x / 2 ) - containerScroll.x < containerSize.x ){
  647. left = targetCoondinates.left + (targetCoondinates.width/2) - ( nodeSize.x / 2 ) ;
  648. this.positionX = "center";
  649. arrowX = "center";
  650. } else if( targetCoondinates.left - containerScroll.x > containerSize.x - targetCoondinates.right ){
  651. left = targetCoondinates.right - nodeSize.x;
  652. this.positionX = "left";
  653. arrowX = "right";
  654. }else{
  655. left = targetCoondinates.left;
  656. this.positionX = "right";
  657. arrowX = "left";
  658. }
  659. }
  660. }
  661. var arrowOffsetX = 0;
  662. if( this.options.isFitToContainer ){
  663. if( left < containerScroll.x ){
  664. arrowOffsetX = containerScroll.x - left;
  665. left = containerScroll.x;
  666. }else if( left + nodeSize.x > containerSize.x + containerScroll.x ){
  667. arrowOffsetX = containerSize.x + containerScroll.x - left - nodeSize.x;
  668. left = containerSize.x + containerScroll.x - nodeSize.x;
  669. }
  670. }
  671. if( this.options.overflow == "scroll" ){
  672. if( top < 0 ){
  673. node.setStyles({
  674. "overflow" : "auto",
  675. "height" : nodeSize.y + top - offsetY
  676. });
  677. this.resetHeight = true;
  678. top = 0
  679. }else if( top + nodeSize.y > containerSize.y + containerScroll.y ){
  680. node.setStyles({
  681. "overflow" : "auto",
  682. "height" : Math.abs( containerSize.y + containerScroll.y - top + offsetY )
  683. });
  684. top = top - offsetY;
  685. this.resetHeight = true;
  686. }
  687. }
  688. if( this.resetHeight ){
  689. if( this.arrowNode )this.arrowNode.setStyle("display","none");
  690. }else if( this.options.hasArrow && this.arrowNode ){
  691. if( arrowY == "top" ){
  692. this.arrowNode.setStyles( {
  693. "top" : "-8px",
  694. "bottom" : "auto",
  695. "background-position": "0px -18px"
  696. });
  697. }else{
  698. this.arrowNode.setStyles( {
  699. "top" : "auto",
  700. "bottom" : "-8px",
  701. "background-position": "0px -28px"
  702. });
  703. }
  704. var aw = this.arrowNode.getSize().x / 2 ;
  705. //var tw = targetCoondinates.width / 2 - aw;
  706. var w = Math.min( targetCoondinates.width , nodeSize.x )/ 2 - aw;
  707. var radiusDv = 0; //圆角和箭头偏移量的差值
  708. var radius = 0; //圆角值
  709. if( arrowX == "center" ) {
  710. this.arrowNode.setStyles({
  711. "left": (nodeSize.x/2 - aw - arrowOffsetX )+"px",
  712. "right": "auto"
  713. })
  714. }else if( arrowX == "left" ){
  715. radius = this.node.getStyle("border-"+arrowY+"-left-radius");
  716. radius = radius ? parseInt( radius ) : 0;
  717. if( radius > w ){
  718. radiusDv = radius - w;
  719. }
  720. this.arrowNode.setStyles({
  721. "left" : w + radiusDv - arrowOffsetX + "px",
  722. "right" : "auto"
  723. })
  724. }else{
  725. radius = this.node.getStyle("border-" + arrowY + "-right-radius");
  726. radius = radius ? parseInt(radius) : 0;
  727. if( radius > w ){
  728. radiusDv = radius - w;
  729. }
  730. this.arrowNode.setStyles({
  731. "left" : "auto",
  732. "right" : w + radiusDv + arrowOffsetX +"px"
  733. })
  734. }
  735. var l = left;
  736. if( radiusDv ){
  737. if( arrowX == "left" ){
  738. l = l - radiusDv;
  739. }else if( arrowX == "right" ){
  740. l = l + radiusDv;
  741. }
  742. }
  743. }
  744. node.setStyles({
  745. "left" : l || left,
  746. "top" : top
  747. });
  748. this.fireEvent( "postSetCoondinates", [arrowX, arrowY] );
  749. },
  750. setPosition : function(){
  751. if( this.options.axis == "x" ){
  752. this.setPosition_x();
  753. }else{
  754. this.setPosition_y();
  755. }
  756. },
  757. setPosition_x : function(){
  758. var top, left;
  759. var targetCoondinates = this.target ? this.target.getCoordinates( this.container ) : this.targetCoordinates ;
  760. var node = this.node;
  761. var nodeSize = node.getSize();
  762. var offsetX = (parseFloat(this.options.offset.x).toString() !== "NaN") ? parseFloat(this.options.offset.x) : 0;
  763. offsetX += this.options.hasArrow ? 10 : 0;
  764. if( this.positionX === "left" ){
  765. left = targetCoondinates.left - nodeSize.x - offsetX;
  766. }else if( this.positionX === "bottom" ){
  767. left = targetCoondinates.right + offsetX;
  768. }
  769. if( this.positionY === "top" ){
  770. top = targetCoondinates.top - nodeSize.y;
  771. }else if( this.positionY === "bottom" ){
  772. top = targetCoondinates.bottom;
  773. }else if( this.positionX === "middle" ){
  774. top = targetCoondinates.top + (targetCoondinates.height/2) - ( nodeSize.y / 2 )
  775. }
  776. node.setStyles({
  777. "left" : left,
  778. "top" : top
  779. });
  780. },
  781. setPosition_y : function(){
  782. var top, left;
  783. var targetCoondinates = this.target ? this.target.getCoordinates( this.container ) : this.targetCoordinates ;
  784. var node = this.node;
  785. var nodeSize = node.getSize();
  786. var offsetY = (parseFloat(this.options.offset.y).toString() !== "NaN") ? parseFloat(this.options.offset.y) : 0;
  787. offsetY += this.options.hasArrow ? 10 : 0;
  788. if( this.positionY === "top" ){
  789. top = targetCoondinates.top - nodeSize.y - offsetY;
  790. }else if( this.positionY === "bottom" ){
  791. top = targetCoondinates.bottom + offsetY;
  792. }
  793. if( this.positionX === "left" ){
  794. left = targetCoondinates.left - nodeSize.x;
  795. }else if( this.positionX === "right" ){
  796. left = targetCoondinates.right;
  797. }else if( this.positionX === "center" ){
  798. left = targetCoondinates.left + (targetCoondinates.width/2) - ( nodeSize.x / 2 )
  799. }
  800. node.setStyles({
  801. "left" : left,
  802. "top" : top
  803. });
  804. },
  805. destroy: function(){
  806. //if( this.options.event == "click" && this.node ){
  807. // this.container.removeEvent("mousedown",this.hideFun );
  808. //}
  809. if( this.options.event == "click" && this.app && this.hideFun_resize ){
  810. this.app.removeEvent("resize",this.hideFun_resize );
  811. }
  812. if( this.targetClickFun )this.target.removeEvent( "click", this.targetClickFun );
  813. if( this.targetMouseenterFun )this.target.removeEvent( "mouseenter", this.targetMouseenterFun );
  814. if( this.targetMouseenterFun2 )this.target.removeEvent( "mouseenter", this.targetMouseenterFun2 );
  815. if( this.targetMouseleaveFun )this.target.removeEvent( "mouseleave", this.targetMouseleaveFun );
  816. if( this.targetMouseleaveFun2 )this.target.removeEvent( "mouseleave", this.targetMouseleaveFun2 );
  817. if( this.node ){
  818. this.node.destroy();
  819. this.node = null;
  820. }
  821. this.fireEvent("destroy",[this]);
  822. MWF.release(this);
  823. },
  824. _getHtml : function(){
  825. //var data = this.data;
  826. //var titleStyle = "font-size:14px;color:#333";
  827. //var valueStyle = "font-size:14px;color:#666;padding-right:20px";
  828. //var persons = [];
  829. //data.invitePersonList.each( function( p ){
  830. // persons.push(p.split("@")[0] )
  831. //}.bind(this));
  832. //
  833. //var html =
  834. // "<div style='overflow: hidden;padding:15px 20px 20px 10px;height:16px;line-height:16px;'>" +
  835. // " <div style='font-size: 12px;color:#666; float: right'>"+ this.lp.applyPerson +":" + data.applicant.split("@")[0] +"</div>" +
  836. // " <div style='font-size: 16px;color:#333;float: left'>"+ this.lp.meetingDetail +"</div>"+
  837. // "</div>"+
  838. // "<div style='font-size: 18px;color:#333;padding:0px 10px 15px 20px;'>"+ data.subject +"</div>"+
  839. // "<div style='height:1px;margin:0px 20px;border-bottom:1px solid #ccc;'></div>"+
  840. // "<table width='100%' bordr='0' cellpadding='7' cellspacing='0' style='margin:13px 13px 13px 13px;'>" +
  841. // "<tr><td style='"+titleStyle+"' width='70'>"+this.lp.beginTime+":</td>" +
  842. // " <td style='"+valueStyle+"'>" + data.startTime + "</td></tr>" +
  843. // "<tr><td style='"+titleStyle+"'>"+this.lp.endTime+":</td>" +
  844. // " <td style='"+valueStyle+"'>" + data.completedTime + "</td></tr>" +
  845. // "<tr><td style='"+titleStyle+"'>"+this.lp.selectRoom +":</td>" +
  846. // " <td style='"+valueStyle+"' item='meetingRoom'></td></tr>" +
  847. // "<tr><td style='"+titleStyle+"'>"+this.lp.invitePerson2+":</td>" +
  848. // " <td style='"+valueStyle+"' item='invitePerson'>"+persons.join(",")+"</td></tr>" +
  849. // "<tr><td style='"+titleStyle+"'>"+this.lp.meetingDescription+":</td>" +
  850. // " <td style='"+valueStyle+"'>"+ data.description +"</td></tr>" +
  851. // "<tr><td style='"+titleStyle+"'>"+this.lp.meetingAttachment+":</td>" +
  852. // " <td style='"+valueStyle+"' item='attachment'></td></tr>"+
  853. // "</table>";
  854. return "";
  855. },
  856. _customNode : function( node, contentNode ){
  857. },
  858. _setContent : function( contentNode ){
  859. this.contentNode = contentNode;
  860. if( this.node ){
  861. this.node.empty();
  862. this.contentNode.inject( this.node );
  863. }
  864. },
  865. _loadCustom : function( callback ){
  866. if(callback)callback();
  867. }
  868. });
  869. MWF.xApplication.Template = MWF.xApplication.Template || {};
  870. MWF.xApplication.Template.MTooltips = MTooltips;