utils.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. (function(Trex) {
  2. /**
  3. * @namespace
  4. * @name Trex.Util
  5. */
  6. Trex.Util = /** @lends Trex.Util */ {
  7. _dispElIds: [],
  8. getDispElId: function() {
  9. var _genId;
  10. do {
  11. _genId = "tx_entry_" + (Math.floor(Math.random() * 90000) + 10000) + "_"; //id: 10000~99999
  12. } while(Trex.Util._dispElIds.contains(_genId));
  13. Trex.Util._dispElIds.push(_genId);
  14. return _genId;
  15. },
  16. generateKey: function() {
  17. return parseInt(Math.random() * 100000000);
  18. },
  19. toStyleString: function(styles) {
  20. var _str = [];
  21. for(var _name in styles) {
  22. if(styles[_name]) {
  23. _str.push(_name.replace(/([A-Z])/g, "-$1").toLowerCase());
  24. _str.push(":");
  25. _str.push(styles[_name]);
  26. _str.push(";");
  27. }
  28. }
  29. return _str.join("");
  30. },
  31. toAttrString: function(attrs) {
  32. var _str = [];
  33. for(var _name in attrs) {
  34. if(attrs[_name]) {
  35. _str.push(" " + _name + "=\"" + attrs[_name] + "\"");
  36. }
  37. }
  38. return _str.join("");
  39. },
  40. getMatchValue: function(reg, html, inx) {
  41. var _matchs;
  42. if((_matchs = reg.exec(html)) != _NULL) {
  43. return _matchs[inx];
  44. } else {
  45. return _NULL;
  46. }
  47. },
  48. getAttachmentType: function(mimeType){
  49. mimeType = (mimeType || "").toLowerCase();
  50. var imageMimeTypes = ['image/jpg', 'image/jpeg', 'image/png', 'image/tiff',
  51. 'image/gif', 'image/bmp', 'image/x-jg', 'image/ief', 'image/pict',
  52. 'jpg', 'bmp', 'gif', 'png'];
  53. if (imageMimeTypes.contains(mimeType)) {
  54. return 'image';
  55. }
  56. return 'file';
  57. },
  58. /**
  59. * 확장자에 따는 thumbnail 이미지 url을 가져온다.
  60. * @param {Object} ext
  61. */
  62. thumburl: function(ext) {
  63. ext = (ext || "").toLowerCase();
  64. switch (ext) {
  65. case "doc":
  66. case "docx":
  67. return getIconPath("#iconpath/pn_word.gif");
  68. case "xls":
  69. case "xlsx":
  70. return getIconPath("#iconpath/pn_xls.gif");
  71. case "ppt":
  72. case "pptx":
  73. return getIconPath("#iconpath/pn_ppt.gif");
  74. case "pdf":
  75. return getIconPath("#iconpath/pn_pdf.gif");
  76. case "txt":
  77. return getIconPath("#iconpath/pn_txt.gif");
  78. case "hwp":
  79. return getIconPath("#iconpath/pn_hwp.gif");
  80. case "zip":
  81. case "alz":
  82. return getIconPath("#iconpath/pn_zip.gif");
  83. case "mp3":
  84. case "wav":
  85. case "ogg":
  86. case "wma":
  87. case "mp4":
  88. case "ape":
  89. case "ra":
  90. case "ram":
  91. return getIconPath("#iconpath/pn_mp3.gif");
  92. case "avi":
  93. case "mpeg":
  94. case "wmv":
  95. case "asf":
  96. return getIconPath("#iconpath/pn_movie.gif");
  97. case "swf":
  98. return getIconPath("#iconpath/pn_swf.gif");
  99. case "htm" :
  100. case "html":
  101. return getIconPath("#iconpath/pn_html.gif");
  102. case "jpg":
  103. case "gif":
  104. case "png":
  105. case "bmp":
  106. return getIconPath("#iconpath/pn_etc.gif");
  107. default:
  108. return getIconPath("#iconpath/pn_etc.gif");
  109. }
  110. },
  111. /**
  112. * 확장자에 따는 preview 이미지 url을 가져온다.
  113. * @param {Object} ext
  114. */
  115. prevurl: function(ext) {
  116. ext = (ext || "").toLowerCase();
  117. switch (ext) {
  118. case "doc":
  119. case "docx":
  120. return getIconPath("#iconpath/p_word_s.gif");
  121. case "xls":
  122. case "xlsx":
  123. return getIconPath("#iconpath/p_xls_s.gif");
  124. case "ppt":
  125. case "pptx":
  126. return getIconPath("#iconpath/p_ppt_s.gif");
  127. case "pdf":
  128. return getIconPath("#iconpath/p_pdf_s.gif");
  129. case "txt":
  130. return getIconPath("#iconpath/p_txt_s.gif");
  131. case "hwp":
  132. return getIconPath("#iconpath/p_hwp_s.gif");
  133. case "zip":
  134. case "alz":
  135. return getIconPath("#iconpath/p_zip_s.gif");
  136. case "mp3":
  137. case "wav":
  138. case "ogg":
  139. case "wma":
  140. case "mp4":
  141. case "ape":
  142. case "ra":
  143. case "ram":
  144. return getIconPath("#iconpath/p_mp3_s.gif");
  145. case "avi":
  146. case "mpeg":
  147. case "wmv":
  148. case "asf":
  149. return getIconPath("#iconpath/p_movie_s.gif");
  150. case "swf":
  151. return getIconPath("#iconpath/p_swf_s.gif");
  152. case "htm" :
  153. case "html":
  154. return getIconPath("#iconpath/p_html_s.gif");
  155. case "jpg":
  156. return getIconPath("#iconpath/p_jpg_s.gif");
  157. case "gif":
  158. return getIconPath("#iconpath/p_gif_s.gif");
  159. case "png":
  160. case "bmp":
  161. return getIconPath("#iconpath/p_png_s.gif");
  162. default:
  163. return getIconPath("#iconpath/p_etc_s.gif");
  164. }
  165. },
  166. getMatchedClassName: function(element, classes){
  167. var matched = _FALSE;
  168. var _class = "";
  169. for(var i = 0; i < classes.length; i++){
  170. _class = classes[i];
  171. if($tx.hasClassName(element, _class)){
  172. matched = _class;
  173. break;
  174. }
  175. }
  176. return matched;
  177. },
  178. getAllAttributesFromEmbed: function(embedSrc){
  179. var map = {};
  180. embedSrc = embedSrc.replace(/<embed|>/ig,"");
  181. try {
  182. var regSplit = /(\w+)=((?:\")[^\"]+(?:\"|$)|(?:')[^']+(?:'|$)|(?:[^\"'][^ \n]+($| |\n)))/ig;
  183. var result;
  184. while( (result = regSplit.exec(embedSrc)) != _NULL ){
  185. map[result[1].trim().toLowerCase()] = result[2].replace(/^(\"|')/i,"").replace(/(\"|')$/i,"").trim();
  186. }
  187. }catch(e){ }
  188. return map;
  189. },
  190. getAllAttributes: function(source){
  191. var _map = {};
  192. var _matchsAttr;
  193. var _reg = /style="(?:\s*|(?:[^"]*(?:;\s*)))width\s*:\s*([0-9]+)px[^"]*"/ig;
  194. while ((_matchsAttr = _reg.exec(source)) != _NULL) {
  195. _map["width"] = _matchsAttr[1];
  196. }
  197. _reg = /style="(?:\s*|(?:[^"]*(?:;\s*)))height\s*:\s*([0-9]+)px[^"]*"/ig;
  198. while ((_matchsAttr = _reg.exec(source)) != _NULL) {
  199. _map["height"] = _matchsAttr[1];
  200. }
  201. _reg = new RegExp("\\s+([a-zA-Z\-]+)=\"([^\"]*)\"", "g");
  202. while ((_matchsAttr = _reg.exec(source)) != _NULL) {
  203. if (!_map[_matchsAttr[1].toLowerCase()]) {
  204. _map[_matchsAttr[1].toLowerCase()] = _matchsAttr[2];
  205. }
  206. }
  207. _reg = new RegExp("\\s+([a-zA-Z\-]+)='([^']*)'", "g");
  208. while ((_matchsAttr = _reg.exec(source)) != _NULL) {
  209. if (!_map[_matchsAttr[1].toLowerCase()]) {
  210. _map[_matchsAttr[1].toLowerCase()] = _matchsAttr[2];
  211. }
  212. }
  213. _reg = new RegExp("\\s+([a-zA-Z\-]+)=([^\\s>]*)", "g");
  214. while ((_matchsAttr = _reg.exec(source)) != _NULL) {
  215. if (!_map[_matchsAttr[1].toLowerCase()]) {
  216. _map[_matchsAttr[1].toLowerCase()] = _matchsAttr[2];
  217. }
  218. }
  219. return _map;
  220. }
  221. };
  222. /**
  223. * @namespace
  224. * @name Trex.HtmlCreator
  225. */
  226. Trex.HtmlCreator = {
  227. /**
  228. * Create Table Markup String
  229. *
  230. * @example
  231. * var items =[
  232. * {
  233. * klass: 'klassName',
  234. * image: 'image url', // can be omitted
  235. * data: 'data'
  236. * }
  237. * ]
  238. *
  239. * var tableMarkup = Trex.HtmlCreator.createTableMarkup(row, col, item);
  240. *
  241. * @param {int} rows
  242. * @param {int} cols
  243. * @param {Object} items
  244. *
  245. */
  246. createTableMarkup: function(rows, cols, items){
  247. var _html = [];
  248. _html.push('<table unselectable="on">');
  249. _html.push('<tbody>');
  250. var _total = items.length;
  251. var _item;
  252. for (var row = 0; row < rows; row++) {
  253. _html.push('<tr>');
  254. for (var col = 0; col < cols; col++) {
  255. if (row * cols + col < _total) {
  256. _item = items[row * cols + col];
  257. if (_item.image) {
  258. var imageUrl = TrexConfig.getIconPath(_item.image); //글상자 > 직접선택 > 선스타일 이미지.
  259. _html.push('<td class="tx-menu-list-item"><a href="javascript:;"><span class="' + (_item.klass || '') + '"><img src="' + imageUrl + '" data="' + _item.data + '"/></span></a></td>');
  260. } else {
  261. _html.push('<td class="tx-menu-list-item"><a href="javascript:;"><span class="' + (_item.klass || '') + '">' + _item.data + '</span></a></td>');
  262. }
  263. } else {
  264. _html.push('<td class="tx-menu-list-item"><a href="javascript:;"><span class="">&nbsp;</span></a></td>');
  265. }
  266. }
  267. _html.push('</tr>');
  268. }
  269. _html.push('</tbody>');
  270. _html.push('</table>');
  271. return _html.join("\n");
  272. }
  273. };
  274. Trex.String = {
  275. escapeQuot: function(str) {
  276. return str.replace(new RegExp('"', "g"), "&quot;").replace(new RegExp("'", "g"), "&#39;");
  277. },
  278. unescapeQuot: function(str) {
  279. return str.replace(new RegExp("&quot;", "gi"), '"').replace(new RegExp("&#39;", "g"), "'");
  280. },
  281. htmlspecialchars: function(str) {
  282. return Trex.String.escapeQuot(str.replace(new RegExp("&", "g"), "&amp;").replace(new RegExp("<", "g"), "&lt;").replace(new RegExp(">", "g"), "&gt;"));
  283. },
  284. unHtmlspecialchars: function(str) {
  285. return Trex.String.unescapeQuot(str.replace(new RegExp("&amp;", "gi"), "&").replace(new RegExp("&lt;", "gi"), "<").replace(new RegExp("&gt;", "gi"), ">"));
  286. },
  287. parseAttribute: function(elStr, attrName){
  288. var regAttribute1 = new RegExp("(^|\\W)" + attrName + '="([^"]*)"', "gi");
  289. var regAttribute2 = new RegExp("(^|\\W)" + attrName + "='([^']*)'", "gi");
  290. var regAttribute3 = new RegExp("(^|\\W)" + attrName + "=([^\\s>]*)", "gi");
  291. var result;
  292. if (result = regAttribute1.exec(elStr)) {
  293. return result[2];
  294. }else if (result = regAttribute2.exec(elStr)) {
  295. return result[2];
  296. }else if (result = regAttribute3.exec(elStr)) {
  297. return result[2];
  298. }else {
  299. return "";
  300. }
  301. },
  302. changeAttribute: function(elStr, attrName, currentValue, value ){
  303. var regAttribute1 = new RegExp("(^|\\W)(" + attrName + '=")' + currentValue + '(")', "gi");
  304. var regAttribute2 = new RegExp("(^|\\W)(" + attrName + "=')" + currentValue + "(')", "gi");
  305. var regAttribute3 = new RegExp("(^|\\W)(" + attrName + "=)"+currentValue, "gi");
  306. var regAttribute4 = new RegExp("<([\\w]+\\s*)", "gi");
  307. var _exists = _FALSE;
  308. if (elStr.search(regAttribute1) > -1) {
  309. _exists = _TRUE;
  310. elStr = elStr.replace(regAttribute1, "$1$2"+value+"$3");
  311. }
  312. if (elStr.search(regAttribute2) > -1) {
  313. _exists = _TRUE;
  314. elStr = elStr.replace(regAttribute2, "$1$2"+value+"$3");
  315. }
  316. if (elStr.search(regAttribute3) > -1) {
  317. _exists = _TRUE;
  318. elStr = elStr.replace(regAttribute3, "$1$2"+value);
  319. }
  320. if(!_exists) {
  321. elStr = elStr.replace(regAttribute4, "<$1" + attrName + '=' + value + ' ');
  322. }
  323. return elStr;
  324. }
  325. };
  326. /*---- Trex.Validator ------------------------------------------------------*/
  327. Trex.Validator = Trex.Class.create({
  328. initialize: function() { },
  329. strip: function(content) {
  330. return content.stripTags().replace(/&nbsp;/g, "").replace(Trex.__WORD_JOINER_REGEXP, "").trim();
  331. },
  332. exists: function(content) {
  333. if(!content) {
  334. return _FALSE;
  335. }
  336. if(this.strip(content) == "") {
  337. if(content.search(/<(img|iframe|object|embed|table|hr|script|TXDB)/i) < 0) {
  338. return _FALSE;
  339. }
  340. }
  341. return _TRUE;
  342. },
  343. equals: function(content, text) {
  344. if(!content || !text) {
  345. return _FALSE;
  346. }
  347. if(content.search(/<(img|iframe|object|embed|table|hr|script|TXDB)/i) < 0) {
  348. if(this.strip(content) == this.strip(text)) {
  349. return _TRUE;
  350. }
  351. }
  352. return _FALSE;
  353. }
  354. });
  355. /*---- Trex.Repeater ------------------------------------------------------*/
  356. Trex.Repeater = Trex.Class.create({
  357. initialize: function(execHandler) {
  358. this.execHandler = execHandler;
  359. },
  360. start: function(term) {
  361. if(this.tItv) {
  362. this.clear();
  363. }
  364. this.tItv = _WIN.setInterval(this.onTimer.bind(this), term);
  365. },
  366. clear: function() {
  367. _WIN.clearInterval(this.tItv);
  368. this.tItv = _NULL;
  369. },
  370. onTimer: function() {
  371. if(this.execHandler != _NULL) {
  372. this.execHandler();
  373. }
  374. }
  375. });
  376. /*---- Trex.Timer ------------------------------------------------------*/
  377. Trex.Timer = Trex.Class.create({
  378. initialize: function(execHandler) {
  379. this.execHandler = execHandler;
  380. },
  381. start: function(term) {
  382. _WIN.setTimeout(this.onTimer.bind(this), term);
  383. },
  384. onTimer: function() {
  385. if(this.execHandler != _NULL) {
  386. this.execHandler();
  387. }
  388. }
  389. });
  390. /**
  391. * Trex.Paging Class
  392. * paging을 위한 class. Ajax나 fileter 등을 통한 dynamic data바인딩은 고려되지 않음. static array로만 사용이 가능
  393. * @class
  394. * @param {Array} data
  395. * @param {Object} config
  396. */
  397. Trex.Paging = Trex.Class.create({
  398. $const:{
  399. DEFAULT_PAGE_SIZE: 5,
  400. DEFAULT_BLOCK_SIZE:10
  401. },
  402. initialize: function(data, config ){
  403. this.data = data;
  404. this.currentpage = config.initPage || 1;
  405. this.totalrow = config.totalrow || this.getTotalRow();
  406. this.pagesize = config.pagesize || Trex.Paging.DEFAULT_PAGE_SIZE;
  407. this.blocksize = config.blocksize || Trex.Paging.DEFAULT_PAGE_SIZE;
  408. this.totalpage = Math.ceil( this.totalrow / this.pagesize );
  409. this.totalblock = Math.ceil( this.totalpage / this.blocksize );
  410. },
  411. getNextPage: function(){
  412. return (this.currentpage < this.totalpage)?this.currentpage+1:0;
  413. },
  414. getPrevPage: function(){
  415. return (this.currentpage > 1)?this.currentpage-1:0;
  416. },
  417. getNextBlock: function(){
  418. var _currentblock = Math.ceil(this.currentpage/this.blocksize);
  419. return ( _currentblock < this.totalblock)?_currentblock * this.blocksize + 1:0
  420. },
  421. getPrevBlock: function(){
  422. var _currentblock = Math.ceil(this.currentpage/this.blocksize);
  423. return (_currentblock > 1)?(_currentblock-2) * this.blocksize + 1:0;
  424. },
  425. getPageList: function(){
  426. var pages = [];
  427. var _startBlock = Math.ceil( this.currentpage / this.blocksize ) - 1;
  428. var _startPage = ( _startBlock * this.blocksize + 1 );
  429. var _endPage = Math.min( this.totalpage, (_startPage + this.blocksize - 1) );
  430. for ( var i = _startPage; i <= _endPage; i++ ){
  431. pages.push(i);
  432. }
  433. return pages;
  434. },
  435. movePage: function( page ){
  436. this.currentpage = page || this.currentpage;
  437. },
  438. getOnePageData: function(){
  439. var result = [];
  440. var _start = (this.currentpage-1) * this.pagesize;
  441. var _end = Math.min( this.currentpage * this.pagesize, this.totalrow ) ;
  442. for( var i = _start; i < _end; i++ ){
  443. result.push( this.data[i] );
  444. }
  445. return result;
  446. },
  447. getTotalRow: function(){
  448. return this.data.length;
  449. }
  450. });
  451. /**
  452. * Trex.Slidebar Class
  453. * slidebar 위젯. 마크업, CSS에 의존성이 있다.
  454. * @class
  455. * @param {Object} config
  456. */
  457. Trex.Slidebar = Trex.Class.create({
  458. initialize: function(config){
  459. /* config = {
  460. * handler: function, 슬라이드가 동작할때 실행될 함수
  461. * elContext: 슬라이드가 제어될 영역, div등의 element
  462. * knoWidth: knob element의 크기
  463. * barSize: 슬라이드 element의 크기
  464. * min: 최소값(논리적인 값, default 0)
  465. * max: 최대값(논리적인 값, default 100)
  466. * interval: 한번 클릭이나 마우스 드래그로 이동하는 값(논리적인 값, default 5)
  467. * defaultValue: 초기 knob이 위치할 값
  468. * }
  469. */
  470. this.elContext = config.el;
  471. this.knobWidth = config.knobWidth;
  472. this.isDisabled = _FALSE;
  473. this.handler = function(value){
  474. if (!this.isDisabled && typeof config.handler == "function") {
  475. config.handler(value);
  476. }
  477. };
  478. this.logicObj = {
  479. 'interval': config.interval || 5 ,
  480. 'min': config.min || 0,
  481. 'max': config.max || 100
  482. };
  483. this.physicObj = {
  484. 'min':0,
  485. 'width': config.barSize || 100
  486. };
  487. this.physicObj.max = this.physicObj.width - this.knobWidth;
  488. this.physicObj.interval = this.logicObj.interval * this.physicObj.max / this.logicObj.max;
  489. this.startPos = 0;
  490. this.startX = 0;
  491. this.isDrag = _FALSE;
  492. this.result = 0;
  493. var elMenu = $tom.collect( this.elContext, "dd.tx-slide" );
  494. // 양끝단에 min값과 max값이 표시 될 수도 있다.
  495. $tom.collect( elMenu, "span.tx-slide-min" ).innerHTML = "";
  496. $tom.collect( elMenu, "span.tx-slide-max" ).innerHTML = "";
  497. /* default 값 셋팅하는 부분이 필요하다? */
  498. this.bindEvent();
  499. this.setKnobPosition(config.defaultValue || config.min || 0);
  500. },
  501. regenerate: function( value ){
  502. value = parseInt(value * this.physicObj.width / this.logicObj.max);
  503. this.setKnobPosition(value);
  504. },
  505. bindEvent: function(){
  506. var elMenu = $tom.collect( this.elContext, "dd.tx-slide" );
  507. var elPrev = $tom.collect( elMenu, "a.tx-slide-prev" );
  508. var elNext = $tom.collect( elMenu, "a.tx-slide-next" );
  509. var elBar = $tom.collect( elMenu, "div.tx-slide-bar" );
  510. var elKnob = this.elKnob = $tom.collect( elMenu, "div.tx-slide-knob" );
  511. $tx.observe( elKnob, "mousedown", function(ev){
  512. this.isDrag = _TRUE;
  513. this.startPos = this.getKnobPosition();
  514. this.startX = ev.clientX;
  515. $tx.stop(ev);
  516. }.bind(this));
  517. $tx.observe( elKnob, "mouseup", function(){
  518. this.isDrag = _FALSE;
  519. }.bind(this));
  520. $tx.observe( this.elContext, "mousemove", function(ev){
  521. if ( this.isDrag ){
  522. this.setKnobPosition( this.startPos + ev.clientX - this.startX);
  523. $tx.stop(ev);
  524. this.handler( this.result );
  525. }
  526. }.bind(this));
  527. $tx.observe( elPrev, "click", function(ev){
  528. var count = Math.round(this.physicObj.interval) - 1;
  529. var that = this;
  530. var moveLeft = function(){
  531. var pos = that.getKnobPosition();
  532. that.setKnobPosition( pos - 1);
  533. if ( count-- > 0 ) {
  534. setTimeout(moveLeft, 10 );
  535. }else{
  536. that.handler(that.result);
  537. }
  538. };
  539. moveLeft();
  540. $tx.stop(ev);
  541. }.bind(this));
  542. $tx.observe( elNext, "click", function(ev){
  543. var count = Math.round(this.physicObj.interval);
  544. var that = this;
  545. var moveRight = function(){
  546. var pos = that.getKnobPosition();
  547. that.setKnobPosition( pos + 1);
  548. if ( --count > 0 ) {
  549. setTimeout(moveRight, 10 );
  550. }else{
  551. that.handler(that.result);
  552. }
  553. };
  554. moveRight();
  555. $tx.stop(ev);
  556. }.bind(this));
  557. $tx.observe( this.elContext, "mouseup", function(){
  558. if ( this.isDrag ) {
  559. this.isDrag = _FALSE;
  560. }
  561. }.bind(this));
  562. $tx.observe( elKnob, "click", function(ev){
  563. $tx.stop(ev);
  564. }.bind(this));
  565. $tx.observe( elBar, "click", function(ev){
  566. if ( !this.isDrag ) {
  567. var x = ev.layerX || ev.x;
  568. this.setKnobPosition( x - this.knobWidth / 2);
  569. this.handler( this.result );
  570. }
  571. }.bind(this));
  572. },
  573. getKnobPosition: function(){
  574. var pos = $tx.getStyle( this.elKnob, "left");
  575. return pos.parsePx();
  576. },
  577. setKnobPosition: function(value){
  578. value = (value < this.physicObj.max)?value:this.physicObj.max;
  579. value = (value > this.physicObj.min)?value:this.physicObj.min;
  580. $tx.setStyle( this.elKnob, {left: value.toPx()});
  581. this.result = Math.round( value * this.logicObj.interval / this.physicObj.interval );
  582. },
  583. setDisable: function(){
  584. this.isDisabled = _TRUE;
  585. },
  586. setEnable: function(){
  587. this.isDisabled = _FALSE;
  588. },
  589. getDisabled: function(){
  590. return this.isDisabled;
  591. }
  592. });
  593. /**
  594. * Trex.DynamicSizer Class
  595. * table의 가로세로 사이즈를 마우로 제어할 수 있는 위젯.
  596. * @class
  597. * @param {Object} config
  598. */
  599. Trex.DynamicSizer = Trex.Class.create({
  600. initialize: function(config){
  601. /* config = {
  602. * el: //다이나믹 사이저가 실릴 영역
  603. * clickHandler : 클릭됐을때
  604. * moveHandler: 사이즈가 변경됐을 때
  605. */
  606. this.config = config;
  607. this.wrapper = config.el;
  608. this.elEventContext = tx.div({className:"tx-dynamic-sizer-context"});
  609. this.currentSize = {row: 0, col: 0};
  610. this.dynamicSizingEnabled = _TRUE;
  611. if( !config.moveHandler ){
  612. config.moveHandler = function(){}
  613. }
  614. if( !config.clickHandler ){
  615. config.clickHandler = function(){}
  616. }
  617. this.wrapper.appendChild( this.elEventContext );
  618. this.previewTable = new Trex.DynamicSizer.PreviewTable({
  619. parentEl: this.elEventContext,
  620. mouseOverHandler: this.changeSize.bind(this),
  621. mouseClickHandler: this.selectSize.bind(this)
  622. });
  623. },
  624. clear: function(){
  625. this.dynamicSizingEnabled = _TRUE;
  626. this.changeSize(0,0);
  627. },
  628. changeSize: function(row, col){
  629. if (this.dynamicSizingEnabled) {
  630. this.currentSize.row = row;
  631. this.currentSize.col = col;
  632. this._changeSelectionSize(row, col);
  633. this.config.moveHandler(row, col);
  634. }
  635. },
  636. _changeSelectionSize: function(row, col){
  637. this.previewTable.moveSelectionPos(row, col);
  638. },
  639. toggleDynamicSizing: function(){
  640. this.dynamicSizingEnabled = !this.dynamicSizingEnabled;
  641. if ( this.dynamicSizingEnabled ){
  642. this.selection.enableResize();
  643. }else{
  644. this.selection.disableResize();
  645. }
  646. },
  647. selectSize:function(ev){
  648. this.config.clickHandler( ev, this.currentSize);
  649. },
  650. getCurruentSize: function(){
  651. return this.currentSize;
  652. }
  653. });
  654. Trex.DynamicSizer.PreviewTable = Trex.Class.create({
  655. $const:{
  656. DEFAULT_TD_STYLE:{
  657. },
  658. DEFAULT_TABLE_PROPERTY:{
  659. cellpadding: "0",
  660. cellspacing: "1"
  661. },
  662. MAX_SIZE: { COL:10, ROW:10 }
  663. },
  664. initialize: function(config){
  665. this.config = config;
  666. this.elTable = _NULL;
  667. this.elTable = this.generateTable("tx-event");
  668. this.elSelection = tx.div( {className:"tx-selection"}, this.generateTable("tx-selection") );
  669. var tablePanel = this.generateTable("tx-panel");
  670. this.eventBinding();
  671. config.parentEl.appendChild( this.elTable );
  672. config.parentEl.appendChild( this.elSelection );
  673. config.parentEl.appendChild( tablePanel );
  674. var pos = $tom.getPosition( this.elTable );
  675. var PROPERTY = Trex.DynamicSizer.PreviewTable.MAX_SIZE;
  676. this.cellSize = { width: Math.round((pos.width - pos.x) / PROPERTY.COL),
  677. height: (pos.height - pos.y) / PROPERTY.ROW }
  678. },
  679. generateTable: function(className){
  680. var tbody = tx.tbody();
  681. var PROPERTY = Trex.DynamicSizer.PreviewTable;
  682. for (var i = 0; i < PROPERTY.MAX_SIZE.ROW; i++) {
  683. var tr = tx.tr();
  684. for (var j = 0; j < PROPERTY.MAX_SIZE.COL; j++) {
  685. var td = tx.td(tx.div( {
  686. style: PROPERTY.DEFAULT_TD_STYLE
  687. }));
  688. td = this.setCoordToAttr(td, j+1, i+1);
  689. tr.appendChild(td);
  690. }
  691. tbody.appendChild(tr);
  692. }
  693. var table = tx.table(PROPERTY.DEFAULT_TABLE_PROPERTY);
  694. $tx.addClassName( table, className || "" );
  695. table.appendChild( tbody );
  696. return table;
  697. },
  698. moveSelectionPos: function(row,col){
  699. var width = ( col * this.cellSize.width).toPx();
  700. var height = ( row * this.cellSize.height).toPx();
  701. $tx.setStyle( this.elSelection, { width: width, height:height } );
  702. },
  703. setCoordToAttr: function(element, col, row){
  704. element.setAttribute("col", col);
  705. element.setAttribute("row", row);
  706. return element;
  707. },
  708. getCoordFromAttr: function(element){
  709. return {
  710. col: element.getAttribute("col") || 0,
  711. row: element.getAttribute("row") || 0
  712. }
  713. },
  714. eventBinding: function(){
  715. // 외부에서 받은 event핸들러들로 binding시킴
  716. this.mouseOverHandler = this.config.mouseOverHandler;
  717. this.mouseClickHandler = this.config.mouseClickHandler;
  718. var self = this;
  719. var _mouseOverHandler = function(ev){
  720. var element = $tx.element(ev) || {};
  721. var tagName = (element.tagName || "").toUpperCase();
  722. if (element && tagName == "TD" ) {
  723. var coord = self.getCoordFromAttr(element);
  724. self.mouseOverHandler(coord.row, coord.col);
  725. }
  726. $tx.stop(ev);
  727. };
  728. var _mouseClickHandler = function(ev){
  729. self.mouseClickHandler(ev);
  730. };
  731. $tx.observe(this.elTable, "mouseover", _mouseOverHandler);
  732. $tx.observe(this.elTable, "click", _mouseClickHandler);
  733. }
  734. });
  735. /*---- Trex.ImageScale ------------------------------------------------------*/
  736. Trex.ImageScale = Trex.Class.create({
  737. initialize: function(data, handler) {
  738. if(!data.imageurl) {
  739. return;
  740. }
  741. if(data.actualwidth) {
  742. return;
  743. }
  744. var _loadHandler = function(width, height) {
  745. data.actualwidth = width;
  746. data.actualheight = height;
  747. if(handler) {
  748. handler(width, height);
  749. }
  750. };
  751. setTimeout(function() {
  752. var _tmpImage = new Image();
  753. _tmpImage.onerror = function() {
  754. _tmpImage = _NULL;
  755. };
  756. if( _tmpImage.onreadystatechange ) { //IE
  757. _tmpImage.onreadystatechange = function() {
  758. if(this.readyState == "complete") {
  759. _loadHandler(this.width, this.height);
  760. _tmpImage = _NULL;
  761. }
  762. };
  763. } else {
  764. _tmpImage.onload = function() {
  765. _loadHandler(this.width, this.height);
  766. _tmpImage = _NULL;
  767. };
  768. }
  769. _tmpImage.src = data.imageurl;
  770. }, 10);
  771. }
  772. });
  773. function getIconPath(virtualPath) {
  774. var realPath = TrexConfig.getIconPath(virtualPath);
  775. return realPath + "";
  776. }
  777. })(Trex);