From 6c4cc97a5fc843d54deca6a54155334e69ae73c5 Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Wed, 19 Sep 2018 16:43:19 +0200 Subject: [PATCH] Implement new taglist widget to represent list of regions of a selected country --- addressbook/js/app.js | 7 +++ addressbook/templates/default/edit.xet | 6 +- api/js/etemplate/et2_widget_selectbox.js | 4 ++ api/js/etemplate/et2_widget_taglist.js | 80 ++++++++++++++++++++++++ api/src/Country.php | 3 - api/src/Etemplate/Widget/Select.php | 2 +- 6 files changed, 95 insertions(+), 7 deletions(-) diff --git a/addressbook/js/app.js b/addressbook/js/app.js index e23cbd4525..3d4812d64b 100644 --- a/addressbook/js/app.js +++ b/addressbook/js/app.js @@ -564,6 +564,13 @@ app.classes.addressbook = AppJS.extend( custom_field.style.display = "none"; } } + var region = this.et2.getWidgetById(selectbox.name.replace('countrycode', 'region')); + if (region) + { + region.set_country_code(selectbox.value); + region.options.select_options = {}; + region.transformAttributes(region.options); + } }, /** diff --git a/addressbook/templates/default/edit.xet b/addressbook/templates/default/edit.xet index 50493eb26c..61a4e775cb 100644 --- a/addressbook/templates/default/edit.xet +++ b/addressbook/templates/default/edit.xet @@ -84,7 +84,7 @@ - + @@ -95,7 +95,7 @@ - + @@ -251,7 +251,7 @@ - + diff --git a/api/js/etemplate/et2_widget_selectbox.js b/api/js/etemplate/et2_widget_selectbox.js index 1605fb7c70..5663534efb 100644 --- a/api/js/etemplate/et2_widget_selectbox.js +++ b/api/js/etemplate/et2_widget_selectbox.js @@ -1317,6 +1317,10 @@ jQuery.extend(et2_selectbox, //(function(){ "use strict"; return var options = ','; return this.cached_server_side_options(widget, options, attrs); }, + state_options: function(widget, attrs) { + var options = attrs.country_code ? attrs.country_code : 'de'; + return this.cached_server_side_options(widget, options, attrs); + }, dow_options: function(widget,attrs) { var options = ','+(attrs.other||[]).join(','); return this.cached_server_side_options(widget, options, attrs); diff --git a/api/js/etemplate/et2_widget_taglist.js b/api/js/etemplate/et2_widget_taglist.js index 7ccd6bdfb5..29d802f40a 100644 --- a/api/js/etemplate/et2_widget_taglist.js +++ b/api/js/etemplate/et2_widget_taglist.js @@ -1369,6 +1369,86 @@ var et2_taglist_thumbnail = (function(){ "use strict"; return et2_taglist.extend et2_register_widget(et2_taglist_thumbnail, ["taglist-thumbnail"]); +/** + * Taglist represents list of states of a country, + * + */ +var et2_taglist_state = (function(){ "use strict"; return et2_taglist.extend( +{ + attributes: { + "minChars": { + default: 0 + }, + "autocomplete_url": { + "default": "" + }, + "autocomplete_params": { + "default": {} + }, + "country_code": { + name: "country code to fetch states for", + default: "de", + type: "string", + description: "Defines country code to fetch list of states for it" + } + }, + /** + * + * @returns {undefined} + */ + init:function () + { + this._super.apply(this, arguments); + this.div.addClass('et2_taglist_state'); + }, + + /** + * Get options automatically from select option cache + * @param {type} _attrs + */ + transformAttributes: function(_attrs) { + // Pretend to be a select box so it works + var type = this._type; + this._type = 'select-state'; + this._super.apply(this, arguments); + this._type = type; + }, + /** + * convert selectbox options from the cache to taglist data [{id:...,label:...},...] format + * + * @param {(object|array)} _options id: label or id: {label: ..., title: ...} pairs, or array if id's are 0, 1, ... + * + * @return {Object[]} Returns an array of objects with ID and label + */ + _options2data: function(_options) + { + var options = jQuery.isArray(_options) ? jQuery.extend({}, _options) : _options; + var data = []; + for(var id in options) + { + var option = {}; + if (typeof options[id] == 'object') + { + jQuery.extend(option, options[id]); + if(option.value) option.id = option.value; + } + else + { + option.label = options[id]; + } + data.push(option); + } + return data; + }, + set_country_code: function (_country_code) + { + var country_code = _country_code || ''; + this.country_code = country_code; + this.options.country_code = country_code; + } +});}).call(this); +et2_register_widget(et2_taglist_state, ["taglist-state"]); + /** * et2_taglist_ro is the readonly implementation of the taglist. * diff --git a/api/src/Country.php b/api/src/Country.php index 37ed62849f..52a6b22312 100755 --- a/api/src/Country.php +++ b/api/src/Country.php @@ -434,13 +434,10 @@ class Country { case 'us': return self::$us_states_array; - break; case 'de': return self::$de_states_array; - break; case 'at': return self::$at_states_array; - break; case 'ch': return self::$ch_states_array; } diff --git a/api/src/Etemplate/Widget/Select.php b/api/src/Etemplate/Widget/Select.php index 14a90abc58..22f822b0e1 100644 --- a/api/src/Etemplate/Widget/Select.php +++ b/api/src/Etemplate/Widget/Select.php @@ -576,7 +576,7 @@ class Select extends Etemplate\Widget break; case 'select-state': - $options = Api\Country::us_states(); + $options = (array)Api\Country::get_states($field); $no_lang = True; break;