e18884e8dc54beacf87c0f3f87f1a7c635abb448
[tine20] / tine20 / Tinebase / js / widgets / keyfield / Renderer.js
1 /* 
2  * Tine 2.0
3  * 
4  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
5  * @author      Cornelius Weiss <c.weiss@metaways.de>
6  * @copyright   Copyright (c) 2011 Metaways Infosystems GmbH (http://www.metaways.de)
7  */
8  
9 Ext.ns('Tine.Tinebase.widgets.keyfield');
10
11 /**
12  * manages key field renderers
13  * 
14  * @namespace   Tine.Tinebase.widgets.keyfield
15  * @class       Tine.Tinebase.widgets.keyfield.Renderer
16  * @singleton
17  */
18 Tine.Tinebase.widgets.keyfield.Renderer = function(){
19     var renderers = {};
20     
21     return {
22         /**
23          * returns key field record renderer
24          * 
25          * @param {String/Application}  app
26          * @param {String}              keyFieldName 
27          * @param {String}              what pipe seperated field with text|icon
28          * @return Ext.data.Store
29          */
30         get: function(app, keyFieldName, what) {
31             var appName = Ext.isString(app) ? app : app.appName,
32                 app = Tine.Tinebase.appMgr.get(appName),
33                 store = Tine.Tinebase.widgets.keyfield.StoreMgr.get(app, keyFieldName),
34                 what = what ? what : 'text|icon',
35                 whatParts = what.split('|'),
36                 key = appName + keyFieldName + what;
37                 
38             if (! renderers[key]) {
39                 renderers[key] = function(id) {
40                     if (! id) return "";
41                     var record = store.getById(id),
42                         i18nValue = record ? record.get('i18nValue') : app.i18n._hidden(id),
43                         icon = record ? record.get('icon') : null,
44                         string = '';
45                         
46                     if (whatParts.indexOf('icon') > -1 && icon) {
47                         string = string + '<img src="' + icon + '" class="tine-keyfield-icon" ext:qtip="' + Ext.util.Format.htmlEncode(i18nValue) + '" />';
48                     }
49                         
50                     if (whatParts.indexOf('text') > -1 && i18nValue) {
51                         string = string + Ext.util.Format.htmlEncode(i18nValue);
52                     }
53                     
54                     return string;
55                 }
56             }
57             
58             return renderers[key];
59         },
60         
61         /**
62          * render a given value
63          * 
64          * @param {String/Application}  app
65          * @param {String}              keyFieldName 
66          * @return Ext.data.Store
67          */
68         render: function(app, keyFieldName, id) {
69             var renderer = this.get(app, keyFieldName);
70             
71             return renderer(id);
72         },
73         
74         /**
75          * register a custom renderer
76          * 
77          * @param {String/Application}  app
78          * @param {String}              keyFieldName 
79          * @param {Function}            renderer
80          */
81         register: function(app, keyFieldName, renderer) {
82             var appName = Ext.isString(app) ? app : app.appName,
83                 key = appName + '_' + keyFieldName;
84                 
85             renderers[key] = renderer;
86         }
87     }
88 }();