1 /*jshint indent:2, curly:true, eqeqeq:true, immed:true, latedef:true,
2 newcap:true, noarg:true, regexp:true, undef:true, strict:true, trailing:true,
4 /*global XT:true, XM:true, Backbone:true, _:true, console:true */
9 // ..........................................................
14 Shared address functionality.
17 numberPolicy: XM.Document.AUTO_NUMBER,
19 // ..........................................................
23 Set default country if applicable.
25 initialize: function (attributes, options) {
26 XM.Document.prototype.initialize.apply(this, arguments);
27 if (options && options.isNew && !this.get('country')) {
28 var settings = XT.session ? XT.session.getSettings() : null,
29 country = settings ? settings.get('DefaultAddressCountry') : null;
30 if (country) { this.set('country', country); }
35 Formats the multiple lines of an address into a
36 text block separating the elements of the address by line breaks.
38 @params {Boolean} Is HTML - default true
41 format: function (isHtml) {
42 return XM.Address.format(this, isHtml);
46 A formatted address that includes city, state and country.
50 formatShort: function () {
51 return XM.Address.formatShort(this);
54 isAllEmpty: function () {
55 return !this.get("line1") &&
60 !this.get("postalCode") &&
65 Success response returns an integer from the server indicating how many times the address
66 is used by other records.
68 @param {Object} Options
71 useCount: function (options) {
72 this.dispatch('XM.Address', 'useCount', this.id, options);
76 validate: function (attributes, options) {
77 var settings = XT.session.getSettings(),
78 strict = settings.get('StrictAddressCountry'),
79 country = attributes.country,
82 // Validate country if setting says to do so
83 if (country && strict) {
84 found = _.find(XM.countries.models, function (c) {
85 return c.get('name') === country;
88 return XT.Error.clone('xt2008');
91 return XM.Document.prototype.validate.apply(this, arguments);
101 XM.Country = XM.Document.extend({
102 /** @scope XM.Country.prototype */
104 recordType: 'XM.Country',
106 documentKey: "abbreviation",
108 // ..........................................................
112 validate: function (attributes) {
115 if (attributes.abbreviation &&
116 attributes.abbreviation.length !== 2) {
117 params.attr = "_abbreviation".loc();
119 return XT.Error.clone('xt1006', { params: params });
122 if (attributes.currencyAbbreviation &&
123 attributes.currencyAbbreviation.length !== 3) {
124 params.attr = "_currencyAbbreviation".loc();
126 return XT.Error.clone('xt1006', { params: params });
129 if (attributes.currencyNumber &&
130 attributes.currencyNumber.length !== 3) {
131 params.attr = "_currencyNumber".loc();
133 return XT.Error.clone('xt1006', { params: params });
136 return XM.Document.prototype.validate.apply(this, arguments);
146 XM.State = XM.Document.extend({
147 /** @scope XM.State.prototype */
149 recordType: 'XM.State',
151 documentKey: "abbreviation",
153 enforceUpperKey: false
161 @extends XM.AddressMixin
163 XM.Address = XM.Document.extend({
164 /** @scope XM.Address.prototype */
166 recordType: 'XM.Address'
170 XM.Address = XM.Address.extend(XM.AddressMixin);
172 // ..........................................................
176 _.extend(XM.Address, {
179 Success response returns an address id for an address with the same fields
182 @param {String} Line1
183 @param {String} Line2
184 @param {String} Line3
186 @param {String} State
187 @param {String} Postal Code
188 @param {String} Country
189 @param {Object} Options
192 findExisting: function (line1, line2, line3, city, state, postalcode, country, options) {
200 postalcode: postalcode,
203 XM.ModelMixin.dispatch('XM.Address', 'findExisting', params, options);
208 This function formats the multiple lines of an address into a
209 text block separating the elements of the address by line breaks.
211 Address format accepts multiple argument formats:
212 XM.Address.format(address);
213 XM.Address.format(address, isHtml);
214 XM.Address.format(name, line1, line2, line3, city, state, postalcode, country);
215 XM.Address.format(name, line1, line2, line3, city, state, postalcode, country, isHtml);
217 Where address is an XM.Address and isHtml determines whether to
218 use HTML line breaks instead of ASCII new line characters. The
219 default for isHtml is true. The longer signatures accept string
220 components of an address.
224 format: function () {
225 if (!arguments[0]) { return; }
227 name, line1, line2, line3,
228 city, state, postalcode, country,
229 breaks, result = '', csz;
231 if (typeof arguments[0] === 'object') {
233 line1 = arguments[0].get('line1');
234 line2 = arguments[0].get('line2');
235 line3 = arguments[0].get('line3');
236 city = arguments[0].get('city');
237 state = arguments[0].get('state');
238 postalcode = arguments[0].get('postalCode');
239 country = arguments[0].get('country');
240 breaks = (arguments[1] === undefined ? true : arguments[1]) ? '<br />' : '\n';
241 } else if (typeof arguments[0] === 'string') {
243 line1 = arguments[1];
244 line2 = arguments[2];
245 line3 = arguments[3];
247 state = arguments[5];
248 postalcode = arguments[6];
249 country = arguments[7];
250 breaks = (arguments[8] === undefined ? true : arguments[8]) ? '<br />' : '\n';
251 } else { return false; }
253 if (name) { fmtlines.push(name); }
254 if (line1) { fmtlines.push(line1); }
255 if (line2) { fmtlines.push(line2); }
256 if (line3) { fmtlines.push(line3); }
257 if (city || state || postalcode) {
259 (city && (state || postalcode) ? ', ' : '') +
261 (state && postalcode ? ' ' : '') +
265 if (country) { fmtlines.push(country); }
267 if (fmtlines.length) { result = fmtlines.join(breaks); }
273 A formatted address that includes city, state and country.
275 short format accepts multiple argument formats:
276 XM.Address.format(address);
277 XM.Address.format(city, state, country);
281 formatShort: function (address) {
287 if (address && typeof address === 'object') {
288 city = address.get('city') || '';
289 state = address.get('state') || '';
290 country = address.get('country') || '';
292 city = arguments[0] || '';
293 state = arguments[1] || '';
294 country = arguments[2] || '';
296 ret = city + (city && state ? ', ' : '') + state;
297 ret += (ret ? ' ' : '') + country;
301 // ..........................................................
306 Option to convert the existing address into a new one.
316 Option to change the existing address so all users of the
317 address are affected.
333 XM.AddressComment = XM.Model.extend({
334 /** @scope XM.AddressComment.prototype */
336 recordType: 'XM.AddressComment'
345 XM.AddressCharacteristic = XM.Model.extend({
346 /** @scope XM.AddressCharacteristic.prototype */
348 recordType: 'XM.AddressCharacteristic'
356 @extends XM.AddressMixin
358 XM.AddressInfo = XM.Document.extend({
359 /** @scope XM.AddressInfo.prototype */
361 recordType: 'XM.AddressInfo',
363 // ..........................................................
368 Return a copy of this address.
370 @return {XM.Address} copy of the address
373 var attrs = _.clone(this.attributes);
375 delete attrs.dataState;
378 delete attrs.isActive;
379 return new XM.AddressInfo(attrs, {isNew: true});
382 isEmpty: function () {
383 return (_.isEmpty(this.get('line1')) &&
384 _.isEmpty(this.get('line2')) &&
385 _.isEmpty(this.get('line3')) &&
386 _.isEmpty(this.get('city')) &&
387 _.isEmpty(this.get('state')) &&
388 _.isEmpty(this.get('postalCode')) &&
389 _.isEmpty(this.get('country')));
394 XM.AddressInfo = XM.AddressInfo.extend(XM.AddressMixin);
396 // ..........................................................
403 @extends XM.Collection
405 XM.AddressInfoCollection = XM.Collection.extend({
406 /** @scope XM.AddressInfoCollection.prototype */
408 model: XM.AddressInfo
415 @extends XM.Collection
417 XM.CountryCollection = XM.Collection.extend({
418 /** @scope XM.CountryCollection.prototype */
427 @extends XM.Collection
429 XM.StateCollection = XM.Collection.extend({
430 /** @scope XM.StateCollection.prototype */