From 2d2e089f3e7b9af6ec7bdb33196da790d43d3ecb Mon Sep 17 00:00:00 2001 From: Jerry Jalava Date: Mon, 15 Oct 2007 10:11:14 +0300 Subject: [PATCH] Fixed dynamic view generation and added example to custom view 'test' --- application.html | 2 +- js/ajatus.core.js | 34 ++++++++---- js/ajatus.views.js | 6 +- js/renderers/list.js | 3 +- js/views/custom/test.js | 44 ++++++++------- js/views/system.js | 144 +++++++++++++++++++++++++++++------------------- 6 files changed, 139 insertions(+), 94 deletions(-) diff --git a/application.html b/application.html index 279fcf0..7f69adc 100644 --- a/application.html +++ b/application.html @@ -33,7 +33,7 @@ - + diff --git a/js/ajatus.core.js b/js/ajatus.core.js index 4a24bf7..81ed0f6 100644 --- a/js/ajatus.core.js +++ b/js/ajatus.core.js @@ -29,8 +29,7 @@ if (console == undefined) $.ajatus.active_type = null; $.ajatus.converter = {}; - $.ajatus.converter.parseJSON = function (json_str) - { + $.ajatus.converter.parseJSON = function (json_str) { try { return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test( @@ -198,7 +197,13 @@ if (console == undefined) $.ajatus.formatter.date.iso8601_to_js = function(iso_date) { var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?"; var isod = iso_date.match(new RegExp(regexp)); - + + if ( !isod + || isod.length < 11) + { + return new Date(); + } + var date = new Date(isod[1], 0, 1); date.setMonth(isod[3] - 1); date.setDate(isod[5]); @@ -305,7 +310,6 @@ if (console == undefined) return time_str; }; - /** * Following functions are taken from the jquery form plugin. * Plugin can be found from http://www.malsup.com/jquery/form/ @@ -403,6 +407,10 @@ if (console == undefined) this.check_metadata(); } + if (! $.ajatus.preferences.client.content_types[this.ready_doc.value._type]) { + this.ready_doc.value._type = 'note'; + } + this.prepare_doc_title(); return this.ready_doc; @@ -439,10 +447,13 @@ if (console == undefined) }, prepare_doc_title: function() { var self = this; + console.log("prepare title"); + console.log(self.ready_doc); if ( $.ajatus.preferences.client.content_types[this.ready_doc.value._type]['title_item'] && ( this.ready_doc.value['title'] == undefined || this.ready_doc.value['title'].val != '') ) { + console.log("Create title"); $.ajatus.preferences.client.content_types[this.ready_doc.value._type].schema['title'] = { label: 'Title', widget: { @@ -451,8 +462,10 @@ if (console == undefined) }, hidden: true }; + console.log(self.ready_doc); var title_val = ''; $.each($.ajatus.preferences.client.content_types[this.ready_doc.value._type]['title_item'], function(i,part){ + console.log("part: "+part); if (self.ready_doc.value[part]) { title_val += self.ready_doc.value[part].val; } else { @@ -460,6 +473,8 @@ if (console == undefined) } }); + console.log("title_val: "+title_val); + this.ready_doc.value['title'] = { val: title_val, widget: $.ajatus.preferences.client.content_types[this.ready_doc.value._type].schema['title'].widget @@ -819,12 +834,10 @@ if (console == undefined) } } - $.jqcouch.on_error = function(request,caller) - { + $.jqcouch.on_error = function(request,caller) { $.ajatus.ajax_error(request, caller); }; - $.jqcouch.on_success = function(data,caller) - { + $.jqcouch.on_success = function(data,caller) { $.ajatus.ajax_success(data, caller); }; @@ -906,8 +919,7 @@ if (console == undefined) // } }; - $.ajatus.start = function() - { + $.ajatus.start = function() { $.ajatus.events.lock_pool.increase(); $.ajatus.locker = new $.ajatus.events.lock({ watch: { @@ -1101,7 +1113,7 @@ if (console == undefined) })(jQuery); -jQuery(document).ready(function(){ +jQuery(document).ready(function() { jQuery('#application').initialize_ajatus({ debug: true, custom_views: [ diff --git a/js/ajatus.views.js b/js/ajatus.views.js index 2f910d2..e9373ac 100644 --- a/js/ajatus.views.js +++ b/js/ajatus.views.js @@ -74,6 +74,9 @@ if ( name && fn) { + if (typeof(fn) == 'string') { + fn = eval(fn); + } if ($.browser.safari) { static_views.views[name] = fn; } else { @@ -109,7 +112,8 @@ $.ajatus.views.generate = function(fn_str) { var fn = 'function(doc){'+fn_str+'};'; - return eval('('+fn+')'); + var func = eval(fn); + return func; } })(jQuery); \ No newline at end of file diff --git a/js/renderers/list.js b/js/renderers/list.js index f15776d..deeaf4d 100644 --- a/js/renderers/list.js +++ b/js/renderers/list.js @@ -2,8 +2,7 @@ $.ajatus = $.ajatus || {}; $.ajatus.renderer = $.ajatus.renderer || {}; - $.ajatus.renderer.list = function(target, content_type, options) - { + $.ajatus.renderer.list = function(target, content_type, options) { var self = this; this.options = $.extend({ className: 'list_holder', diff --git a/js/views/custom/test.js b/js/views/custom/test.js index 007ce28..ac77c82 100644 --- a/js/views/custom/test.js +++ b/js/views/custom/test.js @@ -6,7 +6,7 @@ options: { }, update_statics: true, - + tab: { on_click: function(e) { @@ -26,14 +26,11 @@ { id: type.name + '_list_holder', headers: [ - 'id', 'revision', 'creator', 'created' + 'id', 'title', 'creator', 'created' ], schema: $.extend({ id: { label: 'Id' - }, - revision: { - label: 'Revision' } }, $.ajatus.document.metadata) } @@ -53,23 +50,28 @@ } }, + gen_dynamic_view: function() + { + var dynamic_view = 'return {"key": doc.value.metadata.created,"value": {"_type": doc.value._type, "id": {val: doc._id},'; + dynamic_view += '"title": doc.value.title,'; + + $.each($.ajatus.preferences.client.content_types, function(key,type){ + if (type['title_item']) { + $.each(type['title_item'], function(i,part){ + if (type.schema[part]) { + dynamic_view += '"'+part+'": doc.value.'+part+','; + } + }); + } + }); + + dynamic_view += '"creator": doc.value.metadata.creator, "created": doc.value.metadata.created}};'; + + return dynamic_view; + }, + statics: { - funny: function(doc) { - return { - "key": doc.value.metadata.created, - "value": { - "_type": doc.value._type, - "id": { - val: doc._id - }, - "revision": { - val: doc._rev - }, - "creator": doc.value.metadata.creator, - "created": doc.value.metadata.created - } - }; - } + funny: '$.ajatus.views.generate($.ajatus.views.custom.test.gen_dynamic_view())' } }; diff --git a/js/views/system.js b/js/views/system.js index be562a4..b050d69 100644 --- a/js/views/system.js +++ b/js/views/system.js @@ -82,6 +82,26 @@ }); } }; + $.ajatus.views.system.tags = { + title: 'Tags', + options: { + }, + in_tabs: true, + + tab: { + on_click: function(e) { + $.ajatus.tabs.on_click(e); + $.ajatus.views.system.tags.render(); + } + }, + + render: function() { + var jqcouch = $.jqcouch.view; + $.ajatus.application_content_area.html(''); + + var orphaned_view = $.ajatus.views.generate(''); + } + }; $.ajatus.views.system.trash = { title: 'Trash', options: { @@ -114,70 +134,78 @@ } } }; - - $.each($.ajatus.preferences.client.content_types, function(key,type){ - if (!type.views['list']) - { - return false; + + jqcouch.on_success = function(data, caller) + { + if (data.total_rows == 0) { + var msg = new $.ajatus.elements.message('Empty results', 'No deleted items found.'); + return; } + + $.ajatus.views.system.trash.render_results(data.rows); + }; - jqcouch.on_success = function(data, caller) + jqcouch.temp( + $.ajatus.preferences.client.application_database + '/content', { - if (data.total_rows == 0) { - var msg = new $.ajatus.elements.message('Empty results', 'No deleted items found.'); - return; - } - - var renderer = new $.ajatus.renderer.list( - $.ajatus.application_content_area, - false, - { - id: type.name + '_list_holder', - append: true, - title: type.title, - headers: [ - 'title', 'revised', 'by' - ], - schema: $.extend({ - by: { - label: 'By' - } - }, type.schema) - } - ); - - $.each(data.rows, function(i,doc){ - var doc = new $.ajatus.document(doc); - if (doc.value._type == type.name) { - renderer.set_actions( - [ - { - name: 'undelete', - title: 'Undelete', - click_action: '$.ajatus.document.actions.execute("undelete", doc);' - }, - { - name: 'delete_final', - title: 'Delete', - click_action: '$.ajatus.document.actions.execute("delete_final", doc);' - } - ] - ); - renderer.render_item(doc, i); - } - }); - - renderer.enable_sorting(); - }; + //TODO: Get this from global listing settings + count: $.ajatus.views.system.frontpage.options.group_item_count, + reverse: true + }, + trash_view + ); + }, + + render_results: function(rows) + { + $.each($.ajatus.preferences.client.content_types, function(key,type){ + if (!type.views['list']) { + return false; + } - jqcouch.temp( - $.ajatus.preferences.client.application_database + '/content', + var renderer = new $.ajatus.renderer.list( + $.ajatus.application_content_area, + false, { - count: $.ajatus.views.system.frontpage.options.group_item_count, - reverse: true - }, - trash_view + id: type.name + '_list_holder', + append: true, + title: type.title, + headers: [ + 'title', 'revised', 'by' + ], + schema: $.extend({ + by: { + label: 'By' + } + }, type.schema) + } ); + + $.each(rows, function(i,doc){ + if (doc.value._type == type.name) { + console.log("doc: "); + console.log(doc); + var doc = new $.ajatus.document(doc); + + renderer.set_actions( + [ + { + name: 'undelete', + title: 'Undelete', + click_action: '$.ajatus.document.actions.execute("undelete", doc);' + }, + { + name: 'delete_final', + title: 'Delete', + click_action: '$.ajatus.document.actions.execute("delete_final", doc);' + } + ] + ); + renderer.render_item(doc, i); + } + }); + + renderer.enable_sorting(); }); } }; -- 2.11.4.GIT