forked from extnet/Ext.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelField.cs
More file actions
682 lines (643 loc) · 26 KB
/
ModelField.cs
File metadata and controls
682 lines (643 loc) · 26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/********
* @version : 2.1.1 - Ext.NET Pro License
* @author : Ext.NET, Inc. http://www.ext.net/
* @date : 2012-12-10
* @copyright : Copyright (c) 2007-2012, Ext.NET, Inc. (http://www.ext.net/). All rights reserved.
* @license : See license.txt and http://www.ext.net/license/.
********/
using System.ComponentModel;
using System.Web.UI;
using Ext.Net.Utilities;
namespace Ext.Net
{
/// <summary>
/// Fields are used to define what a Model is. They aren't instantiated directly - instead, when we create a class that extends Ext.data.Model, it will automatically create a Field instance for each field configured in a Model. For example, we might set up a model like this:
///
/// Ext.define('User', {
/// extend: 'Ext.data.Model',
/// fields: [
/// 'name', 'email',
/// {name: 'age', type: 'int'},
/// {name: 'gender', type: 'string', defaultValue: 'Unknown'}
/// ]
/// });
/// Four fields will have been created for the User Model - name, email, age and gender. Note that we specified a couple of different formats here; if we only pass in the string name of the field (as with name and email), the field is set up with the 'auto' type. It's as if we'd done this instead:
///
/// Ext.define('User', {
/// extend: 'Ext.data.Model',
/// fields: [
/// {name: 'name', type: 'auto'},
/// {name: 'email', type: 'auto'},
/// {name: 'age', type: 'int'},
/// {name: 'gender', type: 'string', defaultValue: 'Unknown'}
/// ]
/// });
/// Types and conversion
///
/// The type is important - it's used to automatically convert data passed to the field into the correct format. In our example above, the name and email fields used the 'auto' type and will just accept anything that is passed into them. The 'age' field had an 'int' type however, so if we passed 25.4 this would be rounded to 25.
///
/// Sometimes a simple type isn't enough, or we want to perform some processing when we load a Field's data. We can do this using a convert function. Here, we're going to create a new field based on another:
///
/// Ext.define('User', {
/// extend: 'Ext.data.Model',
/// fields: [
/// 'name', 'email',
/// {name: 'age', type: 'int'},
/// {name: 'gender', type: 'string', defaultValue: 'Unknown'},
///
/// {
/// name: 'firstName',
/// convert: function(value, record) {
/// var fullName = record.get('name'),
/// splits = fullName.split(" "),
/// firstName = splits[0];
///
/// return firstName;
/// }
/// }
/// ]
/// });
/// Now when we create a new User, the firstName is populated automatically based on the name:
///
/// var ed = Ext.create('User', {name: 'Ed Spencer'});
///
/// console.log(ed.get('firstName')); //logs 'Ed', based on our convert function
/// In fact, if we log out all of the data inside ed, we'll see this:
///
/// console.log(ed.data);
///
/// //outputs this:
/// {
/// age: 0,
/// email: "",
/// firstName: "Ed",
/// gender: "Unknown",
/// name: "Ed Spencer"
/// }
/// The age field has been given a default of zero because we made it an int type. As an auto field, email has defaulted to an empty string. When we registered the User model we set gender's defaultValue to 'Unknown' so we see that now. Let's correct that and satisfy ourselves that the types work as we expect:
///
/// ed.set('gender', 'Male');
/// ed.get('gender'); //returns 'Male'
///
/// ed.set('age', 25.4);
/// ed.get('age'); //returns 25 - we wanted an int, not a float, so no decimal places allowed
/// </summary>
[Meta]
[Description("")]
public partial class ModelField : BaseItem
{
/// <summary>
///
/// </summary>
[Description("")]
public ModelField() { }
private JFunction convert;
private JFunction customSortType;
private JFunction serialize;
/// <summary>
///
/// </summary>
[Description("")]
public ModelField(string name)
{
this.Name = name;
}
/// <summary>
///
/// </summary>
[Description("")]
public ModelField(string name, ModelFieldType type)
{
this.Name = name;
this.Type = type;
}
/// <summary>
///
/// </summary>
[Description("")]
public ModelField(string name, ModelFieldType type, string dateFormat)
{
this.Name = name;
this.Type = type;
this.DateFormat = dateFormat;
}
protected internal ValidationCollection Validations
{
get;
set;
}
/// <summary>
/// The name by which the field is referenced within the Model. This is referenced by, for example, the dataIndex property in column definition objects passed to Ext.grid.header.Container.
///
/// Note: In the simplest case, if no properties other than name are required, a field definition may consist of just a String for the field name.
/// </summary>
[Meta]
[DefaultValue("")]
[Description("The name by which the field is referenced within the Model.")]
public virtual string Name
{
get
{
return this.State.Get<string>("Name", "");
}
set
{
this.State.Set("Name", value);
}
}
/// <summary>
///
/// </summary>
[ConfigOption("name")]
[Description("")]
protected virtual string NameProxy
{
get
{
return this.Name.IsEmpty() ? this.Mapping : this.Name;
}
}
/// <summary>
/// (Optional) A path expression for use by the Ext.data.reader.Reader implementation that is creating the Model to extract the Field value from the data object. If the path expression is the same as the field name, the mapping may be omitted.
///
/// The form of the mapping expression depends on the Reader being used.
///
/// Ext.data.reader.Json
/// The mapping is a string containing the javascript expression to reference the data from an element of the data item's root Array. Defaults to the field name.
/// Ext.data.reader.Xml
/// The mapping is an Ext.DomQuery path to the data item relative to the DOM element that represents the record. Defaults to the field name.
/// Ext.data.reader.Array
/// The mapping is a number indicating the Array index of the field's value. Defaults to the field specification's Array position.
/// If a more complex value extraction strategy is required, then configure the Field with a convert function. This is passed the whole row object, and may interrogate it in whatever way is necessary in order to return the desired data.
/// </summary>
[Meta]
[ConfigOption]
[Category("Config Options")]
[DefaultValue("")]
[Description("(Optional) A path expression for use by the Ext.data.reader.Reader implementation that is creating the Model to extract the Field value from the data object. If the path expression is the same as the field name, the mapping may be omitted.")]
public virtual string Mapping
{
get
{
return this.State.Get<string>("Mapping", "");
}
set
{
this.State.Set("Mapping", value);
}
}
/// <summary>
///
/// </summary>
[Meta]
[Category("Config Options")]
[DefaultValue("")]
[Description("")]
public virtual string ServerMapping
{
get
{
return this.State.Get<string>("ServerMapping", "");
}
set
{
this.State.Set("ServerMapping", value);
}
}
/// <summary>
/// (Optional) The data type for automatic conversion from received data to the stored value if convert has not been specified. This may be specified as a string value. Possible values are
///
/// auto (Default, implies no conversion)
/// string
/// int
/// float
/// boolean
/// date
/// This may also be specified by referencing a member of the Ext.data.Types class.
///
/// Developers may create their own application-specific data types by defining new members of the Ext.data.Types class.
/// </summary>
[Meta]
[ConfigOption(JsonMode.ToLower)]
[Category("Config Options")]
[DefaultValue(ModelFieldType.Auto)]
[Description("(Optional) The data type for automatic conversion from received data to the stored value if convert has not been specified.")]
public virtual ModelFieldType Type
{
get
{
return this.State.Get<ModelFieldType>("Type", ModelFieldType.Auto);
}
set
{
this.State.Set("Type", value);
}
}
/// <summary>
/// A function which converts a Field's value to a comparable value in order to ensure correct sort ordering. Predefined functions are provided in Ext.data.SortTypes.
/// </summary>
[Meta]
[ConfigOption(typeof(ToLowerCamelCase))]
[Category("Config Options")]
[DefaultValue(SortTypeMethod.None)]
[Description("Sort method")]
public virtual SortTypeMethod SortType
{
get
{
return this.State.Get<SortTypeMethod>("SortType", SortTypeMethod.None);
}
set
{
this.State.Set("SortType", value);
}
}
/// <summary>
/// Initial direction to sort ("ASC" or "DESC"). Defaults to "ASC".
/// </summary>
[Meta]
[ConfigOption]
[Category("Config Options")]
[DefaultValue(SortDirection.Default)]
[Description("(Optional) Initial direction to sort")]
public virtual SortDirection SortDir
{
get
{
return this.State.Get<SortDirection>("SortDir", SortDirection.Default);
}
set
{
this.State.Set("SortDir", value);
}
}
/// <summary>
/// Empty value representation during saving (default value as None)
/// </summary>
[Meta]
[ConfigOption(JsonMode.ToLower)]
[Category("Config Options")]
[DefaultValue(EmptyValue.None)]
[Description("Empty value representation during saving (default value as None)")]
public virtual EmptyValue SubmitEmptyValue
{
get
{
return this.State.Get<EmptyValue>("SubmitEmptyValue", EmptyValue.None);
}
set
{
this.State.Set("SubmitEmptyValue", value);
}
}
/// <summary>
/// A function which converts a Field's value to a comparable value in order to ensure correct sort ordering. A custom sort example:
///
/// // current sort after sort we want
/// // +-+------+ +-+------+
/// // |1|First | |1|First |
/// // |2|Last | |3|Second|
/// // |3|Second| |2|Last |
/// // +-+------+ +-+------+
///
/// sortType: function(value) {
/// switch (value.toLowerCase()) // native toLowerCase():
/// {
/// case 'first': return 1;
/// case 'second': return 2;
/// default: return 3;
/// }
/// }
/// </summary>
[Meta]
[ConfigOption("sortType", JsonMode.Raw)]
[Category("Config Options")]
[PersistenceMode(PersistenceMode.InnerProperty)]
[Description("A function which converts a Field's value to a comparable value in order to ensure correct sort ordering.")]
public virtual JFunction CustomSortType
{
get
{
if (this.customSortType == null)
{
this.customSortType = new JFunction();
if (!this.DesignMode)
{
this.customSortType.Args = new string[] {"value"};
}
}
return this.customSortType;
}
}
/// <summary>
/// A function which converts the Model's value for this Field into a form which can be used by whatever Writer is being used to sync data with the server.
///
/// The function should return a string which represents the Field's value.
///
/// It is passed the following parameters:
/// value : Mixed
/// The Field's value - the value to be serialized.
/// record : Ext.data.Model
/// The record being serialized.
/// </summary>
[Meta]
[ConfigOption("serialize", JsonMode.Raw)]
[Category("Config Options")]
[PersistenceMode(PersistenceMode.InnerProperty)]
[Description("A function which converts the Model's value for this Field into a form which can be used by whatever Writer is being used to sync data with the server.")]
public virtual JFunction Serialize
{
get
{
if (this.serialize == null)
{
this.serialize = new JFunction();
if (!this.DesignMode)
{
this.serialize.Args = new string[] { "value", "record" };
}
}
return this.serialize;
}
}
/// <summary>
/// (Optional) A function which converts the value provided by the Reader into an object that will be stored in the Model. It is passed the following parameters:
///
/// value : Mixed
/// The data value as read by the Reader, if undefined will use the configured defaultValue.
/// record : Ext.data.Model
/// The data object containing the Model as read so far by the Reader. Note that the Model may not be fully populated at this point as the fields are read in the order that they are defined in your fields array.
///
/// // example of convert function
/// function fullName(v, record){
/// return record.name.last + ', ' + record.name.first;
/// }
///
/// function location(v, record){
/// return !record.city ? '' : (record.city + ', ' + record.state);
/// }
///
/// var Dude = Ext.regModel({
/// fields: [
/// {name: 'fullname', convert: fullName},
/// {name: 'firstname', mapping: 'name.first'},
/// {name: 'lastname', mapping: 'name.last'},
/// {name: 'city', defaultValue: 'homeless'},
/// 'state',
/// {name: 'location', convert: location}
/// ]
/// });
///
/// // create the data store
/// var store = new Ext.data.Store({
/// reader: {
/// type: 'json',
/// model: 'Dude',
/// idProperty: 'key',
/// root: 'daRoot',
/// totalProperty: 'total'
/// }
/// });
///
/// var myData = [
/// { key: 1,
/// name: { first: 'Fat', last: 'Albert' }
/// // notice no city, state provided in data object
/// },
/// { key: 2,
/// name: { first: 'Barney', last: 'Rubble' },
/// city: 'Bedrock', state: 'Stoneridge'
/// },
/// { key: 3,
/// name: { first: 'Cliff', last: 'Claven' },
/// city: 'Boston', state: 'MA'
/// }
/// ];
/// </summary>
[Meta]
[ConfigOption(JsonMode.Raw)]
[Category("Config Options")]
[PersistenceMode(PersistenceMode.InnerProperty)]
[TypeConverter(typeof(ExpandableObjectConverter))]
[Description("(Optional) A function which converts the value provided by the Reader into an object that will be stored in the Record.")]
public virtual JFunction Convert
{
get
{
if (this.convert == null)
{
this.convert = new JFunction();
if (!this.DesignMode)
{
this.convert.Args = new string[] { "value", "record" };
}
}
return this.convert;
}
}
/// <summary>
/// If you ensure that data comes with correct format then convert can be set to null, it increase a parsing performance
/// </summary>
[Meta]
[Category("Config Options")]
[DefaultValue(false)]
[Description("")]
public virtual bool NullConvert
{
get
{
return this.State.Get<bool>("NullConvert", false);
}
set
{
this.State.Set("NullConvert", value);
}
}
[ConfigOption("convert", JsonMode.Raw)]
[DefaultValue(null)]
protected virtual string NullConvertProxy
{
get
{
return this.NullConvert ? "null" : null;
}
}
/// <summary>
///
/// </summary>
[Meta]
[DefaultValue(false)]
public virtual bool RenderMilliseconds
{
get;
set;
}
/// <summary>
/// (Optional) Used when converting received data into a Date when the type is specified as "date".
///
/// A format string for the Ext.Date.parse function, or "timestamp" if the value provided by the Reader is a UNIX timestamp, or "time" if the value provided by the Reader is a javascript millisecond timestamp.
///
/// It is quite important to note that while this config is optional, it will default to using the base
/// JavaScript Date object's `parse` function if not specified, rather than {@link Ext.Date#parse Ext.Date.parse}.
/// This can cause unexpected issues, especially when converting between timezones, or when converting dates that
/// do not have a timezone specified. The behavior of the native `Date.parse` is implementation-specific, and
/// depending on the value of the date string, it might return the UTC date or the local date. For this reason
/// it is strongly recommended that you always specify an explicit date format when parsing dates.
/// </summary>
[Meta]
[ConfigOption(typeof(NetToPHPDateFormatStringJsonConverter))]
[Category("Config Options")]
[DefaultValue("")]
[Description("(Optional) Used when converting received data into a Date when the type is specified as \"date\".")]
public virtual string DateFormat
{
get
{
string temp = (string)this.ViewState["DateFormat"] ?? "";
if (this.Type == ModelFieldType.Date && this.ViewState["DateFormat"] == null)
{
temp = this.RenderMilliseconds ? "yyyy-MM-dd\\THH:mm:ss.u" : "yyyy-MM-dd\\THH:mm:ss";
}
return temp;
}
set
{
this.State.Set("DateFormat", value);
}
}
/// <summary>
/// Used to provide a custom format when serializing dates with a writer. If this is not specified, the DateFormat will be used.
/// </summary>
[Meta]
[ConfigOption(typeof(NetToPHPDateFormatStringJsonConverter))]
[Category("Config Options")]
[DefaultValue("")]
[Description("Used to provide a custom format when serializing dates with a writer. If this is not specified, the DateFormat will be used.")]
public virtual string DateWriteFormat
{
get
{
return this.State.Get<string>("DateWriteFormat", "");
}
set
{
this.State.Set("DateWriteFormat", value);
}
}
/// <summary>
/// Used when converting received data into a Date when the Type is specified as "Date". This configuration takes precedence over DateFormat.
/// </summary>
[Meta]
[ConfigOption(typeof(NetToPHPDateFormatStringJsonConverter))]
[Category("Config Options")]
[DefaultValue("")]
[Description("Used when converting received data into a Date when the Type is specified as \"Date\". This configuration takes precedence over DateFormat.")]
public virtual string DateReadFormat
{
get
{
return this.State.Get<string>("DateReadFormat", "");
}
set
{
this.State.Set("DateReadFormat", value);
}
}
/// <summary>
/// (Optional) The default value used when a Model is being created by a Reader when the item referenced by the mapping does not exist in the data object (i.e. undefined). (defaults to "")
///
/// Please pay attention that if you use string const then need wrap like this
/// DefaultValue="'String const'"
/// </summary>
[Meta]
[ConfigOption(JsonMode.Raw)]
[Category("Config Options")]
[DefaultValue("")]
[Description("(Optional) The default value used when a Model is being created by a Reader when the item referenced by the mapping does not exist in the data object (i.e. undefined). (defaults to \"\")")]
public virtual string DefaultValue
{
get
{
return this.State.Get<string>("DefaultValue", "");
}
set
{
this.State.Set("DefaultValue", value);
}
}
/// <summary>
/// True to render this property as complex object
/// </summary>
[Meta]
[Category("Config Options")]
[DefaultValue(false)]
[Description("True to render this property as complex object")]
public virtual bool IsComplex
{
get
{
return this.State.Get<bool>("IsComplex", false);
}
set
{
this.State.Set("IsComplex", value);
}
}
/// <summary>
/// False to exclude this field from the Ext.data.Model.modified fields in a model. This will also exclude the field from being written using a Ext.data.writer.Writer. This option is useful when model fields are used to keep state on the client but do not need to be persisted to the server. Defaults to true.
/// </summary>
[Meta]
[ConfigOption]
[Category("Config Options")]
[DefaultValue(true)]
[Description("False to exclude this field from the Ext.data.Model.modified fields in a model. This will also exclude the field from being written using a Ext.data.writer.Writer. This option is useful when model fields are used to keep state on the client but do not need to be persisted to the server. Defaults to true.")]
public virtual bool Persist
{
get
{
return this.State.Get<bool>("Persist", true);
}
set
{
this.State.Set("Persist", value);
}
}
/// <summary>
/// Use when converting received data into a Number type (either int or float). If the value cannot be parsed, null will be used if useNull is true, otherwise the value will be 0. Defaults to false.
/// </summary>
[Meta]
[ConfigOption]
[Category("Config Options")]
[DefaultValue(false)]
[Description("Use when converting received data into a Number type (either int or float). If the value cannot be parsed, null will be used if useNull is true, otherwise the value will be 0. Defaults to false.")]
public virtual bool UseNull
{
get
{
return this.State.Get<bool>("UseNull", false);
}
set
{
this.State.Set("UseNull", value);
}
}
/// <summary>
/// Configure `true` to encode html in the field before sync
/// </summary>
[Meta]
[ConfigOption]
[DefaultValue(false)]
[Description("Configure `true` to encode html in the field before sync")]
public virtual bool HtmlEncode
{
get
{
return this.State.Get<bool>("HtmlEncode", false);
}
set
{
this.State.Set("HtmlEncode", value);
}
}
}
}