ruby-extjs 2.0.3 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,423 @@
1
+ String.leftPad = function (d, b, c) {
2
+ var a = String(d);
3
+ if (!c) {
4
+ c = " "
5
+ }
6
+ while (a.length < b) {
7
+ a = c + a
8
+ }
9
+ return a
10
+ };
11
+ Ext.define('Ext.ux.TextMaskCore', {
12
+ constructor : function (mask, money) {
13
+ this.money = money === true;
14
+ this.setMask(mask);
15
+ },
16
+ blankChar : '_',
17
+ money : false,
18
+ moneyZeros : 0,
19
+ moneyPrecision : 0,
20
+ version : '2.6',
21
+ specialChars : {
22
+ 'L' : /^[A-Z]$/,
23
+ 'l' : /^[a-z]$/,
24
+ '9' : /^[0-9]$/,
25
+ 'A' : /^[A-Za-z]$/,
26
+ '_' : /^.$/
27
+ },
28
+ mask : function (v) {
29
+ return this.money ? this.maskMoney(v) : this.maskNormal(v);
30
+ },
31
+ maskNormal : function (v) {
32
+ v = this.unmask(v);
33
+ v = v.split('');
34
+ var m = '';
35
+ var i = 0;
36
+ Ext.each(this.maskList, function (item) {
37
+ if (Ext.isString(item)) {
38
+ m += item;
39
+ } else {
40
+ if (v[i] && item.test(v[i])) {
41
+ m += v[i];
42
+ } else {
43
+ m += this.blankChar;
44
+ };
45
+ i++;
46
+ }
47
+ }, this);
48
+ return m;
49
+ },
50
+ maskMoney : function (v) {
51
+ v = String(this.unmask(v));
52
+ var negativo = false;
53
+ if (v.indexOf('-') >= 0) {
54
+ negativo = true;
55
+ v = v.replace(new RegExp('\[-\]', 'g'), '');
56
+ };
57
+ if (Math.round(v) !== v) {
58
+ v = Math.round(Number(Ext.num(v, 0)) * Number('1' + String.leftPad('', this.moneyPrecision, '0')));
59
+ };
60
+ v = String.leftPad(Number(Ext.num(v, 0)), this.moneyZeros, '0');
61
+ v = v.split('');
62
+ var m = '';
63
+ var i = v.length - 1;
64
+ var mi = this.maskList.length - 1;
65
+ while (i >= 0) {
66
+ var item = this.maskList[mi];
67
+ if (mi >= 0) {
68
+ if (Ext.isString(item)) {
69
+ m = item + m;
70
+ } else {
71
+ if (v[i] && item.test(v[i])) {
72
+ m = v[i] + m;
73
+ } else {
74
+ m = '0' + m;
75
+ };
76
+ i--;
77
+ };
78
+ mi--;
79
+ } else {
80
+ if (this.specialChars['9'].test(v[i])) {
81
+ m = v[i] + m;
82
+ };
83
+ i--;
84
+ }
85
+ };
86
+ if (this.textMask.indexOf('#') >= 0) {
87
+ m = this.textMask.slice(0, this.textMask.indexOf('#')) + (negativo ? '-' : '') + m;
88
+ };
89
+ return m;
90
+ },
91
+ unmask : function (v) {
92
+ v = v === undefined ? '' : v;
93
+ return this.money ? this.unmaskMoney(v) : this.unmaskNormal(v);
94
+ },
95
+ unmaskNormal : function (v) {
96
+ v = String(v);
97
+ var specialChars = '';
98
+ Ext.iterate(this.specialChars, function (k) {
99
+ specialChars += k;
100
+ });
101
+ var chars = this.textMask.replace(new RegExp('\[' + specialChars + '\]', 'g'), '');
102
+ v = v.replace(new RegExp('\[' + chars + '\]', 'g'), '');
103
+ v = v.split('');
104
+ var m = '';
105
+ var i = 0;
106
+ Ext.each(this.maskList, function (item) {
107
+ if (!Ext.isString(item)) {
108
+ if (v[i] && item.test(v[i])) {
109
+ m += v[i];
110
+ };
111
+ i++;
112
+ }
113
+ }, this);
114
+ return m;
115
+ },
116
+ unmaskMoney : function (v) {
117
+ v = String(v);
118
+ if (v.indexOf('+') >= 0) {
119
+ v = v.replace(new RegExp('\[-\]', 'g'), '');
120
+ };
121
+ var negativo = v.indexOf('-') >= 0;
122
+ var precision = v.lastIndexOf('.');
123
+ if (precision === -1) {
124
+ precision = 0;
125
+ } else {
126
+ precision = v.length - precision - 1;
127
+ };
128
+ if (precision > this.moneyPrecision) {
129
+ v = v.slice(0, - (precision - this.moneyPrecision));
130
+ precision = this.moneyPrecision;
131
+ };
132
+ var specialChars = '';
133
+ Ext.iterate(this.specialChars, function (k) {
134
+ specialChars += k;
135
+ });
136
+ var chars = this.textMask.replace(new RegExp('\[' + specialChars + '\]', 'g'), '');
137
+ v = v.replace(new RegExp('\[' + chars + '\]', 'g'), '');
138
+ v = v.split('');
139
+ var m = '';
140
+ var i = v.length - 1;
141
+ var mi = this.maskList.length - 1;
142
+ while (i >= 0) {
143
+ if (mi >= 0) {
144
+ var item = this.maskList[mi];
145
+ if (!Ext.isString(item)) {
146
+ if (v[i] && item.test(v[i])) {
147
+ m = v[i] + m;
148
+ };
149
+ i--;
150
+ };
151
+ mi--;
152
+ } else {
153
+ if (v[i] && this.specialChars['9'].test(v[i])) {
154
+ m = v[i] + m;
155
+ };
156
+ i--;
157
+ }
158
+ };
159
+ m = this.parsePrecision(m, precision);
160
+ if (negativo) {
161
+ m = '-' + m;
162
+ };
163
+ return String(m);
164
+ },
165
+ parsePrecision : function (v, precision) {
166
+ v = String(v);
167
+ var sinal = v.indexOf('-') >= 0 ? '-' : '';
168
+ v = v + String.leftPad('', this.moneyPrecision - precision, '0');
169
+ if (this.moneyPrecision > 0) {
170
+ v = String.leftPad(v, this.moneyPrecision + 1, '0');
171
+ return sinal + String(Ext.num(v.slice(0, -this.moneyPrecision), 0)) + '.' + v.slice(-this.moneyPrecision);
172
+ } else {
173
+ return sinal + v;
174
+ }
175
+ },
176
+ parseMask : function (mask) {
177
+ var regList = [];
178
+ if (this.money) {
179
+ this.moneyZeros = 0;
180
+ while (mask.indexOf('0') >= 0) {
181
+ mask = mask.replace('0', '9');
182
+ this.moneyZeros++;
183
+ };
184
+ this.moneyPrecision = Math.min(mask.length - Math.max(mask.lastIndexOf('.'), mask.lastIndexOf(',')) - 1, mask.length);
185
+ };
186
+ Ext.each(mask.match(/<![^<][^!]*!>/g), function (exp) {
187
+ regList.push(new RegExp('^' + exp.replace(/(<!)|(!>)/g, '') + '$', ''));
188
+ });
189
+ mask = mask.replace(/<![^<][^!]*!>/g, '?');
190
+ this.textMask = mask;
191
+ if (this.money) {
192
+ mask = mask.slice(mask.indexOf('#') + 1);
193
+ };
194
+ this.maskList = [];
195
+ var regI = 0;
196
+ var maskArr = mask.split('');
197
+ for (var i = 0; i < maskArr.length; i++) {
198
+ if (maskArr[i] === '?') {
199
+ this.maskList.push(regList[regI]);
200
+ regI++;
201
+ } else {
202
+ this.maskList.push(this.specialChars[maskArr[i]] || maskArr[i]);
203
+ }
204
+ };
205
+ return this.maskList;
206
+ },
207
+ getLength : function (v) {
208
+ v = this.mask(v);
209
+ var i = v.indexOf(this.blankChar);
210
+ if (i === -1) {
211
+ i = v.length;
212
+ };
213
+ return i;
214
+ },
215
+ setMask : function (mask) {
216
+ if (!Ext.isEmpty(mask) && mask !== this.oldMask) {
217
+ this.oldMkask = mask;
218
+ this.parseMask(mask);
219
+ } else if (Ext.isEmpty(this.oldMask)) {
220
+ this.parseMask('');
221
+ };
222
+ return this;
223
+ }
224
+ });
225
+ Ext.util.Format.TextMask = new Ext.ux.TextMaskCore();
226
+ Ext.util.Format.MoneyMask = new Ext.ux.TextMaskCore('', true);
227
+ Ext.util.Format.maskRenderer = function (mask, money) {
228
+ return function (v) {
229
+ Ext.util.Format.TextMask.money = money;
230
+ return Ext.util.Format.TextMask.setMask(mask).mask(v);
231
+ }
232
+ };
233
+ Ext.require(['Ext.ux.TextMaskCore']);
234
+ Ext.define('Ext.ux.TextMaskPlugin', {
235
+ extend : 'Ext.AbstractPlugin',
236
+ alias : "plugin.textmask",
237
+ useMask : true,
238
+ date : false,
239
+ maskRel : {
240
+ m : '99',
241
+ d : '99',
242
+ n : '99',
243
+ j : '99',
244
+ Y : '9999'
245
+ },
246
+ init : function (cp) {
247
+ this.cp = cp;
248
+ if (this.cp.xtype === 'datefield') {
249
+ this.date = true;
250
+ }
251
+ if (this.date) {
252
+ this.cp.mask = '';
253
+ Ext.each(this.cp.format.split(''), function (item) {
254
+ this.cp.mask += this.maskRel[item] || item;
255
+ }, this);
256
+ }
257
+ cp.textMask = new Ext.ux.TextMaskCore(cp.mask, cp.money);
258
+ cp.emptyText = cp.textMask.mask('');
259
+ cp.updateHidden = this.updateHidden;
260
+ cp.getKeyCode = this.getKeyCode;
261
+ cp.simpleUpdateHidden = this.simpleUpdateHidden;
262
+ cp.getValue = this.getValue;
263
+ cp.getRawValue = this.getRawValue;
264
+ cp.getValueWithMask = this.getValueWithMask;
265
+ cp.getValueWithoutMask = this.getValueWithoutMask;
266
+ cp.setMask = this.setMask;
267
+ if (this.date) {
268
+ cp.setValue = this.setDateValue;
269
+ } else {
270
+ cp.setValue = this.setValue;
271
+ }
272
+ if (Ext.isEmpty(cp.useMask)) {
273
+ cp.useMask = this.useMask;
274
+ }
275
+ cp.on('afterrender', this.afterRender, cp);
276
+ },
277
+ afterRender : function () {
278
+ if (this.money) {
279
+ this.inputEl.setStyle('text-align', 'right');
280
+ }
281
+ this.hiddenField = this.inputEl.insertSibling({
282
+ tag : 'input',
283
+ type : 'hidden',
284
+ name : this.name,
285
+ value : this.textMask.mask(this.value)
286
+ }, 'after');
287
+ this.hiddenName = this.name;
288
+ this.inputEl.dom.removeAttribute('name');
289
+ this.enableKeyEvents = true;
290
+ this.inputEl.on({
291
+ keypress : this.updateHidden,
292
+ keydown : function (e) {
293
+ if (this.readOnly) {
294
+ return false;
295
+ }
296
+ if (e.getKey() === e.BACKSPACE) {
297
+ if (this.money) {
298
+ this.hiddenField.dom.value = this.hiddenField.dom.value.substr(0, this.hiddenField.dom.value.length - 1);
299
+ this.hiddenField.dom.value = this.hiddenField.dom.value.replace(/[.]/g, '');
300
+ this.hiddenField.dom.value = this.textMask.parsePrecision(this.hiddenField.dom.value, this.textMask.moneyPrecision);
301
+ this.hiddenField.dom.value = this.textMask.unmask(this.hiddenField.dom.value);
302
+ } else {
303
+ this.hiddenField.dom.value = this.hiddenField.dom.value.substr(0, this.hiddenField.dom.value.length - 1);
304
+ }
305
+ this.updateHidden(e);
306
+ }
307
+ this.keyDownEventKey = e.getKey();
308
+ },
309
+ keyup : this.simpleUpdateHidden,
310
+ scope : this
311
+ });
312
+ this.inputEl.dom.value = this.textMask.mask(this.hiddenField.dom.value);
313
+ this.setValue(this.value);
314
+ },
315
+ getKeyCode : function (onKeyDownEvent, type) {
316
+ if (this.readOnly) {
317
+ return false;
318
+ }
319
+ var keycode = {};
320
+ keycode.unicode = onKeyDownEvent.getKey();
321
+ keycode.isShiftPressed = onKeyDownEvent.shiftKey;
322
+ keycode.isDelete = ((onKeyDownEvent.getKey() === Ext.EventObject.DELETE && type === 'keydown') || (type === 'keypress' && onKeyDownEvent.charCode === 0 && onKeyDownEvent.keyCode === Ext.EventObject.DELETE)) ? true : false;
323
+ keycode.isTab = (onKeyDownEvent.getKey() === Ext.EventObject.TAB) ? true : false;
324
+ keycode.isBackspace = (onKeyDownEvent.getKey() === Ext.EventObject.BACKSPACE) ? true : false;
325
+ keycode.isLeftOrRightArrow = (onKeyDownEvent.getKey() === Ext.EventObject.LEFT || onKeyDownEvent.getKey() === Ext.EventObject.RIGHT) ? true : false;
326
+ keycode.pressedKey = String.fromCharCode(keycode.unicode);
327
+ return (keycode);
328
+ },
329
+ updateHidden : function (e) {
330
+ if (this.readOnly || !this.useMask) {
331
+ return false;
332
+ }
333
+ var key = this.getKeyCode(e, 'keydown');
334
+ var kk = this.keyDownEventKey || e.getKey();
335
+ if (!(kk >= e.F1 && kk <= e.F12) && !e.isNavKeyPress()) {
336
+ if (this.inputEl.dom.selectionStart === 0 && this.inputEl.dom.selectionEnd === this.inputEl.dom.value.length) {
337
+ this.hiddenField.dom.value = this.money ? 0 : '';
338
+ }
339
+ if (!key.isBackspace) {
340
+ if (this.money) {
341
+ this.hiddenField.dom.value = String(this.hiddenField.dom.value) + String(key.pressedKey);
342
+ this.hiddenField.dom.value = this.hiddenField.dom.value.replace(/[.]/g, '');
343
+ this.hiddenField.dom.value = this.textMask.parsePrecision(this.hiddenField.dom.value, this.textMask.moneyPrecision);
344
+ this.hiddenField.dom.value = this.textMask.unmask(this.hiddenField.dom.value);
345
+ } else {
346
+ this.hiddenField.dom.value = this.textMask.unmask(this.hiddenField.dom.value + key.pressedKey);
347
+ }
348
+ }
349
+ this.inputEl.dom.value = this.textMask.mask(this.hiddenField.dom.value);
350
+ this.inputEl.dom.selectionStart = this.textMask.getLength(this.hiddenField.dom.value);
351
+ this.inputEl.dom.selectionEnd = this.inputEl.dom.selectionStart;
352
+ e.preventDefault();
353
+ }
354
+ },
355
+ simpleUpdateHidden : function (e) {
356
+ if (this.readOnly || this.useMask) {
357
+ return false;
358
+ }
359
+ this.hiddenField.dom.value = this.inputEl.dom.value;
360
+ },
361
+ getValue : function () {
362
+ if (this.returnWithMask) {
363
+ var valor = this.getValueWithMask();
364
+ if (this.emptyText == this.getValueWithMask()) {
365
+ return "";
366
+ }
367
+ return this.getValueWithMask();
368
+ } else {
369
+ return this.getValueWithoutMask();
370
+ }
371
+ },
372
+ getValueWithMask : function () {
373
+ if (this.hiddenField) {
374
+ return this.inputEl.dom.value;
375
+ } else {
376
+ return '';
377
+ }
378
+ },
379
+ getValueWithoutMask : function () {
380
+ if (this.hiddenField) {
381
+ var value = this.hiddenField.dom.value;
382
+ if (value == '0.00') {
383
+ value = '';
384
+ }
385
+ return value;
386
+ } else {
387
+ return '';
388
+ }
389
+ },
390
+ getRawValue : function () {
391
+ return this.getValue();
392
+ },
393
+ setValue : function (v) {
394
+ if (this.useMask) {
395
+ if (this.inputEl) {
396
+ this.hiddenField.dom.value = this.textMask.unmask(v);
397
+ this.inputEl.dom.value = this.textMask.mask(v);
398
+ }
399
+ this.value = this.textMask.unmask(v);
400
+ } else {
401
+ if (this.inputEl) {
402
+ this.hiddenField.dom.value = v;
403
+ this.inputEl.dom.value = v;
404
+ }
405
+ this.value = v;
406
+ }
407
+ },
408
+ setDateValue : function (v) {
409
+ if (v === 'now') {
410
+ v = new Date();
411
+ }
412
+ if (this.inputEl) {
413
+ v = this.formatDate(this.parseDate(v));
414
+ this.hiddenField.dom.value = v;
415
+ this.inputEl.dom.value = this.textMask.mask(v);
416
+ }
417
+ this.value = v;
418
+ },
419
+ setMask : function (mask) {
420
+ this.textMask.setMask(mask);
421
+ this.setValue(this.hiddenField.dom.value);
422
+ }
423
+ });