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.ComboBox} 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.svgEl = this.signPanel.createChild({
100 xmlns : 'http://www.w3.org/2000/svg',
104 viewBox: '0 0 '+this.width+' '+this.height,
108 cls: "roo-signature-svg-r",
116 y1: (this.height*0.8), // start set the line in 80% of height
117 x2: this.width, // end
118 y2: (this.height*0.8), // end set the line in 80% of height
121 'stroke-dasharray': "3",
122 'shape-rendering': "crispEdges",
123 'pointer-events': "none"
127 cls: "roo-signature-svg-p",
131 'pointer-events': 'none'
136 this.svgBox = this.svgEl.dom.getScreenCTM();
138 createSVG : function(){
139 var svg = this.signPanel;
140 var r = svg.select('roo-signature-svg-r', true).first().dom;
143 r.addEventListener('mousedown', function(e) { return t.down(e); }, false);
144 r.addEventListener('mousemove', function(e) { return t.move(e); }, false);
145 r.addEventListener('mouseup', function(e) { return t.up(e); }, false);
146 r.addEventListener('mouseout', function(e) { return t.up(e); }, false);
147 r.addEventListener('touchstart', function(e) { return t.down(e); }, false);
148 r.addEventListener('touchmove', function(e) { return t.move(e); }, false);
149 r.addEventListener('touchend', function(e) { return t.up(e); }, false);
152 isTouchEvent : function(e){
153 return e.type.match(/^touch/);
155 getCoords : function (e) {
156 var pt = this.svgEl.dom.createSVGPoint();
159 if (this.isTouchEvent(e)) {
160 pt.x = e.targetTouches[0].clientX
161 pt.y = e.targetTouches[0].clientY;
163 var a = this.svgEl.dom.getScreenCTM();
165 var mx = pt.matrixTransform(b);
166 return mx.x + ',' + mx.y;
168 //mouse event headler
169 down : function (e) {
170 this.signatureTmp += 'M' + this.getCoords(e) + ' ';
171 this.signPanel.select('roo-signature-svg-p', true).first().dom.setAttributeNS('http://www.w3.org/2000/svg','d', this.signatureTmp);
173 this.isMouseDown = true;
177 move : function (e) {
178 if (this.isMouseDown) {
179 this.signatureTmp += 'L' + this.getCoords(e) + ' ';
180 this.signPanel.select('roo-signature-svg-p', true).first().dom.setAttributeNS('http://www.w3.org/2000/svg', 'd', this.signatureTmp);
186 this.isMouseDown = false;
187 var sp = this.signatureTmp.split(' ');
190 if(!sp[sp.length-2].match(/^L/)){
194 this.signatureTmp = sp.join(" ");
197 if(this.getValue() != this.signatureTmp){
198 this.signPanel.select('roo-signature-svg-r', true).first().attr('fill', '#ffa');
199 this.isConfirmed = false;
205 * Protected method that will not generally be called directly. It
206 * is called when the editor creates its toolbar. Override this method if you need to
207 * add custom toolbar buttons.
208 * @param {HtmlEditor} editor
210 createToolbar : function(editor){
211 function btn(id, toggle, handler){
212 var xid = fid + '-'+ id ;
216 cls : 'x-btn-icon x-edit-'+id,
217 enableToggle:toggle !== false,
218 scope: editor, // was editor...
219 handler:handler||editor.relayBtnCmd,
220 clickEvent:'mousedown',
221 tooltip: etb.buttonTips[id] || undefined, ///tips ???
227 var tb = new Roo.Toolbar(editor.wrap.dom.firstChild);
231 cls : ' x-signature-btn x-signature-'+id,
232 scope: editor, // was editor...
234 clickEvent:'mousedown',
235 text: this.labels.clear
242 cls : ' x-signature-btn x-signature-'+id,
243 scope: editor, // was editor...
244 handler: this.setConfirmed,
245 clickEvent:'mousedown',
246 text: this.labels.confirm
254 * @return {String} Image Data URI
256 getImageDataURI : function(){
257 var svg = this.svgEl.dom.outerHTML;
258 var src = 'data:image/svg+xml;base64,'+window.btoa(svg);
263 * @return {Boolean} this.isConfirmed
265 getConfirmed : function(){
266 return this.isConfirmed;
269 getSignature : function(){
270 return this.signatureTmp;
274 this.signatureTmp = '';
275 this.signPanel.select('roo-signature-svg-r', true).first().attr('fill', '#ffa');
276 this.signPanel.select('roo-signature-svg-p', true).first().dom.setAttributeNS('http://www.w3.org/2000/svg', 'd', '');
277 this.isConfirmed = false;
278 Roo.form.Signature.superclass.reset.call(this);
281 // Roo.log(this.signPanel.dom.contentWindow.up())
284 setConfirmed : function(){
285 if(!this.getSignature()){
288 this.signPanel.select('#svg-r', true).first().attr('fill', '#cfc');
289 this.setValue(this.getSignature());
290 this.isConfirmed = true;
291 // Roo.log(Roo.get(this.signPanel.dom.contentWindow.r).attr('fill', '#cfc'));
294 // Subclasses should provide the validation implementation by overriding this
295 validateValue : function(value){
300 if(this.isConfirmed){