MTooltips.js 38 KB

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