4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.form.Signature
14 * @extends Roo.form.Field
18 * @param {Object} config Configuration options
21 Roo.form.Signature = function(config){
22 Roo.form.Signature.superclass.constructor.call(this, config);
24 this.addEvents({// not in used??
27 * Fires when the 'confirm' icon is pressed (add a listener to enable add button)
28 * @param {Roo.form.Signature} combo This combo box
33 * Fires when the 'edit' icon is pressed (add a listener to enable add button)
34 * @param {Roo.form.ComboBox} combo This combo box
35 * @param {Roo.data.Record|false} record The data record returned from the underlying store (or false on nothing selected)
41 Roo.extend(Roo.form.Signature, Roo.form.Field, {
43 * @cfg {Object} labels Label to use when rendering a form.
55 * @cfg {Number} width The signature panel width (defaults to 300)
59 * @cfg {Number} height The signature panel height (defaults to 100)
63 * @cfg {Boolean} allowBlank False to validate that the value length > 0 (defaults to false)
68 // {Object} signPanel The signature SVG panel element (defaults to {})
70 // {Boolean} isMouseDown False to validate that the mouse down event (defaults to false)
72 // {Boolean} isConfirmed validate the signature is confirmed or not for submitting form (defaults to false)
74 // {String} signatureTmp SVG mapping string (defaults to empty string)
78 defaultAutoCreate : { // modified by initCompnoent..
84 onRender : function(ct, position){
86 Roo.form.Signature.superclass.onRender.call(this, ct, position);
88 this.wrap = this.el.wrap({
89 cls:'x-form-signature-wrap', style : 'width: ' + this.width + 'px', cn:{cls:'x-form-signature'}
92 this.createToolbar(this);
93 this.signPanel = this.wrap.createChild({
95 style: 'width: ' + this.width + 'px; height: ' + this.height + 'px; border: 0;'
99 this.svgID = Roo.id();
100 this.svgEl = this.signPanel.createChild({
101 xmlns : 'http://www.w3.org/2000/svg',
103 id : this.svgID + "-svg",
106 viewBox: '0 0 '+this.width+' '+this.height,
110 id: this.svgID + "-svg-r",
117 id: this.svgID + "-svg-l",
119 y1: (this.height*0.8), // start set the line in 80% of height
120 x2: this.width, // end
121 y2: (this.height*0.8), // end set the line in 80% of height
124 'stroke-dasharray': "3",
125 'shape-rendering': "crispEdges",
126 'pointer-events': "none"
130 id: this.svgID + "-svg-p",
134 'pointer-events': 'none'
139 this.svgBox = this.svgEl.dom.getScreenCTM();
141 createSVG : function(){
142 var svg = this.signPanel;
143 var r = svg.select('#'+ this.svgID + '-svg-r', true).first().dom;
146 r.addEventListener('mousedown', function(e) { return t.down(e); }, false);
147 r.addEventListener('mousemove', function(e) { return t.move(e); }, false);
148 r.addEventListener('mouseup', function(e) { return t.up(e); }, false);
149 r.addEventListener('mouseout', function(e) { return t.up(e); }, false);
150 r.addEventListener('touchstart', function(e) { return t.down(e); }, false);
151 r.addEventListener('touchmove', function(e) { return t.move(e); }, false);
152 r.addEventListener('touchend', function(e) { return t.up(e); }, false);
155 isTouchEvent : function(e){
156 return e.type.match(/^touch/);
158 getCoords : function (e) {
159 var pt = this.svgEl.dom.createSVGPoint();
162 if (this.isTouchEvent(e)) {
163 pt.x = e.targetTouches[0].clientX;
164 pt.y = e.targetTouches[0].clientY;
166 var a = this.svgEl.dom.getScreenCTM();
168 var mx = pt.matrixTransform(b);
169 return mx.x + ',' + mx.y;
171 //mouse event headler
172 down : function (e) {
173 this.signatureTmp += 'M' + this.getCoords(e) + ' ';
174 this.signPanel.select('#'+ this.svgID + '-svg-p', true).first().attr('d', this.signatureTmp);
176 this.isMouseDown = true;
180 move : function (e) {
181 if (this.isMouseDown) {
182 this.signatureTmp += 'L' + this.getCoords(e) + ' ';
183 this.signPanel.select('#'+ this.svgID + '-svg-p', true).first().attr( 'd', this.signatureTmp);
189 this.isMouseDown = false;
190 var sp = this.signatureTmp.split(' ');
193 if(!sp[sp.length-2].match(/^L/)){
197 this.signatureTmp = sp.join(" ");
200 if(this.getValue() != this.signatureTmp){
201 this.signPanel.select('#'+ this.svgID + '-svg-r', true).first().attr('fill', '#ffa');
202 this.isConfirmed = false;
208 * Protected method that will not generally be called directly. It
209 * is called when the editor creates its toolbar. Override this method if you need to
210 * add custom toolbar buttons.
211 * @param {HtmlEditor} editor
213 createToolbar : function(editor){
214 function btn(id, toggle, handler){
215 var xid = fid + '-'+ id ;
219 cls : 'x-btn-icon x-edit-'+id,
220 enableToggle:toggle !== false,
221 scope: editor, // was editor...
222 handler:handler||editor.relayBtnCmd,
223 clickEvent:'mousedown',
224 tooltip: etb.buttonTips[id] || undefined, ///tips ???
230 var tb = new Roo.Toolbar(editor.wrap.dom.firstChild);
234 cls : ' x-signature-btn x-signature-'+id,
235 scope: editor, // was editor...
237 clickEvent:'mousedown',
238 text: this.labels.clear
245 cls : ' x-signature-btn x-signature-'+id,
246 scope: editor, // was editor...
247 handler: this.confirmHandler,
248 clickEvent:'mousedown',
249 text: this.labels.confirm
256 * when user is clicked confirm then show this image.....
258 * @return {String} Image Data URI
260 getImageDataURI : function(){
261 var svg = this.svgEl.dom.parentNode.innerHTML;
262 var src = 'data:image/svg+xml;base64,'+window.btoa(svg);
267 * @return {Boolean} this.isConfirmed
269 getConfirmed : function(){
270 return this.isConfirmed;
274 * @return {Number} this.width
276 getWidth : function(){
281 * @return {Number} this.height
283 getHeight : function(){
287 getSignature : function(){
288 return this.signatureTmp;
292 this.signatureTmp = '';
293 this.signPanel.select('#'+ this.svgID + '-svg-r', true).first().attr('fill', '#ffa');
294 this.signPanel.select('#'+ this.svgID + '-svg-p', true).first().attr( 'd', '');
295 this.isConfirmed = false;
296 Roo.form.Signature.superclass.reset.call(this);
298 setSignature : function(s){
299 this.signatureTmp = s;
300 this.signPanel.select('#'+ this.svgID + '-svg-r', true).first().attr('fill', '#ffa');
301 this.signPanel.select('#'+ this.svgID + '-svg-p', true).first().attr( 'd', s);
303 this.isConfirmed = false;
304 Roo.form.Signature.superclass.reset.call(this);
307 // Roo.log(this.signPanel.dom.contentWindow.up())
310 setConfirmed : function(){
314 // Roo.log(Roo.get(this.signPanel.dom.contentWindow.r).attr('fill', '#cfc'));
317 confirmHandler : function(){
318 if(!this.getSignature()){
322 this.signPanel.select('#'+ this.svgID + '-svg-r', true).first().attr('fill', '#cfc');
323 this.setValue(this.getSignature());
324 this.isConfirmed = true;
326 this.fireEvent('confirm', this);
329 // Subclasses should provide the validation implementation by overriding this
330 validateValue : function(value){
335 if(this.isConfirmed){