From: Michael M Slusarz Date: Tue, 18 Aug 2009 04:33:20 +0000 (-0600) Subject: Fix addressbook preferences management in IMP. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=7b268ccd061d9523bd9db44881f0ae615141f3d9;p=horde.git Fix addressbook preferences management in IMP. Also, breakout javascript to static file. --- diff --git a/imp/js/addressbooksmanagement.js b/imp/js/addressbooksmanagement.js new file mode 100644 index 000000000..e3237f1b4 --- /dev/null +++ b/imp/js/addressbooksmanagement.js @@ -0,0 +1,166 @@ +/** + * Provides the javascript for managing addressbooks. + * + * See the enclosed file COPYING for license information (GPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/gpl.html. + */ + +var ImpAddressbooksmanagement = { + // Variables set by other code: fields + + deselectHeaders: function() + { + $F('unselected_search_sources').selectedIndex = $F('selected_search_sources').selectedIndex = -1; + }, + + resetHidden: function() + { + $('search_sources').setValue($F('selected_search_sources').join("\t")); + }, + + addSource: function() + { + this._sourceAction($('unselected_search_sources'), $('selected_search_sources')); + }, + + removeSource: function() + { + this._sourceAction($('selected_search_sources'), $('unselected_search_sources')); + }, + + _sourceAction: function(from, to) + { + var i = 1; + + $A(from.options).slice(1).each(function(s) { + if (s.selected) { + to[i] = s.cloneNode(false); + s.remove(); + } + ++i; + }); + + this.resetHidden(); + }, + + moveSourceUp: function() + { + var sss = $('selected_search_sources'), + sel = sss.selectedIndex; + + if (sel == -1 || sss.length <= 2) { + return; + } + + // Deselect everything but the first selected item + sss.selectedIndex = sel; + + sss.options[sel].previous().insert({ before: sss.options[sel].cloneNode(false) }); + sss.options[sel].remove(); + + this.resetHidden(); + }, + + moveSourceDown: function() + { + var i, + sss = $('selected_search_sources'), + sel = sss.selectedIndex, + l = sss.length, + tmp = []; + + if (sel == -1 || l <= 2) { + return; + } + + // deselect everything but the first selected item + sss.selectedIndex = sel; + + sss.options[sel].next().insert({ after: sss.options[sel].cloneNode(false) }); + sss.options[sel].remove(); + + this.resetHidden(); + }, + + updateSearchFields: function() + { + var sv = this._getSelectedValue(false), + sf = $('search_fields'); + + sf.update(''); + + this.fields.each(function(f) { + if (f[0] == sv) { + f.slice(1).each(function(o) { + var tmp = new Option(o[1], o[0]); + if (o[2]) { + tmp.selected = true; + } + sf.insert(tmp); + }); + } + }); + + this.changeSearchFields(); + }, + + _getSelectedValue: function(index) + { + var sss = $('selected_search_sources'); + + if (sss) { + return index ? sss.selectedIndex : sss.options[sss.selectedIndex].value; + } else { + return index ? 0 : this.fields[0][0]; + } + }, + + changeSearchFields: function() + { + var data = [], + i = 0, + sf = $('search_fields'), + sv = this._getSelectedValue(true); + + $A(sf.options).each(function(o) { + this.fields[sv][i + 1][2] = o.selected; + ++i; + }.bind(this)); + + this.fields.each(function(f) { + var tmp = [ f[0] ]; + f.slice(1).each(function(o) { + if (o[2]) { + tmp.push(o[0]); + } + }); + data.push(tmp.join("\t")); + }); + + $('search_fields_string').setValue(data.join("\n")); + }, + + onDomLoad: function() + { + this.updateSearchFields(); + + if ($('search_fields')) { + $('search_fields').observe('change', this.changeSearchFields.bind(this)); + } + + if ($('unselected_search_sources')) { + $('unselected_search_sources').observe('change', this.deselectHeaders.bind(this)); + $('selected_search_sources').observe('change', function() { + this.deselectHeaders(); + this.updateSearchFields(); + }.bind(this)); + $('addsource').observe('click', this.addSource.bind(this)); + $('removesource').observe('click', this.removeSource.bind(this)); + $('moveup').observe('click', this.moveSourceUp.bind(this)); + $('movedown').observe('click', this.moveSourceDown.bind(this)); + } + } + +}; + +document.observe('dom:loaded', ImpAddressbooksmanagement.onDomLoad.bind(ImpAddressbooksmanagement)); diff --git a/imp/lib/Application.php b/imp/lib/Application.php index b7b253f1e..2be58dc02 100644 --- a/imp/lib/Application.php +++ b/imp/lib/Application.php @@ -63,6 +63,13 @@ class IMP_Application extends Horde_Registry_Application static public $noCompress = false; /** + * Cached data for prefs pages. + * + * @var array + */ + static public $prefsCache = array(); + + /** * Constructor. * * @param array $args The following entries: @@ -418,6 +425,10 @@ class IMP_Application extends Horde_Registry_Application )); break; + case 'addressbooks': + $this->_prefsPrepareSourceselect(); + break; + case 'flags': Horde::addScriptFile('colorpicker.js', 'horde', true); Horde::addScriptFile('flagmanagement.js', 'imp', true); @@ -783,6 +794,84 @@ class IMP_Application extends Horde_Registry_Application } } + /** + * TODO + */ + protected function _prefsPrepareSourceselect() + { + self::$prefsCache['sourceselect'] = array(); + + $registry = Horde_Registry::singleton(); + if (!$registry->hasMethod('contacts/sources') || + $GLOBALS['prefs']->isLocked('search_sources')) { + return; + } + + $readable = $search_fields = $prefSelect = $writeable = $writeSelect = array(); + + try { + $readable = $registry->call('contacts/sources'); + } catch (Horde_Exception $e) {} + + try { + $writeable = $registry->call('contacts/sources', true); + } catch (Horde_Exception $e) {} + + $search = IMP_Compose::getAddressSearchParams(); + + if (count($readable) == 1) { + // Only one source, no need to display the selection widget + $search['sources'] = array_keys($readable); + } + + foreach ($search['sources'] as $source) { + if (!empty($readable[$source])) { + $prefSelect[$source] = $readable[$source]; + } + } + + $readSelect = array_diff(array_keys($readable), $search['sources']); + + if (!$GLOBALS['prefs']->isLocked('add_source')) { + foreach ($writeable as $source => $name) { + $writeSelect[] = array( + 'val' => $source, + 'sel' => ($GLOBALS['prefs']->getValue('add_source') == $source), + 'name' => $name + ); + } + } + + $source_count = 0; + + foreach (array_keys($readable) as $source) { + $search_fields[$source_count][] = $source; + + try { + foreach ($registry->call('contacts/fields', $source) as $field) { + if ($field['search']) { + $search_fields[$source_count][] = array($field['name'], $field['label'], isset($search['fields'][$source]) && in_array($field['name'], $search['fields'][$source])); + } + } + } catch (Horde_Exception $e) {} + + ++$source_count; + } + + Horde::addScriptFile('addressbooksmanagement.js', 'imp', true); + Horde::addInlineScript(array( + 'ImpAddressbooksmanagement.fields = ' . Horde_Serialize::serialize($search_fields, Horde_Serialize::JSON, Horde_Nls::getCharset()) + )); + self::$prefsCache['sourceselect'] = array( + 'prefSelect' => $prefSelect, + 'readable' => $readable, + 'readSelect' => $readSelect, + 'search' => $search, + 'writeable' => $writeable, + 'writeSelect' => $writeSelect + ); + } + /* horde/services/cache.php methods. */ /** diff --git a/imp/templates/prefs/sourceselect.inc b/imp/templates/prefs/sourceselect.inc index 81334976f..2a8e1eaf2 100644 --- a/imp/templates/prefs/sourceselect.inc +++ b/imp/templates/prefs/sourceselect.inc @@ -1,332 +1,42 @@ hasMethod('contacts/sources')) { - $search = IMP_Compose::getAddressSearchParams(); - try { - $readable = $registry->call('contacts/sources'); - } catch (Horde_Exception $e) { - $readable = null; - } - try { - $writeable = $registry->call('contacts/sources', true); - } catch (Horde_Exception $e) { - $writeable = null; - } - - $nbReadSources = count(array_keys($readable)); - $nbWriteSources = count(array_keys($writeable)); - - if ($nbReadSources == 1) { - // Only one source, no need to display the selection widget - $search['sources'] = array_keys($readable); - $readSelect = ''; - } - - $prefSelect = ''; - foreach ($search['sources'] as $source) { - if (!empty($readable[$source])) { - $prefSelect .= '\n"; - } - } - - $readSelect = ''; - if (is_array($readable)) { - foreach (array_diff(array_keys($readable), $search['sources']) as $source) { - $readSelect .= '\n"; - } - } - - if (is_array($writeable)) { - $writeSelect = '' . "\n"; - $writeSource = ''; - foreach ($writeable as $source => $name) { - $sel = $prefs->getValue('add_source') == $source ? ' selected="selected"' : ''; - $writeSelect .= '\n"; - $writeSource = $source; - } - } - - $search_fields = array(); - if (is_array($readable)) { - foreach (array_keys($readable) as $source) { - try { - $search_fields[$source] = $registry->call('contacts/fields', $source); - } catch (Horde_Exception $e) {} - } - } - - $js = "var searchFields = [];\n"; - $source_count = 0; - foreach ($search_fields as $source => $fields) { - $js .= "searchFields[$source_count] = [];\n"; - $js .= "searchFields[$source_count][0] = '$source';\n"; - - $field_count = 1; - foreach ($fields as $field) { - if ($field['search']) { - $marked = isset($search['fields'][$source]) && in_array($field['name'], $search['fields'][$source]) ? 'true' : 'false'; - $js .= "searchFields[$source_count][$field_count] = ['" . $field['name'] . "', '" . $field['label'] . "', $marked];\n"; - $field_count++; - } - } - - $source_count++; - } -} +$cache = IMP_Application::$prefsCache['sourceselect']; +if (!empty($cache['readSelect']) || !empty($cache['prefSelect'])): +$imagedir = $registry->getImageDir('horde'); ?> - -isLocked('search_sources') && (!empty($readSelect) || !empty($prefSelect))): ?> - -
-" /> - 1): ?> + +" /> + 1): ?>
- - + + + - getImageDir('horde')) ?> +
- getImageDir('horde')) ?> +
- - + $val): ?> + + - getImageDir('horde')) ?> +
- getImageDir('horde')) ?> +
@@ -337,26 +47,27 @@ function changeSearchFields()
- +
-
- - -isLocked('add_source') && !empty($writeSelect)): ?> -
-
- - + +
+
+ +