4 * Ajatus - Distributed CRM
5 * @requires jQuery v1.2.1
7 * Copyright (c) 2007 Jerry Jalava <jerry.jalava@gmail.com>
8 * Copyright (c) 2007 Nemein Oy <http://nemein.com>
9 * Website: http://ajatus.info
10 * Licensed under the GPL license
11 * http://www.gnu.org/licenses/gpl.html
16 $.ajatus = $.ajatus || {};
17 $.ajatus.renderer = $.ajatus.renderer || {};
19 $.ajatus.renderer.list_tpl = function(target, content_type, options) {
20 var default_options = {
21 className: 'list_holder',
26 show_count_on_title: true,
38 render_item_after_load: false,
45 this.options = $.extend({}, default_options, options);
46 if ( typeof options['pool'] != 'undefined'
47 && typeof options['pool']['settings'] != 'undefined')
49 this.options.pool.settings = $.extend(default_options.pool.settings, options['pool']['settings']);
53 this.headers = this.options.headers || content_type.list_headers;
54 this.schema_fields = this.options.schema || (content_type.list_schema || content_type.schema);
56 this.options.actions = [
59 title: $.ajatus.i10n.get('View'),
64 title: $.ajatus.i10n.get('Edit'),
69 title: $.ajatus.i10n.get('Archive'),
71 click_action: '$.ajatus.document.actions.execute("archive", doc);'
75 title: $.ajatus.i10n.get('Delete'),
77 click_action: '$.ajatus.document.actions.execute("delete", doc);'
81 this.holder = $('<div class="'+options.className+'" id="'+options.id+'" />');
83 title_str = this.options.title || content_type.title;
84 if (this.options.plural_title) {
85 title_str = $.ajatus.i10n.plural($.ajatus.i10n.get(title_str));
87 title_str = $.ajatus.i10n.get(title_str);
89 if (this.options.title_suffix != null) {
90 title_str += ' ' + this.options.title_suffix;
99 toString: function() {
100 var str = this.title;
101 if (this.show_count) {
102 var cnt = this.items.length;
103 if (this.total_items != null) {
104 cnt = this.total_items;
111 this.tpl_data.show_count = this.options.show_count_on_title;
114 this.processed_items = [];
116 if (this.options.main_template == null) {
117 this.options.main_template = '{#template MAIN}' +
119 '<table class="listing">' +
121 '{#foreach $T.headers as header}' +
122 '{#include HCOLUMN root=$T.header}' +
126 '{#foreach $T.items as item}' +
127 '<tr class="{#cycle values=[\'odd\',\'even\']}">' +
128 '{#include ICOLUMNS root=$T.item}' +
135 this.header_column_tpl = $.createTemplate('<th class="{$T.className}">{$T.content}</th>');
136 this.item_columns_tpl = '';
137 this.item_actions_tpl = $.createTemplate('<a href="{$T.url}" title="{$T.title}" {#if $T.onclick} onclick="{$T.onclick}" {#/if}><img src="{$T.icon}" class="{$T.className}" alt="{$T.title}"/></a>');
140 if (this.options.pool.enabled) {
141 this.pool = new $.ajatus.renderer(this.options.pool.settings, this);
144 this.attach(this.options.append);
146 $.extend($.ajatus.renderer.list_tpl.prototype, {
147 attach: function(append) {
149 this.holder.appendTo(this.target);
151 this.target.html(this.holder);
155 this._collect_headers();
156 this._collect_items();
160 render_item: function(doc, idx) {//from_db
161 // if ( typeof from_db != 'undefined'
164 // this.items.push(doc);
166 return this._render_item(doc, idx);
170 add_item: function(doc) {
171 if (this.options.pool.enabled) {
172 this.pool.add_item(doc);
174 this.items.push(doc);
177 add_total_count: function(count) {
178 if (typeof count != 'undefined') {
179 this.tpl_data.total_items = count;
181 if (this.options.pool.enabled) {
182 this.pool.items_added(count);
186 enable_sorting: function() {
187 $('table', this.holder).tablesorter();
189 _collect_headers: function() {
191 $.each(this.headers, function(i,h){
192 var schema = _self.schema_fields[h];
201 className = 'first_column ' + className;
204 _self.tpl_data.headers.push({
205 className: className,
206 content: $.ajatus.i10n.get(schema.label)
210 if (this.options.show_actions) {
211 this.tpl_data.headers.push({
212 className: 'actions',
213 content: $.ajatus.i10n.get('Actions')
217 _collect_items: function() {
220 for (var i=0; i<this.items.length; i++) {
221 var doc = this.items[i];
225 $.each(this.headers, function(i, h){
228 className = 'first_column ' + className;
231 var data = $.extend({
243 data.val = doc[h] || doc.value[h];
246 if (data.val === null) {
247 var full_doc = new $.ajatus.document.loader(doc._id, doc._rev);
254 }, full_doc.value[h]);
257 var widget = new $.ajatus.widget(data.widget.name, data.widget.config);
258 data.val = widget.value_on_view(data.val, 'list');
262 var normalized_type = type.toString().replace('_', ' ');
264 var img_title = $.ajatus.i10n.get(normalized_type);
265 data.val = '<img src="' + $.ajatus.preferences.client.theme_icons_url + type + '.png' + '" title="'+img_title+'" alt="'+img_title+'"/>';
269 column_className: className,
273 if (_self.options.show_actions) {
274 row_data['actions'] = {
275 items: _self._get_actions_for_item(doc)
279 _self.processed_items.push(doc._id);
283 this.tpl_data.items.push(row_data);
286 _get_actions_for_item: function(doc, actions) {
287 if (typeof actions == 'undefined') {
288 var actions = this.options.actions;
290 var content_type = $.ajatus.preferences.client.content_types[doc.value._type];
292 var actions_length = actions.length-1;
294 var action_items = [];
296 $.each(actions, function(i,action){
298 url: '#' + action.name + '.' + content_type.name + '.' + (doc._id || doc.id),
300 icon: $.ajatus.preferences.client.theme_icons_url + action.icon,
301 className: action.name + '_link',
304 if (typeof action['click_action'] != 'undefined') {
305 action_data['onclick'] = function(e){eval(action.click_action);}
308 action_items.push(action_data);
313 _create_items_tpl: function() {
314 var item_columns_tpl = '';
315 $.each(this.headers, function(i,h){
316 item_columns_tpl += '<td class="{$T.'+h+'.column_className}">{$T.'+h+'.content}</td>';
319 if (this.options.show_actions) {
320 item_columns_tpl += '<td class="actions">' +
321 '{#foreach $T.actions.items as aitem}' +
322 '{#include IACTIONS root=$T.aitem}' +
326 this.item_columns_tpl = $.createTemplate(item_columns_tpl, {
327 'IACTIONS': this.item_actions_tpl
330 this.item_columns_tpl = $.createTemplate(item_columns_tpl);
333 _render: function() {
334 this._create_items_tpl();
336 $(this.holder).setTemplate(this.options.main_template, {
337 'HCOLUMN': this.header_column_tpl,
338 'ICOLUMNS': this.item_columns_tpl
340 $(this.holder).processTemplate(this.tpl_data);
342 _render_item: function(doc, idx) {
343 if ($.inArray(doc._id, this.processed_items) != -1) {
347 var tpl_data = this._get_item_tpl_data(doc);
348 var tbody = $('tbody', this.holder);
350 var row_class = "even";
351 if (idx != 'undefined') {
358 var item_columns = '';
359 $.each(this.headers, function(i,h){
360 item_columns += '<td class="{$T.'+h+'.column_className}">{$T.'+h+'.content}</td>';
362 item_columns += '<td class="actions">' +
363 '{#foreach $T.actions.items as aitem}' +
364 '{#include IACTIONS root=$T.aitem}' +
368 var item_row = '<tr class="'+row_class+'">' + item_columns + '</tr>';
370 item_row_tpl = $.createTemplate(item_row, {
371 'IACTIONS': this.item_actions_tpl
374 $(tbody).setTemplate(item_row_tpl);
375 $(tbody).processTemplate(tpl_data, {}, true);
377 this.processed_items.push(doc._id);
381 _get_item_tpl_data: function(doc) {
385 $.each(this.headers, function(i, h){
388 className = 'first_column ' + className;
391 var data = $.extend({
403 data.val = doc[h] || doc.value[h];
406 if (data.val === null) {
407 var full_doc = new $.ajatus.document.loader(doc._id, doc._rev);
414 }, full_doc.value[h]);
417 var widget = new $.ajatus.widget(data.widget.name, data.widget.config);
418 data.val = widget.value_on_view(data.val, 'list');
422 var normalized_type = type.toString().replace('_', ' ');
424 var img_title = $.ajatus.i10n.get(normalized_type);
425 data.val = '<img src="' + $.ajatus.preferences.client.theme_icons_url + type + '.png' + '" title="'+img_title+'" alt="'+img_title+'"/>';
429 column_className: className,
433 if (_self.options.show_actions) {
434 row_data['actions'] = {
435 items: _self._get_actions_for_item(doc)