/*! Chinese region picker - v0.0.1 - 2013-10-24 * https://github.com/xixilive/chineserp * Copyright (c) 2013 xixilive; Licensed MIT */ (function($) { 'use strict'; //ECMA262-5 methods: Array#indexOf if (!('indexOf' in Array.prototype)) { Array.prototype.indexOf= function(find, i) { if (i === undefined){ i= 0; } if (i<0){ i+= this.length; } if (i<0){ i= 0; } for (var n= this.length; i "); ); }) picker.pick('PROVINCE,CITY,SUBURB', function(regions){ console.log( regions.map(function(d){ return d.n; }).join(" > "); ); }) }}) @return RegionPicker object */ var RegionPicker = ChineseRegion.RegionPicker = function(options){ var self = this; this.options = options; new DataProxy(options.remote || '', function(proxy){ self.initialize(proxy); }); return self; }; RegionPicker.prototype = { initialize: function(proxy){ this.proxy = proxy; if(ChineseRegion.ifn(this.options.initialized)){ this.options.initialized(this); } }, /** pick up a specified region @param {String | Array} value @param {Object} options @param {Function} callback @return undefined @example pick(value, callback) @example pick(value, options, callback) */ pick: function(){ var value = arguments[0], options = {}, f; switch(arguments.length){ case 2: f = arguments[1]; break; case 3: options = arguments[1]; f = arguments[2]; break; } if(!ChineseRegion.ifn(f)){ f = function(){}; } var proxy = this.proxy; if(!this.proxy){ f([]); return; } if(value === null || value === ''){ value = proxy.indices().collection[0].i; } if(/^\d+$/.test(value)){ //pickById this._pickById(value, f); return; }else{ //pickByNames if(typeof value === 'string'){ value = value.split(/[,\s]+/,3); } this._pickByNames(value, options, f); return; } f([]); }, /** Provide array that is the super-collection of regions @param {Array} regions @return {Array} a array contains 3 or less elements, in same order as the elements in regions argument */ _pickedCollections: function(regions){ if(!regions || !regions.map){ return []; } var collections = []; if(regions[0]){ collections.push(this.proxy.collection('index').collection); } if(regions[1]){ collections.push(this.proxy.collection(regions[1].i).collection); } if(regions[2] && regions[1].c){ collections.push(regions[1].c); } return collections; }, /** Pickup regions via specified ID @param {String} id @param {Function} f, a callback function that will be call when regions have picked */ _pickById: function(id, f){ var self = this, proxy = this.proxy, regions = []; regions[0] = proxy.indices().first(function(r){ return r.i === id.substr(0,2) + '0000'; }); proxy.load(id, function(c){ regions = regions.concat(c.findById(id)); f(regions, self._pickedCollections(regions)); }); }, /** Pickup a region via specified string seperated with comma or space @param {String} names @param {Object} options @param {Function} f, a callback function that will be call when regions have picked */ _pickByNames: function(names, options, f){ var self = this, proxy = this.proxy, regions = []; regions[0] = proxy.indices().first(function(r){ return r.n === names[0]; }); if(!regions[0]){ f([]); return; } proxy.load(regions[0].i, function(c){ regions = regions.concat(c.findByNames(names[1],names[2],options)); f(regions, self._pickedCollections(regions)); }); } }; }).call(this, this.jQuery || this.$ || this.Zepto || this.Prototype);