prevent errors in renderer if keyfields are not available
[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             try {
32                 var appName = Ext.isString(app) ? app : app.appName,
33                     app = Tine.Tinebase.appMgr.get(appName),
34                     store = Tine.Tinebase.widgets.keyfield.StoreMgr.get(app, keyFieldName),
35                     what = what ? what : 'text|icon',
36                     whatParts = what.split('|'),
37                     key = appName + keyFieldName + what;
38             } catch (e) {
39                 Tine.log.error(e);
40             }
41                 
42             if (! renderers[key]) {
43                 renderers[key] = function(id) {
44                     if (! id) return "";
45                     var record = store.getById(id),
46                         i18nValue = record ? record.get('i18nValue') : app.i18n._hidden(id),
47                         icon = record ? record.get('icon') : null,
48                         string = '';
49                         
50                     if (whatParts.indexOf('icon') > -1 && icon) {
51                         string = string + '<img src="' + icon + '" class="tine-keyfield-icon" ext:qtip="' + Ext.util.Format.htmlEncode(i18nValue) + '" />';
52                     }
53                         
54                     if (whatParts.indexOf('text') > -1 && i18nValue) {
55                         string = string + Ext.util.Format.htmlEncode(i18nValue);
56                     }
57                     
58                     return string;
59                 }
60             }
61             
62             return renderers[key];
63         },
64         
65         /**
66          * render a given value
67          * 
68          * @param {String/Application}  app
69          * @param {String}              keyFieldName 
70          * @return Ext.data.Store
71          */
72         render: function(app, keyFieldName, id) {
73             var renderer = this.get(app, keyFieldName);
74             
75             return renderer(id);
76         },
77         
78         /**
79          * register a custom renderer
80          * 
81          * @param {String/Application}  app
82          * @param {String}              keyFieldName 
83          * @param {Function}            renderer
84          */
85         register: function(app, keyFieldName, renderer) {
86             var appName = Ext.isString(app) ? app : app.appName,
87                 key = appName + '_' + keyFieldName;
88                 
89             renderers[key] = renderer;
90         }
91     }
92 }();