Tablet.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. o2.widget = o2.widget || {};
  2. o2.xDesktop.requireApp("Template", "widget.ColorPicker", null, false);
  3. o2.widget.Tablet = o2.Tablet = new Class({
  4. Implements: [Options, Events],
  5. Extends: o2.widget.Common,
  6. options: {
  7. "style": "default",
  8. "path": o2.session.path+"/widget/$Tablet/",
  9. "contentWidth" : 0, //绘图区域宽度,不制定则基础 this.node的宽度
  10. "contentHeight" : 0, //绘图区域高度,不制定则基础 this.node的高度 - 操作条高度
  11. "lineWidth" : 1, //铅笔粗细
  12. "color" : "#000000", //画笔颜色
  13. tools : [
  14. "save", "|",
  15. "undo",
  16. "redo", "|",
  17. "size",
  18. "color", "|",
  19. "reset",//,
  20. //"clear" //橡皮
  21. "cancel"
  22. ],
  23. "description" : "", //描述文字
  24. "action" : null, //uploadImage方法的上传服务,可选,如果不设置,使用公共图片服务
  25. "method": "", //使用action 的方法
  26. "parameter": {}, //action 时的url参数
  27. "data": null, //formdata 的data
  28. "reference": "", //uploadImage方法的使用 使用公共图片服务上传时的参数
  29. "referenceType" : "", //使用公共图片服务上传时的参数, 目前支持 processPlatformJob, processPlatformForm, portalPage, cmsDocument, forumDocument
  30. "resultMaxSize" : 0 //使用 reference 时有效
  31. },
  32. initialize: function(node, options, app){
  33. this.node = node;
  34. this.app = app;
  35. this.reset();
  36. this.setOptions(options);
  37. this.path = this.options.path || (o2.session.path+"/widget/$Tablet/");
  38. this.cssPath = this.path + this.options.style+"/css.wcss";
  39. this.lp = {
  40. save : "保存",
  41. reset : "清空",
  42. undo : "撤销",
  43. redo : "重做",
  44. size : "粗细",
  45. color : "颜色",//,
  46. //clear : "橡皮"
  47. "cancel": "取消"
  48. };
  49. this._loadCss();
  50. this.fireEvent("init");
  51. },
  52. load: function( ){
  53. //存储当前表面状态数组-上一步
  54. this.preDrawAry = [];
  55. //存储当前表面状态数组-下一步
  56. this.nextDrawAry = [];
  57. //中间数组
  58. this.middleAry = [];
  59. this.container = new Element("div.container", {
  60. styles : this.css.container
  61. }).inject(this.node);
  62. this.loadToolBar();
  63. this.contentNode = new Element("div.contentNode", { styles : this.css.contentNode}).inject(this.container);
  64. this.contentNode.addEvent("selectstart", function(e){
  65. e.preventDefault();
  66. e.stopPropagation();
  67. });
  68. this.loadDescription();
  69. this.setContentSize();
  70. if( this.checkBroswer() ){
  71. this.loadContent();
  72. }
  73. //this.imageNode = new Element("img",{
  74. //}).inject(this.contentNode);
  75. //this.imageNode.setStyles({
  76. // "display" : "none"
  77. //});
  78. if( this.app ){
  79. this.resizeFun = this.setContentSize.bind(this);
  80. this.app.addEvent( "resize", this.resizeFun );
  81. }
  82. },
  83. loadDescription : function(){
  84. if( this.options.description ){
  85. this.descriptionNode = new Element("div",{
  86. "styles": this.css.descriptionNode,
  87. "text": this.options.description
  88. }).inject( this.container )
  89. }
  90. },
  91. setContentSize : function(){
  92. var nodeSize = this.node.getSize();
  93. this.contentWidth = this.options.contentWidth || nodeSize.x;
  94. if( this.contentWidth < 100 )this.contentWidth = 100;
  95. this.contentNode.setStyle("width", this.contentWidth );
  96. if( this.options.contentHeight ){
  97. this.contentHeight = this.options.contentHeight;
  98. }else{
  99. var toolbarSize = this.toolbarNode ? this.toolbarNode.getSize() : { x : 0, y : 0 };
  100. var descriptionSize = this.descriptionNode ? this.descriptionNode.getSize() : { x : 0, y : 0 };
  101. var toolbarMargin = this.toolbarNode ? this.toolbarNode.getStyles("margin-top", "margin-bottom", "padding-top", "padding-bottom", "bordrt-top-width", "bordrt-bottom-width") : null;
  102. var m1 = (toolbarMargin) ? toolbarMargin["margin-top"].toInt()+toolbarMargin["margin-bottom"].toInt()+
  103. toolbarMargin["padding-bottom"].toInt()+toolbarMargin["padding-top"].toInt()+
  104. (toolbarMargin["bordrt-top-width"].toInt() || 0)+(toolbarMargin["bordrt-bottom-width"].toInt() || 0) : 0;
  105. var descriptionMargin = this.descriptionNode ? this.descriptionNode.getStyles("margin-top", "margin-bottom", "padding-top", "padding-bottom", "bordrt-top-width", "bordrt-bottom-width") : null;
  106. var m2 = (descriptionMargin) ? descriptionMargin["margin-top"].toInt()+descriptionMargin["margin-bottom"].toInt()+
  107. descriptionMargin["padding-bottom"].toInt()+descriptionMargin["padding-top"].toInt()+
  108. (descriptionMargin["bordrt-top-width"].toInt() || 0)+(descriptionMargin["bordrt-bottom-width"].toInt() || 0) : 0;
  109. var contentMargin = this.contentNode.getStyles("margin-top", "margin-bottom", "padding-top", "padding-bottom", "bordrt-top-width", "bordrt-bottom-width");
  110. var m3 = contentMargin["margin-top"].toInt()+contentMargin["margin-bottom"].toInt()+
  111. contentMargin["padding-bottom"].toInt()+contentMargin["padding-top"].toInt()+
  112. (contentMargin["bordrt-top-width"].toInt() || 0)+(contentMargin["bordrt-bottom-width"].toInt() || 0);
  113. this.contentHeight = nodeSize.y - toolbarSize.y - descriptionSize.y - m1 - m2 - m3;
  114. }
  115. if( this.contentHeight < 150 )this.contentHeight = 150;
  116. this.contentNode.setStyle("height", this.contentHeight );
  117. if( this.canvas ){
  118. var d = this.ctx.getImageData(0,0,this.canvas.clientWidth,this.canvas.clientHeight);
  119. this.canvas.set("width", this.contentWidth );
  120. this.canvas.set("height", this.contentHeight );
  121. this.ctx.putImageData(d,0,0);
  122. }
  123. },
  124. loadToolBar: function(){
  125. this.toolbarNode = new Element("div.toolbar", {
  126. "styles" : this.css.toolbar
  127. }).inject(this.container);
  128. this.toolbar = new o2.widget.Tablet.Toolbar( this , this.toolbarNode );
  129. this.toolbar.load();
  130. },
  131. loadContent : function( ){
  132. this.canvas = new Element("canvas", {
  133. width : this.contentWidth,
  134. height : this.contentHeight
  135. }).inject( this.contentNode );
  136. this.ctx = this.canvas.getContext("2d");
  137. var preData=this.ctx.getImageData(0,0,this.contentWidth,this.contentHeight);
  138. //空绘图表面进栈
  139. this.middleAry.push(preData);
  140. this.canvas.ontouchstart = this.canvas.onmousedown = function(ev){
  141. var ev = ev || event;
  142. var ctx = this.ctx;
  143. var canvas = this.canvas;
  144. var container = this.contentNode;
  145. var position = this.contentNode.getPosition();
  146. var doc = $(document);
  147. //ctx.strokeStyle="#0000ff" 线条颜色; 默认 #000000
  148. if( this.options.color )ctx.strokeStyle= this.currentColor || this.options.color; // 线条颜色; 默认 #000000
  149. if( this.options.lineWidth )ctx.lineWidth= this.currentWidth || this.options.lineWidth; //默认1 像素
  150. ctx.beginPath();
  151. ctx.moveTo(ev.clientX-position.x,ev.clientY-position.y);
  152. //当前绘图表面状态
  153. var preData= ctx.getImageData(0,0,this.contentWidth,this.contentHeight);
  154. //当前绘图表面进栈
  155. this.preDrawAry.push(preData);
  156. var mousemove = function(ev){
  157. ctx.lineTo(ev.client.x - position.x,ev.client.y - position.y);
  158. ctx.stroke();
  159. };
  160. doc.addEvent( "mousemove", mousemove );
  161. doc.addEvent( "touchmove", mousemove );
  162. var mouseup = function(ev){
  163. //document.onmousemove = document.onmouseup = null;
  164. doc.removeEvent("mousemove", mousemove);
  165. doc.removeEvent("mouseup", mouseup);
  166. doc.removeEvent("touchmove", mousemove);
  167. doc.removeEvent("touchend", mouseup);
  168. //当前绘图表面状态
  169. var preData= ctx.getImageData(0,0,this.contentWidth,this.contentHeight);
  170. if( this.nextDrawAry.length==0){
  171. //当前绘图表面进栈
  172. this.middleAry.push(preData);
  173. }else{
  174. this.middleAry=[];
  175. this.middleAry=this.middleAry.concat(this.preDrawAry);
  176. this.middleAry.push(preData);
  177. this.nextDrawAry=[];
  178. this.toolbar.enableItem("redo");
  179. }
  180. if(this.preDrawAry.length){
  181. this.toolbar.enableItem("undo");
  182. this.toolbar.enableItem("reset");
  183. }
  184. ctx.closePath();
  185. }.bind(this);
  186. doc.addEvent("mouseup", mouseup);
  187. doc.addEvent("touchend", mouseup);
  188. //document.onmouseup = function(ev){
  189. // document.onmousemove = document.onmouseup = null;
  190. // ctx.closePath();
  191. //}
  192. }.bind(this)
  193. },
  194. uploadImage: function( success, failure ){
  195. var image = this.getImage();
  196. if( image ){
  197. if( this.options.action ){
  198. this.action = (typeOf(this.options.action)=="string") ? o2.Actions.get(action).action : this.options.action;
  199. this.action.invoke({
  200. "name": this.options.method,
  201. "async": true,
  202. "data": this.getFormData( image ),
  203. "file": image,
  204. "parameter": this.options.parameter,
  205. "success": function(json){
  206. success(json)
  207. }.bind(this)
  208. });
  209. }else if( this.options.reference && this.options.referenceType ){
  210. //公共图片上传服务
  211. var maxSize = this.options.resultMaxSize;
  212. o2.xDesktop.uploadImageByScale(
  213. this.options.reference,
  214. this.options.referenceType,
  215. maxSize,
  216. this.getFormData( image ),
  217. image,
  218. success,
  219. failure
  220. );
  221. }
  222. }else{
  223. }
  224. },
  225. getFormData : function( image ){
  226. if( !image )image = this.getImage();
  227. var formData = new FormData();
  228. formData.append('file', image, this.fileName );
  229. if( this.options.data ){
  230. Object.each(this.options.data, function(v, k){
  231. formData.append(k, v)
  232. });
  233. }
  234. return formData;
  235. },
  236. getImage : function( base64Code ){
  237. var src = base64Code || this.getBase64Code();
  238. src=window.atob(src);
  239. var ia = new Uint8Array(src.length);
  240. for (var i = 0; i < src.length; i++) {
  241. ia[i] = src.charCodeAt(i);
  242. }
  243. return new Blob([ia], {type: this.fileType });
  244. },
  245. getBase64Code : function(){
  246. var ctx = this.ctx;
  247. var canvas = this.canvas;
  248. //var container = this.contentNode;
  249. //var size = this.options.size;
  250. var width, height;
  251. //if( this.options.resultMaxSize ){
  252. // if( this.contentWidth > )
  253. //}else{
  254. // width = this.contentWidth;
  255. // height = this.contentHeight
  256. //}
  257. width = this.contentWidth;
  258. height = this.contentHeight;
  259. //ctx.drawImage(this.imageNode,0,0, this.contentWidth,this.contentHeight,0,0,width,height);
  260. var src=canvas.toDataURL( this.fileType );
  261. src=src.split(',')[1];
  262. if(!src){
  263. return "";
  264. }else{
  265. return src
  266. }
  267. },
  268. getBase64Image: function( base64Code ){
  269. if( !base64Code )base64Code = this.getBase64Code();
  270. if( !base64Code )return null;
  271. return 'data:'+ this.fileType +';base64,' + base64Code;
  272. },
  273. close : function(){
  274. this.container.destroy();
  275. delete this;
  276. },
  277. checkBroswer : function(){
  278. if( window.Uint8Array && window.HTMLCanvasElement && window.atob && window.Blob){
  279. this.available = true;
  280. return true;
  281. }else{
  282. this.available = false;
  283. this.container.set("html", "<p>您的浏览器不支持以下特性:</p><ul><li>canvas</li><li>Blob</li><li>Uint8Array</li><li>FormData</li><li>atob</li></ul>");
  284. return false;
  285. }
  286. },
  287. save : function(){
  288. var base64code = this.getBase64Code();
  289. var imageFile = this.getImage( base64code );
  290. var base64Image = this.getBase64Image( base64code );
  291. this.fireEvent("save", [ base64code, base64Image, imageFile]);
  292. },
  293. reset : function( itemNode ){
  294. this.fileName = "untitled.png";
  295. this.fileType = "image/png";
  296. if( this.ctx ){
  297. var canvas = this.canvas;
  298. this.ctx.clearRect(0,0,canvas.clientWidth,canvas.clientHeight);
  299. }
  300. },
  301. undo : function( itemNode ){
  302. if(this.preDrawAry.length>0){
  303. var popData=this.preDrawAry.pop();
  304. var midData=this.middleAry[this.preDrawAry.length+1];
  305. this.nextDrawAry.push(midData);
  306. this.ctx.putImageData(popData,0,0);
  307. }
  308. this.toolbar.setAllItemsStatus();
  309. },
  310. redo : function( itemNode ){
  311. if(this.nextDrawAry.length){
  312. var popData=this.nextDrawAry.pop();
  313. var midData=this.middleAry[this.middleAry.length-this.nextDrawAry.length-2];
  314. this.preDrawAry.push(midData);
  315. this.ctx.putImageData(popData,0,0);
  316. }
  317. this.toolbar.setAllItemsStatus();
  318. },
  319. size : function( itemNode ){
  320. if( !this.sizeSelector ){
  321. this.sizeSelector = new o2.widget.Tablet.SizePicker(this.container, itemNode, null, {}, {
  322. "onSelect": function (width) {
  323. this.currentWidth = width;
  324. }.bind(this)
  325. });
  326. }
  327. },
  328. color : function( itemNode ){
  329. if( !this.colorSelector ){
  330. this.colorSelector = new o2.xApplication.Template.widget.ColorPicker( this.container, itemNode, null, {}, {
  331. "lineWidth" : 1,
  332. "onSelect": function (color) {
  333. this.currentColor = color;
  334. }.bind(this)
  335. });
  336. }
  337. },
  338. clear : function( itemNode ){
  339. },
  340. cancel: function(){
  341. this.reset();
  342. this.fireEvent("cancel");
  343. }
  344. });
  345. o2.widget.Tablet.Toolbar = new Class({
  346. Implements: [Options, Events],
  347. initialize: function (tablet, container) {
  348. this.tablet = tablet;
  349. this.container = container;
  350. this.css = tablet.css;
  351. this.lp = this.tablet.lp;
  352. this.imagePath = o2.session.path+"/widget/$Tablet/"+ this.tablet.options.style +"/icon/";
  353. this.items = {};
  354. this.itemsEnableFun = {
  355. save : {
  356. enable : function(){ return true }
  357. },
  358. reset : {
  359. enable : function(){ return this.tablet.preDrawAry.length > 0}.bind(this)
  360. },
  361. undo : {
  362. enable : function(){ return this.tablet.preDrawAry.length > 0 }.bind(this)
  363. },
  364. redo : {
  365. enable : function(){ return this.tablet.nextDrawAry.length > 0 }.bind(this)
  366. },
  367. size : {
  368. enable : function(){ return true }
  369. },
  370. color : {
  371. enable : function(){ return true }
  372. }
  373. }
  374. },
  375. getHtml : function(){
  376. var items;
  377. var tools = this.tablet.options.tools;
  378. if( tools ){
  379. items = tools;
  380. }else{
  381. items = [
  382. "save", "|",
  383. "reset", "|",
  384. "undo", "|",
  385. "redo", "|",
  386. "size", "|",
  387. "color"//, "|",
  388. //"clear" //橡皮
  389. ];
  390. }
  391. var html = "";
  392. var style = "toolItem";
  393. items.each( function( item ){
  394. switch( item ){
  395. case "|":
  396. html += "<div styles='" + "separator" + "'></div>";
  397. break;
  398. case "save" :
  399. html += "<div item='save' styles='" + style + "'>"+ this.lp.save +"</div>";
  400. break;
  401. case "reset" :
  402. html += "<div item='reset' styles='" + style + "'>"+ this.lp.reset +"</div>";
  403. break;
  404. case "undo" :
  405. html += "<div item='undo' styles='" + style + "'>"+ this.lp.undo +"</div>";
  406. break;
  407. case "redo" :
  408. html += "<div item='redo' styles='" + style + "'>"+ this.lp.redo +"</div>";
  409. break;
  410. case "size" :
  411. html += "<div item='size' styles='" + style + "'>"+ this.lp.size +"</div>";
  412. break;
  413. case "color" :
  414. html += "<div item='color' styles='" + style + "'>"+ this.lp.color +"</div>";
  415. break;
  416. case "clear" :
  417. html += "<div item='clear' styles='" + style + "'>"+ this.lp.clear +"</div>";
  418. break;
  419. case "cancel" :
  420. html += "<div item='cancel' styles='toolRightItem'>"+ this.lp.cancel +"</div>";
  421. break;
  422. }
  423. }.bind(this));
  424. return html;
  425. },
  426. load: function () {
  427. var _self = this;
  428. var imagePath = this.imagePath;
  429. this.items = {};
  430. var html = this.getHtml();
  431. this.container.set("html", html);
  432. this.container.getElements("[styles]").each(function (el) {
  433. el.setStyles(_self.css[el.get("styles")]);
  434. var item = el.get("item");
  435. if ( item ) {
  436. this.items[ item ] = el;
  437. el.setStyle("background-image","url("+ imagePath + item +"_normal.png)");
  438. el.addEvents({
  439. mouseover : function(){
  440. _self._setItemNodeActive(this.el);
  441. }.bind({ item : item, el : el }),
  442. mouseout : function(){
  443. _self._setItemNodeNormal(this.el);
  444. }.bind({ item : item, el : el }),
  445. click : function( ev ){
  446. if( _self["tablet"][this.item] )_self["tablet"][this.item]( this.el );
  447. }.bind({ item : item, el : el })
  448. });
  449. if( item == "color" || item == "size" ){
  450. if( _self["tablet"][item] )_self["tablet"][item]( el );
  451. }
  452. }
  453. }.bind(this));
  454. this.setAllItemsStatus();
  455. },
  456. setAllItemsStatus : function(){
  457. for( var item in this.items ){
  458. var node = this.items[item];
  459. if( this.itemsEnableFun[item] ){
  460. if( this.itemsEnableFun[item].enable() ){
  461. this.enableItem( item )
  462. }else{
  463. this.disableItem( item );
  464. }
  465. }
  466. }
  467. },
  468. disableItem : function( itemName ){
  469. var itemNode = this.items[ itemName ]; //this.container.getElement("[item='+itemName+']");
  470. itemNode.store("status", "disable");
  471. this._setItemNodeDisable( itemNode );
  472. },
  473. enableItem : function( itemName ){
  474. var itemNode = this.items[ itemName ];
  475. itemNode.store("status", "enable");
  476. this._setItemNodeNormal( itemNode );
  477. },
  478. _setItemNodeDisable : function( itemNode ){
  479. var item = itemNode.get("item");
  480. itemNode.setStyles( this.css.toolItem_disable );
  481. itemNode.setStyle("background-image","url("+ this.imagePath+ item +"_disable.png)");
  482. },
  483. _setItemNodeActive: function( itemNode ){
  484. if( itemNode.retrieve("status") == "disable" )return;
  485. var item = itemNode.get("item");
  486. itemNode.setStyles( this.css.toolItem_over );
  487. itemNode.setStyle("background-image","url("+ this.imagePath+ item +"_active.png)");
  488. },
  489. _setItemNodeNormal: function( itemNode ){
  490. if( itemNode.retrieve("status") == "disable" )return;
  491. var item = itemNode.get("item");
  492. var style = itemNode.get("styles");
  493. itemNode.setStyles( this.css[style] );
  494. itemNode.setStyle("background-image","url("+ this.imagePath+ item +"_normal.png)");
  495. }
  496. });
  497. o2.xDesktop.requireApp("Template", "MTooltips", null, false);
  498. o2.widget.Tablet.SizePicker = new Class({
  499. Implements: [Options, Events],
  500. Extends: MTooltips,
  501. options: {
  502. style : "default",
  503. axis: "y", //箭头在x轴还是y轴上展现
  504. position : { //node 固定的位置
  505. x : "auto", //x 轴上left center right, auto 系统自动计算
  506. y : "auto" //y轴上top middle bottom, auto 系统自动计算
  507. },
  508. //event : "click", //事件类型,有target 时有效, mouseenter对应mouseleave,click 对应 container 的 click
  509. nodeStyles : {
  510. "min-width" : "260px"
  511. },
  512. lineWidth : 1
  513. },
  514. initialize : function( container, target, app, data, options, targetCoordinates ){
  515. //可以传入target 或者 targetCoordinates,两种选一
  516. //传入target,表示触发tooltip的节点,本类根据 this.options.event 自动绑定target的事件
  517. //传入targetCoordinates,表示 出发tooltip的位置,本类不绑定触发事件
  518. if( options ){
  519. this.setOptions(options);
  520. }
  521. this.container = container;
  522. this.target = target;
  523. this.targetCoordinates = targetCoordinates;
  524. this.app = app;
  525. if(app)this.lp = app.lp;
  526. this.data = data;
  527. if( this.target ){
  528. this.setTargetEvents();
  529. }
  530. },
  531. _customNode : function( node ){
  532. o2.UD.getDataJson("sizePicker", function(json) {
  533. this.rulerContainer = new Element("div",{
  534. styles : {
  535. "margin-left": " 23px",
  536. "margin-right": " 1px",
  537. "width" : "228px"
  538. }
  539. }).inject(this.node);
  540. this.ruleList = ["0.1","0.5","1","5","10", "15","20"];
  541. this.rulerTitleContainer = new Element("div",{
  542. styles : { "overflow" : "hidden" }
  543. }).inject( this.rulerContainer );
  544. this.ruleList.each( function( rule ){
  545. new Element("div", {
  546. text : rule,
  547. styles : {
  548. width : "32px",
  549. float : "left",
  550. "text-align" : "center"
  551. }
  552. }).inject( this.rulerTitleContainer )
  553. }.bind(this));
  554. this.rulerContentContainer = new Element("div",{
  555. styles : { "overflow" : "hidden" }
  556. }).inject( this.rulerContainer );
  557. new Element("div", {
  558. styles : {
  559. width : "14px",
  560. height : "10px",
  561. "text-align" : "center",
  562. float : "left",
  563. "border-right" : "1px solid #aaa"
  564. }
  565. }).inject( this.rulerContentContainer );
  566. this.ruleList.each( function( rule, i ){
  567. if( i == this.ruleList.length - 1 )return;
  568. new Element("div", {
  569. styles : {
  570. width : "32px",
  571. height : "10px",
  572. "text-align" : "center",
  573. float : "left",
  574. "border-right" : "1px solid #aaa"
  575. }
  576. }).inject( this.rulerContentContainer )
  577. }.bind(this));
  578. this.silderContainer = new Element("div", {
  579. "height" : "25px",
  580. "line-height" : "25px",
  581. "margin-top" : "4px"
  582. }).inject( this.node );
  583. this.sliderArea = new Element("div", {styles : {
  584. "margin-top": "2px",
  585. "margin-bottom": "10px",
  586. "height": "10px",
  587. "overflow": " hidden",
  588. "margin-left": " 37px",
  589. "margin-right": " 15px",
  590. "border-top": "1px solid #999",
  591. "border-left": "1px solid #999",
  592. "border-bottom": "1px solid #E1E1E1",
  593. "border-right": "1px solid #E1E1E1",
  594. "background-color": "#EEE",
  595. "width" : "200px"
  596. }}).inject( this.silderContainer );
  597. this.sliderKnob = new Element("div", {styles : {
  598. "height": "8px",
  599. "width": " 8px",
  600. "background-color": "#999",
  601. "z-index": " 99",
  602. "border-top": "1px solid #DDD",
  603. "border-left": "1px solid #DDD",
  604. "border-bottom": "1px solid #777",
  605. "border-right": "1px solid #777",
  606. "cursor": "pointer"
  607. } }).inject( this.sliderArea );
  608. this.slider = new Slider(this.sliderArea, this.sliderKnob, {
  609. range: [1, 30],
  610. initialStep: 10,
  611. onChange: function(value){
  612. if( value < 10 ){
  613. this.lineWidth = (value / 10)
  614. }else{
  615. this.lineWidth = value - 9;
  616. }
  617. this.drawPreview( this.lineWidth );
  618. this.fireEvent("select", this.lineWidth )
  619. }.bind(this)
  620. });
  621. previewContainer = new Element("div").inject(this.node);
  622. new Element("div",{ text : "预览", styles : {
  623. "float" : "left",
  624. "margin-top" : "5px",
  625. "width" : "30px"
  626. }}).inject(this.silderContainer);
  627. this.previewNode = new Element("div", {
  628. styles : {
  629. "margin" : "0px 0px 0px 37px",
  630. "width" : "200px"
  631. }
  632. }).inject( this.node );
  633. this.canvas = new Element("canvas", {
  634. width : 200,
  635. height : 30
  636. }).inject( this.previewNode );
  637. this.ctx = this.canvas.getContext("2d");
  638. this.drawPreview();
  639. new Element("button", {
  640. text : "重置",
  641. type : "button",
  642. styles :{
  643. "margin-left" : "40px",
  644. "font-size" : "12px",
  645. "border-radius" : "3px",
  646. "cursor" : "pointer" ,
  647. "border" : "1px solid #ccc",
  648. "padding" : "5px 10px",
  649. "background-color" : "#f7f7f7"
  650. },
  651. events : {
  652. click : function(){
  653. this.lineWidth = this.options.lineWidth || 1;
  654. var step;
  655. if( this.lineWidth < 1 ){
  656. step = this.lineWidth * 10
  657. }else{
  658. step = this.lineWidth + 9
  659. }
  660. this.slider.set( parseInt( step ) );
  661. this.drawPreview( this.lineWidth );
  662. this.fireEvent("select", this.lineWidth )
  663. }.bind(this)
  664. }
  665. }).inject( this.node );
  666. }.bind(this));
  667. //this.resultInput = new Element("input").inject(this.contentNode);
  668. },
  669. drawPreview : function( lineWidth ){
  670. if( !lineWidth )lineWidth = this.options.lineWidth || 1;
  671. var canvas = this.canvas;
  672. var ctx = this.ctx;
  673. ctx.clearRect(0,0,canvas.clientWidth,canvas.clientHeight);
  674. var coordinates = this.previewNode.getCoordinates();
  675. var doc = $(document);
  676. ctx.strokeStyle="#000000"; //线条颜色; 默认 #000000
  677. //ctx.strokeStyle= this.currentColor || this.options.color; // 线条颜色; 默认 #000000
  678. ctx.lineWidth= lineWidth ; //默认1 像素
  679. ctx.beginPath();
  680. //ctx.moveTo( (coordinates.bottom-coordinates.top - lineWidth ) / 2, coordinates.left);
  681. ctx.moveTo( 1 , 15 );
  682. ctx.lineTo( 200, 15 );
  683. ctx.stroke();
  684. }
  685. });