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.preferences = {
23 $.ajatus.preferences.local = {};
24 $.ajatus.preferences.local_defaults = {
27 email: 'root@ajatus.info'
35 theme_name: 'default',
54 $.ajatus.preferences.client = {};
55 $.ajatus.preferences.client_defaults = {
59 theme_url: 'themes/default/',
60 theme_icons_url: 'themes/default/images/icons/',
61 application_url: '/_utils/ajatus/',
62 application_database_identifier: '',
69 'note', 'contact', 'event', 'expense', 'hour_report'
81 // expire: (24 * 60 * 60) // 24hrs in seconds
85 $.ajatus.preferences.loader = function() {
88 var on_success = function(data) {
91 $.ajatus.events.lock_pool.decrease();
92 $.ajatus.preferences.revision = data._rev;
94 $.ajatus.preferences.local = data.value;
95 $.each($.ajatus.preferences.local_defaults, function(i,n){
96 if (typeof $.ajatus.preferences.local[i] == 'undefined') {
97 $.ajatus.preferences.modified = true;
98 $.ajatus.preferences.local[i] = n;
100 if (typeof n == 'object') {
101 $.each(n, function(xi,xn){
102 if (typeof $.ajatus.preferences.local[i][xi] == 'undefined') {
103 $.ajatus.preferences.modified = true;
104 $.ajatus.preferences.local[i][xi] = xn;
113 this.dc = $.jqCouch.connection('doc', on_success);
115 $.extend($.ajatus.preferences.loader.prototype, {
117 $.ajatus.events.lock_pool.increase();
118 var data = this.dc.get($.ajatus.preferences.client.application_database + '/preferences');
121 $.ajatus.events.lock_pool.decrease();
122 $.ajatus.ajax_error(data.request);
127 $.ajatus.preferences.view = {
128 title: 'Preferences',
129 icon: 'preferences.png',
132 'user', 'localization', 'layout', 'replication', 'backups', 'expansions', 'auto_save'
136 on_click: function(e)
138 $.ajatus.tabs.on_click(e);
139 $.ajatus.preferences.view.render();
144 $.ajatus.application_content_area.html('');
145 $.ajatus.layout.body.set_class('preferences');
146 $.ajatus.views.on_change("#view.preferences");
148 $.ajatus.toolbar.show();
150 var form = $('<form name="preferences" id="preferences_form"/>');
151 form.appendTo($.ajatus.application_content_area);
155 _rev: $.ajatus.preferences.revision
157 var hidden_fields = function() {
159 'input', { type: 'hidden', id: 'preferences_id', name: '_id', value: this._id }, '',
160 'input', { type: 'hidden', id: 'preferences_revision', name: '_rev', value: this._rev }, ''
163 $(form).tplAppend(hidden_data, hidden_fields);
165 var form_actions = $('<div class="form_actions"><input type="submit" name="save" value="' + $.ajatus.i10n.get('Save') + '" /><input type="submit" name="cancel" value="' + $.ajatus.i10n.get('Cancel') + '" /></div>');
167 var sections_holder = $('<div id="preference_sections" />');
168 sections_holder.appendTo(form);
170 $.each($.ajatus.preferences.view.sections, function(i,s){
171 var section_holder = $('<div class="section" id="section_' + s + '" />');
172 section_holder.appendTo(sections_holder);
174 var form_items = $.ajatus.preferences.view.generate_section_form(s);
177 var title = $('<h3 />').html($.ajatus.i10n.get(s));
178 title.appendTo(section_holder);
180 form_items.appendTo(section_holder);
184 form_actions.appendTo(form);
186 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Save'), 'save.png', function(){
187 $('#preferences_form input[@type=submit][name*=save]').trigger("click", []);
189 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Cancel'), 'cancel.png', function(){
190 $('#preferences_form input[@type=submit][name*=cancel]').trigger("click", []);
193 $.ajatus.forms.register.custom(form, '$.ajatus.preferences.view.process_form');
196 process_form: function(form_data, form) {
197 // $.ajatus.debug('Process preferences');
198 // console.log(form_data);
205 var form_values = {};
208 $.each(form_data, function(i,row){
209 if (row.name.toString().match(/__(.*?)/)) {
212 if ( row.name == '_id'
213 && ( row.value != undefined
216 doc['_id'] = String(row.value);
217 form_id = doc['_id'];
219 else if( row.name == '_rev'
220 && ( typeof row.value != "undefined"
223 doc['_rev'] = String(row.value);
225 if (row.name != 'submit') {
226 if (row.name == '_type') {
228 else if ( row.name.substr(0,6) != "widget"
229 && row.name.substr(0,8) != "metadata")
233 var prev_val = false;
235 var name_parts_count = 0;
236 if (row.name.toString().match(/;/g)) {
237 name_parts = row.name.toString().split(";");
238 name_parts_count = name_parts.length;
241 $.each(form_data, function(x,r){
242 if (r.name == 'widget['+row.name+':name]') {
243 widget['name'] = r.value;
244 } else if (r.name == 'widget['+row.name+':config]') {
245 widget['config'] = $.ajatus.converter.parseJSON(r.value);
246 } else if (r.name == 'widget['+row.name+':prev_val]') {
247 prev_val = $.ajatus.converter.parseJSON(r.value);
251 var wdgt = new $.ajatus.widget(widget['name'], widget['config']);
252 item['val'] = wdgt.value_on_save(row.value, prev_val);
253 item['widget'] = widget;
255 if (name_parts_count > 0) {
257 for (var i=0; i < name_parts_count; i++) {
258 var arr_keys = false;
261 if (name_parts[i].match(/\[/g)) {
262 var arr_keys = name_parts[i].split('[');
264 name_parts[i] = name_parts[i].replace(/\[/g, '"][');
265 var key = '["'+name_parts[i];
267 var key = "['"+name_parts[i]+"']";
270 if (prevs.length > 0) {
271 $.each(prevs, function(pi, pk){
272 key_prefix = "['" + pk + "']" + key_prefix;
274 key = key_prefix + key;
278 var tmp_key = key_prefix + "['" + arr_keys[0] + "']";
279 if (typeof eval("form_values"+tmp_key) == 'undefined') {
280 eval("form_values"+tmp_key+"=[];");
284 var multiple = false;
285 if (typeof eval("form_values"+key) == 'undefined') {
286 if (key_prefix != '') {
287 eval("form_values"+key+"=new Array();");
289 eval("form_values"+key+"={};");
295 prevs.push(name_parts[i]);
296 if (i == name_parts_count-1) {
297 if (typeof item['val'] == 'undefined') {
301 if (typeof item == 'object') {
302 // eval("form_values"+key+".push('"+item.toSource()+"');");
303 if (item.widget.name == 'boolean') {
304 eval("form_values"+key+"="+item.val+";");
306 eval("form_values"+key+".push('"+item.val+"');");
309 eval("form_values"+key+".push('"+item+"');");
312 if (typeof item == 'object') {
313 eval("form_values"+key+"='"+item.val+"';");
315 eval("form_values"+key+"='"+item.val+"';");
321 form_values[row.name] = item;
324 else if (row.name.substr(0,8) == "metadata")
326 if (! form_values['metadata']) {
327 form_values['metadata'] = {};
330 var re = /\bmetadata\[([a-z]+)\b/;
331 var results = re.exec(row.name);
332 var key = results[1];
334 form_values['metadata'][key] = {
341 doc['value'] = form_values;
343 doc = new $.ajatus.document(doc);
345 $.ajatus.preferences.view.save(doc.value);
348 save: function(preferences) {
351 var on_success = function(data) {
353 $.ajatus.preferences.revision = data.rev;
354 $.ajatus.events.named_lock_pool.decrease('unsaved');
355 window.location.reload();
360 prefs_doc = new $.ajatus.document({
362 _rev: $.ajatus.preferences.revision,
365 prefs_doc.value._type = 'preferences';
368 revised: $.ajatus.formatter.date.js_to_iso8601(new Date()),
369 revisor: $.ajatus.preferences.local.user.email
371 prefs_doc = $.ajatus.document.modify_metadata(prefs_doc, new_metadata);
374 metadata: prefs_doc.value.metadata
376 $.each($.ajatus.preferences.local_defaults, function(k,data){
377 if (typeof prefs[k] == 'undefined') {
381 if (typeof data == 'object') {
382 $.each(data, function(sk, sdata){
383 if (typeof prefs_doc.value[k] == 'undefined') {
384 prefs[k][sk] = sdata;
386 if (typeof prefs_doc.value[k][sk] == 'undefined') {
387 prefs[k][sk] = sdata;
389 if (typeof prefs_doc.value[k][sk]['val'] == 'undefined') {
390 prefs[k][sk] = prefs_doc.value[k][sk];
392 prefs[k][sk] = prefs_doc.value[k][sk].val;
398 if (typeof prefs_doc.value[k] == 'undefined') {
401 if (typeof prefs_doc.value[k]['val'] == 'undefined') {
402 prefs[k] = prefs_doc.value[k];
404 prefs[k] = prefs_doc.value[k].val;
410 prefs_doc.value = prefs;
412 $.jqCouch.connection('doc', on_success).save($.ajatus.preferences.client.application_database, prefs_doc);
417 generate_section_form: function(section) {
418 if (typeof $.ajatus.preferences.local[section] == 'undefined') {
423 var section_data = $.ajatus.preferences.local[section];
425 function auto_gen(section, data) {
426 var rows = $('<ul/>');
428 $.each(data, function(i,s){
429 // console.log("i: "+i+" s: ");
431 var s_type = typeof(s);
432 // console.log('type: '+s_type);
434 var normalized_name = i.toString().replace('_', ' ');
438 wdgt = $.ajatus.widget('boolean');
441 wdgt = $.ajatus.widget('integer');
445 wdgt = $.ajatus.widget('text');
449 'li', { id: '_setting_'+section+'_'+i, className: 'row' }, [
450 'label', { id: '_setting_'+section+'_'+i+'_label' }, $.ajatus.i10n.get(normalized_name),
451 'div', { id: '_setting_'+section+'_'+i+'_widget', className: wdgt.name }, wdgt.get_edit_tpl(section+';'+i, {val: s}),
452 'br', { className: 'clear_fix' }, ''
455 wdgt.init($('#_setting_'+i+'_widget',rows), true);
458 $('li:first', rows).addClass('first');
459 $('li:last', rows).addClass('last');
464 function gen_localization(data) {
465 var rows = $('<ul/>');
466 var langs = $.ajatus.i10n.get_available_langs();
469 $.each(langs, function(i,l) {
473 wdgt = $.ajatus.widget('selection', {
477 'li', { id: '_setting_localization_language', className: 'row' }, [
478 'label', { id: '_setting_localization_language_label' }, $.ajatus.i10n.get('Language'),
479 'div', { id: '_setting_localization_language_widget', className: wdgt.name }, wdgt.get_edit_tpl('localization;language', {val: data.language}),
480 'br', { className: 'clear_fix' }, ''
483 wdgt.init($('#_setting_localization_language_widget',rows), true);
488 function gen_layout(data) {
489 var rows = $('<ul/>');
490 var themes = $.ajatus.preferences.client.themes.available;
493 $.each(themes, function(i,l) {
494 theme_opts[i] = $.ajatus.i10n.get(l);
497 wdgt = $.ajatus.widget('selection', {
501 'li', { id: '_setting_layout_theme_name', className: 'row' }, [
502 'label', { id: '_setting_layout_theme_name_label' }, $.ajatus.i10n.get('Theme'),
503 'div', { id: '_setting_layout_theme_name_widget', className: wdgt.name }, wdgt.get_edit_tpl('layout;theme_name', {val: data.theme_name}),
504 'br', { className: 'clear_fix' }, ''
507 wdgt.init($('#_setting_layout_theme_name_widget', rows), true);
512 function gen_autosave(data) {
513 var rows = $('<ul/>');
514 var section = 'auto_save';
517 $.each(data, function(i,s){
518 // console.log("i: "+i+" s: ");
520 var s_type = typeof(s);
521 // console.log('type: '+s_type);
523 var normalized_name = i.toString().replace('_', ' ');
527 wdgt = $.ajatus.widget('boolean');
530 wdgt = $.ajatus.widget('integer',{
535 wdgt = $.ajatus.widget('text');
539 'li', { id: '_setting_'+section+'_'+i, className: 'row' }, [
540 'label', { id: '_setting_'+section+'_'+i+'_label' }, $.ajatus.i10n.get(normalized_name),
541 'div', { id: '_setting_'+section+'_'+i+'_widget', className: wdgt.name }, wdgt.get_edit_tpl(section+';'+i, {val: s}),
542 'br', { className: 'clear_fix' }, ''
545 wdgt.init($('#_setting_'+i+'_widget',rows), true);
548 $('li:first', rows).addClass('first');
549 $('li:last', rows).addClass('last');
554 // Example how to handle arrays with checkboxes
555 // function gen_types(data) {
556 // var rows = $('<ul />');
558 // $.each($.ajatus.types.available, function(key,type){
559 // // console.log("key: "+key);
560 // // console.log(type);
562 // var selected = false;
563 // $.each(data.active, function(i,t){
569 // var widget = new $.ajatus.widget('boolean', {
573 // rows.createAppend(
574 // 'li', { id: '_setting_types_active_'+key, className: 'row' }, [
575 // 'label', { id: '_setting_types_active_'+key+'_label' }, $.ajatus.i10n.get(type.title),
576 // 'div', { id: '_setting_types_active_'+key+'_widget', className: widget.name }, widget.get_edit_tpl('types;active', {val: (selected ? key : '')}), //["'+key+'"]
577 // 'br', { className: 'clear_fix' }, ''
580 // widget.init($('#_setting_types_active_'+key+'_widget',rows), true);
588 items = gen_localization(section_data);
591 items = gen_layout(section_data);
594 items = gen_autosave(section_data);
601 items = auto_gen(section, section_data);