Updated Ajatus to use the new jqCouch library
[ajatus.git] / js / ajatus.preferences.js
blobcd7a11d20010574916a538f976ade978d38e0b63
1 /*
2 * This file is part of
4 * Ajatus - Distributed CRM
5 * @requires jQuery v1.2.1
6 *
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
15 (function($){
16 $.ajatus = $.ajatus || {};
18 $.ajatus.preferences = {
19 revision: null,
20 modified: false
23 $.ajatus.preferences.local = {};
24 $.ajatus.preferences.local_defaults = {
25 user: {
26 name: 'Root Ajatus',
27 email: 'root@ajatus.info'
29 replication: {
30 enabled: false,
31 public_key: '',
32 allowed_keys: ''
34 layout: {
35 theme: 'default'
37 localization: {
38 language: 'en_GB'
40 backups: {
41 enabled: false,
42 auto: false
44 expansions: {
45 active: []
49 $.ajatus.preferences.client = {};
50 $.ajatus.preferences.client_defaults = {
51 debug: false,
52 language: null,
53 server_url: '/',
54 theme_url: 'themes/default/',
55 application_url: '/_utils/ajatus/',
56 application_database_identifier: '',
57 content_types: {},
58 custom_views: [],
59 custom_widgets: [],
60 extensions: [],
61 types: {
62 in_use: [
63 'note', 'contact', 'event', 'expense', 'hour_report'
65 system: [
66 'tag'
69 themes: {
70 available: [
71 'default'
76 $.ajatus.preferences.loader = function() {
77 this.loaded = false;
79 var on_success = function(data) {
80 this.loaded = true;
82 $.ajatus.events.lock_pool.decrease();
83 $.ajatus.preferences.revision = data._rev;
85 $.ajatus.preferences.local = data.value;
86 $.each($.ajatus.preferences.local_defaults, function(i,n){
87 if (typeof $.ajatus.preferences.local[i] == 'undefined') {
88 $.ajatus.preferences.modified = true;
89 $.ajatus.preferences.local[i] = n;
91 if (typeof n == 'object') {
92 $.each(n, function(xi,xn){
93 if (typeof $.ajatus.preferences.local[i][xi] == 'undefined') {
94 $.ajatus.preferences.modified = true;
95 $.ajatus.preferences.local[i][xi] = xn;
97 });
99 });
101 return data;
104 this.dc = $.jqCouch.connection('doc', on_success);
106 $.extend($.ajatus.preferences.loader.prototype, {
107 load: function() {
108 $.ajatus.events.lock_pool.increase();
109 var data = this.dc.get($.ajatus.preferences.client.application_database + '/preferences');
110 if (! data._id) {
111 this.loaded = false;
112 $.ajatus.events.lock_pool.decrease();
113 $.ajatus.ajax_error(data.request);
118 $.ajatus.preferences.view = {
119 title: 'Preferences',
120 options: {},
121 sections: [
122 'user', 'localization', 'layout', 'replication', 'backups', 'expansions'
125 tab: {
126 on_click: function(e)
128 $.ajatus.tabs.on_click(e);
129 $.ajatus.preferences.view.render();
133 render: function() {
134 $.ajatus.application_content_area.html('');
135 $.ajatus.layout.body.set_class('preferences');
136 $.ajatus.views.on_change("#view.preferences");
138 $.ajatus.toolbar.show();
140 var form = $('<form name="preferences" id="preferences_form"/>');
141 form.appendTo($.ajatus.application_content_area);
143 var hidden_data = {
144 _id: 'preferences',
145 _rev: $.ajatus.preferences.revision
147 var hidden_fields = function() {
148 return [
149 'input', { type: 'hidden', id: 'preferences_id', name: '_id', value: this._id }, '',
150 'input', { type: 'hidden', id: 'preferences_revision', name: '_rev', value: this._rev }, ''
153 $(form).tplAppend(hidden_data, hidden_fields);
155 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>');
157 var sections_holder = $('<div id="preference_sections" />');
158 sections_holder.appendTo(form);
160 $.each($.ajatus.preferences.view.sections, function(i,s){
161 var section_holder = $('<div class="section" id="section_' + s + '" />');
162 section_holder.appendTo(sections_holder);
164 var form_items = $.ajatus.preferences.view.generate_section_form(s);
166 if (form_items) {
167 var title = $('<h3 />').html($.ajatus.i10n.get(s));
168 title.appendTo(section_holder);
170 form_items.appendTo(section_holder);
174 form_actions.appendTo(form);
176 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Save'), 'images/icons/save.png', function(){
177 $('#preferences_form input[@type=submit][name*=save]').trigger("click", []);
179 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Cancel'), 'images/icons/cancel.png', function(){
180 $('#preferences_form input[@type=submit][name*=cancel]').trigger("click", []);
183 $.ajatus.forms.register.custom(form, '$.ajatus.preferences.view.process_form');
186 process_form: function(form_data, form) {
187 // $.ajatus.debug('Process preferences');
188 // console.log(form_data);
190 var doc = {
191 _id: '',
192 _rev: '',
193 value: {}
195 var form_values = {};
196 var form_id = '';
198 $.each(form_data, function(i,row){
199 if (row.name.toString().match(/__(.*?)/)) {
200 return;
202 if ( row.name == '_id'
203 && ( row.value != undefined
204 && row.value != ''))
206 doc['_id'] = String(row.value);
207 form_id = doc['_id'];
209 else if( row.name == '_rev'
210 && ( typeof row.value != "undefined"
211 && row.value != ''))
213 doc['_rev'] = String(row.value);
214 } else {
215 if (row.name != 'submit') {
216 if (row.name == '_type') {
218 else if ( row.name.substr(0,6) != "widget"
219 && row.name.substr(0,8) != "metadata")
221 var item = {};
222 var widget = {};
223 var prev_val = false;
224 var name_parts = [];
225 var name_parts_count = 0;
226 if (row.name.toString().match(/;/g)) {
227 name_parts = row.name.toString().split(";");
228 name_parts_count = name_parts.length;
231 $.each(form_data, function(x,r){
232 if (r.name == 'widget['+row.name+':name]') {
233 widget['name'] = r.value;
234 } else if (r.name == 'widget['+row.name+':config]') {
235 widget['config'] = $.ajatus.converter.parseJSON(r.value);
236 } else if (r.name == 'widget['+row.name+':prev_val]') {
237 prev_val = $.ajatus.converter.parseJSON(r.value);
241 var wdgt = new $.ajatus.widget(widget['name'], widget['config']);
242 item['val'] = wdgt.value_on_save(row.value, prev_val);
243 item['widget'] = widget;
245 if (name_parts_count > 0) {
246 var prevs = [];
247 for (var i=0; i < name_parts_count; i++) {
248 var arr_keys = false;
249 var key_prefix = '';
251 if (name_parts[i].match(/\[/g)) {
252 var arr_keys = name_parts[i].split('[');
254 name_parts[i] = name_parts[i].replace(/\[/g, '"][');
255 var key = '["'+name_parts[i];
256 } else {
257 var key = "['"+name_parts[i]+"']";
260 if (prevs.length > 0) {
261 $.each(prevs, function(pi, pk){
262 key_prefix = "['" + pk + "']" + key_prefix;
264 key = key_prefix + key;
267 if (arr_keys) {
268 var tmp_key = key_prefix + "['" + arr_keys[0] + "']";
269 if (typeof eval("form_values"+tmp_key) == 'undefined') {
270 eval("form_values"+tmp_key+"=[];");
274 var multiple = false;
275 if (typeof eval("form_values"+key) == 'undefined') {
276 if (key_prefix != '') {
277 eval("form_values"+key+"=new Array();");
278 } else {
279 eval("form_values"+key+"={};");
281 } else {
282 multiple = true;
285 prevs.push(name_parts[i]);
286 if (i == name_parts_count-1) {
287 if (item.val == '') {
288 return;
290 if ($.browser.mozilla) {
291 if (multiple) {
292 if (typeof item == 'object') {
293 // eval("form_values"+key+".push('"+item.toSource()+"');");
294 eval("form_values"+key+".push('"+item.val+"');");
295 } else {
296 eval("form_values"+key+".push('"+item.val+"');");
298 } else {
299 if (typeof item == 'object') {
300 // eval("form_values"+key+"="+item.toSource()+";");
301 eval("form_values"+key+"='"+item.val+"';");
302 } else {
303 eval("form_values"+key+"='"+item.val+"';");
306 } else {
307 eval("form_values"+key+"="+item+";");
311 } else {
312 form_values[row.name] = item;
315 else if (row.name.substr(0,8) == "metadata")
317 if (!form_values['metadata'])
319 form_values['metadata'] = {};
322 var re = /\bmetadata\[([a-z]+)\b/;
323 var results = re.exec(row.name);
324 var key = results[1];
326 form_values['metadata'][key] = {
327 val: row.value
333 doc['value'] = form_values;
335 doc = new $.ajatus.document(doc);
337 $.ajatus.preferences.view.save(doc.value);
340 save: function(preferences) {
341 var saved = false;
343 var on_success = function(data) {
344 saved = true;
345 $.ajatus.preferences.revision = data.rev;
346 $.ajatus.events.named_lock_pool.decrease('unsaved');
347 window.location.reload();
349 return data;
352 prefs_doc = new $.ajatus.document({
353 _id: 'preferences',
354 _rev: $.ajatus.preferences.revision,
355 value: preferences
357 prefs_doc.value._type = 'preferences';
359 var new_metadata = {
360 revised: $.ajatus.formatter.date.js_to_iso8601(new Date()),
361 revisor: $.ajatus.preferences.local.user.email
363 prefs_doc = $.ajatus.document.modify_metadata(prefs_doc, new_metadata);
365 var prefs = {
366 metadata: prefs_doc.value.metadata
368 $.each($.ajatus.preferences.local_defaults, function(k,data){
369 if (typeof prefs[k] == 'undefined') {
370 prefs[k] = {};
373 if (typeof data == 'object') {
374 $.each(data, function(sk, sdata){
375 if (typeof prefs_doc.value[k] == 'undefined') {
376 prefs[k][sk] = sdata;
377 } else {
378 if (typeof prefs_doc.value[k][sk] == 'undefined') {
379 prefs[k][sk] = sdata;
380 } else {
381 if (typeof prefs_doc.value[k][sk]['val'] == 'undefined') {
382 prefs[k][sk] = prefs_doc.value[k][sk];
383 } else {
384 prefs[k][sk] = prefs_doc.value[k][sk].val;
389 } else {
390 if (typeof prefs_doc.value[k] == 'undefined') {
391 prefs[k] = data;
392 } else {
393 if (typeof prefs_doc.value[k]['val'] == 'undefined') {
394 prefs[k] = prefs_doc.value[k];
395 } else {
396 prefs[k] = prefs_doc.value[k].val;
401 prefs_doc.value = prefs;
403 $.jqCouch.connection('doc', on_success).save($.ajatus.preferences.client.application_database, prefs_doc);
405 return saved;
408 generate_section_form: function(section) {
409 if (typeof $.ajatus.preferences.local[section] == 'undefined') {
410 return false;
413 var items = false;
414 var section_data = $.ajatus.preferences.local[section];
416 function auto_gen(section, data) {
417 var rows = $('<ul/>');
419 $.each(data, function(i,s){
420 // console.log("i: "+i+" s: ");
421 // console.log(s);
422 var s_type = typeof(s);
423 // console.log('type: '+s_type);
424 var wdgt = null;
425 var normalized_name = i.toString().replace('_', ' ');
427 switch (s_type) {
428 case 'boolean':
429 wdgt = $.ajatus.widget('boolean');
430 break;
431 case 'number':
432 wdgt = $.ajatus.widget('integer');
433 break;
434 case 'string':
435 default:
436 wdgt = $.ajatus.widget('text');
439 rows.createAppend(
440 'li', { id: '_setting_'+section+'_'+i, className: 'row' }, [
441 'label', { id: '_setting_'+section+'_'+i+'_label' }, $.ajatus.i10n.get(normalized_name),
442 'div', { id: '_setting_'+section+'_'+i+'_widget', className: wdgt.name }, wdgt.get_edit_tpl(section+';'+i, {val: s}),
443 'br', { className: 'clear_fix' }, ''
446 wdgt.init($('#_setting_'+i+'_widget',rows), true);
449 $('li:first', rows).addClass('first');
450 $('li:last', rows).addClass('last');
452 return rows;
455 function gen_localization(data) {
456 var rows = $('<ul/>');
457 var langs = $.ajatus.i10n.get_available_langs();
458 var lang_opts = {};
460 $.each(langs, function(i,l) {
461 lang_opts[i] = l;
464 wdgt = $.ajatus.widget('selection', {
465 items: lang_opts
467 rows.createAppend(
468 'li', { id: '_setting_localization_language', className: 'row' }, [
469 'label', { id: '_setting_localization_language_label' }, $.ajatus.i10n.get('Language'),
470 'div', { id: '_setting_localization_language_widget', className: wdgt.name }, wdgt.get_edit_tpl('localization;language', {val: data.language}),
471 'br', { className: 'clear_fix' }, ''
474 wdgt.init($('#_setting_localization_language_widget',rows), true);
476 return rows;
479 function gen_layout(data) {
480 var rows = $('<ul/>');
481 var themes = $.ajatus.preferences.client.themes.available;
482 var theme_opts = {};
484 $.each(themes, function(i,l) {
485 theme_opts[i] = $.ajatus.i10n.get(l);
488 wdgt = $.ajatus.widget('selection', {
489 items: theme_opts
491 rows.createAppend(
492 'li', { id: '_setting_layout_theme', className: 'row' }, [
493 'label', { id: '_setting_layout_theme_label' }, $.ajatus.i10n.get('Theme'),
494 'div', { id: '_setting_layout_theme_widget', className: wdgt.name }, wdgt.get_edit_tpl('layout;theme', {val: data.theme}),
495 'br', { className: 'clear_fix' }, ''
498 wdgt.init($('#_setting_layout_theme_widget', rows), true);
500 return rows;
503 // Example how to handle arrays with checkboxes
504 // function gen_types(data) {
505 // var rows = $('<ul />');
507 // $.each($.ajatus.types.available, function(key,type){
508 // // console.log("key: "+key);
509 // // console.log(type);
511 // var selected = false;
512 // $.each(data.active, function(i,t){
513 // if (t == key) {
514 // selected = true;
515 // }
516 // });
518 // var widget = new $.ajatus.widget('boolean', {
519 // value: key
520 // });
522 // rows.createAppend(
523 // 'li', { id: '_setting_types_active_'+key, className: 'row' }, [
524 // 'label', { id: '_setting_types_active_'+key+'_label' }, $.ajatus.i10n.get(type.title),
525 // 'div', { id: '_setting_types_active_'+key+'_widget', className: widget.name }, widget.get_edit_tpl('types;active', {val: (selected ? key : '')}), //["'+key+'"]
526 // 'br', { className: 'clear_fix' }, ''
527 // ]
528 // );
529 // widget.init($('#_setting_types_active_'+key+'_widget',rows), true);
530 // });
532 // return rows;
533 // }
535 switch (section) {
536 case 'localization':
537 items = gen_localization(section_data);
538 break;
539 case 'layout':
540 items = gen_layout(section_data);
541 break;
542 case 'replication':
543 case 'backups':
544 case 'expansions':
545 break;
546 default:
547 items = auto_gen(section, section_data);
550 return items;
554 })(jQuery);