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 || {};
18 $.ajatus.renderer_defaults = {
21 max_item_before_pool: 20,
26 $.ajatus.renderer = function(settings, r) {
27 this.settings = $.extend({}, $.ajatus.renderer_defaults, (settings || {}));
29 if (this.settings.type == 'block') {
30 this.handler = new $.ajatus.renderer._block(this.settings, r);
31 } else if (this.settings.type == 'scroll') {
32 this.handler = new $.ajatus.renderer._scroll(this.settings, r);
35 $.extend($.ajatus.renderer.prototype, {
36 add_item: function(callback, args) {
37 this.handler.add_item(callback, args);
40 items_added: function(doc_count) {
41 if (typeof this.handler.items_added == 'function') {
42 this.handler.items_added(doc_count);
51 $.ajatus.renderer._block = function(settings, renderer) {
52 this.settings = $.extend({
54 max_item_before_pool: 20
56 this.render_lock = false;
60 this.renderer = renderer;
62 $.extend($.ajatus.renderer._block.prototype, {
63 add_item: function(callback, args) {
68 this.items.push(item);
69 $.ajatus.events.named_lock_pool.increase('block_renderer');
71 if (! this.render_lock) {
80 _on_tick: function() {
82 if (this.items.length == 0)
84 $.ajatus.events.named_lock_pool.clear('block_renderer')
87 this.items = $.grep(this.items, function(n, i){
88 if (i < _self.settings.tick_limit)
90 var functionToCall = eval(n.callback);
92 if (functionToCall.apply(functionToCall, n.args)) {
93 _self.done_items.push(i);
94 $.ajatus.events.named_lock_pool.decrease('block_renderer');
100 this.renderer.enable_sorting();
103 _create_lock: function() {
105 _self.render_lock = new $.ajatus.events.lock({
106 disable_application: false,
108 validate: function() {
110 return $.ajatus.events.named_lock_pool.count('block_renderer') == 0;
115 on_release: function() {
116 _self._lock_finished();
121 _lock_finished: function() {
125 $.ajatus.renderer._scroll = function(settings, renderer) {
126 this.settings = $.extend({
128 preload_distance: 1000,
133 this.render_lock = false;
135 this.renderer = renderer;
138 this.done_items = [];
139 this.started = false;
141 this.status_holder = null;
144 this.from_back_btn = false;
146 this.scroller_id = $.ajatus.utils.generate_id();
147 this.mouse_state = 'up';
153 $.extend($.ajatus.renderer._scroll.prototype, {
154 add_item: function(callback, args) {
159 this.items.push(item);
160 $.ajatus.events.named_lock_pool.decrease('scroll_renderer');
163 items_added: function(total_docs) {
164 if (! this.started) {
168 if ( this.settings.use_db
169 && typeof total_docs != 'undefined')
171 this.total_docs = total_docs;
172 this.page_count = (total_docs / this.settings.items_per_page);
174 this.page_count = (this.items.length / this.settings.items_per_page);
177 $('div.renderer_scroll_status_holder .total_pages').val(this.page_count);
180 load_items: function(d) {
181 if ( typeof d == 'undefined'
188 if (typeof this.settings.db.static != 'undefined') {
189 var d = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, this.settings.db.static, opts).rows[0];
191 var d = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, this.settings.db.temp, opts).rows[0];
196 var on_success = function(data) {
197 $.each(data.rows, function(i,doc){
198 var doc = new $.ajatus.document(doc);
200 if ($.ajatus.tags.active != '') {
201 if ($.ajatus.utils.array.has_match($.ajatus.tags.active, doc.value.tags.val)) {
202 _self.renderer.render_item(doc, i);
205 _self.renderer.render_item(doc, i);
215 startkey_docid: d._id || d.id,
216 count: this.settings.items_per_page
218 if (typeof this.settings.db.static != 'undefined') {
219 $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, this.settings.db.static, opts);
221 $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, this.settings.db.temp, opts);
230 $.ajatus.events.named_lock_pool.clear('scroll_renderer');
232 if (this.status_holder == null) {
233 this.status_holder = $('<div class="renderer_scroll_status_holder" />').appendTo($.ajatus.application_content_area).hide();
234 $('<input type="hidden" name="scroll_id" class="scroll_id" />').val(this.scroller_id).appendTo(this.status_holder);
235 $('<input type="hidden" name="scroll_total_pages" class="total_pages" />').val('1').appendTo(this.status_holder);
236 $('<input type="hidden" name="scroll_history_index" class="history_index" />').val('').appendTo(this.status_holder);
237 $('<input type="hidden" name="scroll_mouse_state" class="mouse_state" />').val('up').appendTo(this.status_holder);
240 if (this.spacer == null) {
241 this.spacer = $('<div class="renderer_scroll_spacer" />').appendTo($.ajatus.application_content_area).css({
242 position: 'absolute',
250 $(document).mousedown(function(e){
251 $('.mouse_state', _self.status_holder).val('down');
252 _self.mouse_state = 'down';
254 $.cookie(_self.scroller_id+"_height", $(document).scrollTop());
255 $.cookie(_self.scroller_id+"_scroll", $(document).scrollTop() - $(document).height());
257 $(document).mouseup(function(e){
258 $('.mouse_state', _self.status_holder).val('up');
259 _self.mouse_state = 'up';
262 if ($.cookie(this.scroller_id)) {
263 this.from_back_btn = true;
265 $.cookie(this.scroller_id, "1");
268 if( this.from_back_btn ) {
269 $(".renderer_scroll_spacer").css({
270 height: $.cookie(this.scroller_id+"_height") + "px"
272 scroll(0, $.cookie(this.scroller_id+"_scroll") );
275 $('.history_index', this.status_holder).val('1');
279 $.ajatus.views.on_change_actions.add('$.ajatus.events.named_lock_pool.clear("scroll_renderer");');
284 _on_tick: function() {
287 if ( !this.settings.use_db
288 && this.items.length == 0)
290 $.ajatus.events.named_lock_pool.clear('scroll_renderer');
294 // console.log("_on_tick this.page_count: "+this.page_count);
295 // console.log("_on_tick history_index: "+$('.history_index', this.status_holder).val());
296 // console.log("_on_tick this.items.length: "+this.items.length);
298 if (this.mouse_state == 'up' && $(document).height() - $(document).scrollTop() < this.settings.preload_distance) {
299 if (this.settings.use_db) {
301 $.each(this.items, function(i,n){
302 last_doc = n.args[1];
304 var functionToCall = eval(n.callback);
306 if (functionToCall.apply(functionToCall, n.args)) {
307 $.ajatus.events.named_lock_pool.decrease('scroll_renderer');
308 _self.done_items += 1;
313 if (this.done_items < this.total_docs) {
314 this.load_items(last_doc);
317 this.items = $.grep(this.items, function(n, i){
318 if (i < _self.settings.items_per_page)
320 var functionToCall = eval(n.callback);
322 if (functionToCall.apply(functionToCall, n.args)) {
323 $.ajatus.events.named_lock_pool.decrease('scroll_renderer');
331 var hi = $('.history_index', this.status_holder).val();
332 $('.history_index', this.status_holder).val(parseInt(hi)+1);
333 this.renderer.enable_sorting();
336 if (parseInt($('.history_index', this.status_holder).val()) > this.page_count && this.page_count != 1 && this.items.length <= 0) {
337 $.ajatus.events.named_lock_pool.clear('scroll_renderer');
342 _create_lock: function() {
344 _self.render_lock = new $.ajatus.events.lock({
345 disable_application: false,
347 validate: function() {
349 return $.ajatus.events.named_lock_pool.count('scroll_renderer') == 0;
355 on_release: function() {
356 _self._lock_finished();
361 _lock_finished: function() {