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'
82 $.ajatus.preferences.loader = function() {
85 var on_success = function(data) {
88 $.ajatus.events.lock_pool.decrease();
89 $.ajatus.preferences.revision = data._rev;
91 $.ajatus.preferences.local = data.value;
92 $.each($.ajatus.preferences.local_defaults, function(i,n){
93 if (typeof $.ajatus.preferences.local[i] == 'undefined') {
94 $.ajatus.preferences.modified = true;
95 $.ajatus.preferences.local[i] = n;
97 if (typeof n == 'object') {
98 $.each(n, function(xi,xn){
99 if (typeof $.ajatus.preferences.local[i][xi] == 'undefined') {
100 $.ajatus.preferences.modified = true;
101 $.ajatus.preferences.local[i][xi] = xn;
110 this.dc = $.jqCouch.connection('doc', on_success);
112 $.extend($.ajatus.preferences.loader.prototype, {
114 $.ajatus.events.lock_pool.increase();
115 var data = this.dc.get($.ajatus.preferences.client.application_database + '/preferences');
118 $.ajatus.events.lock_pool.decrease();
119 $.ajatus.ajax_error(data.request);
124 $.ajatus.preferences.view = {
125 title: 'Preferences',
128 'user', 'localization', 'layout', 'replication', 'backups', 'expansions', 'auto_save'
132 on_click: function(e)
134 $.ajatus.tabs.on_click(e);
135 $.ajatus.preferences.view.render();
140 $.ajatus.application_content_area.html('');
141 $.ajatus.layout.body.set_class('preferences');
142 $.ajatus.views.on_change("#view.preferences");
144 $.ajatus.toolbar.show();
146 var form = $('<form name="preferences" id="preferences_form"/>');
147 form.appendTo($.ajatus.application_content_area);
151 _rev: $.ajatus.preferences.revision
153 var hidden_fields = function() {
155 'input', { type: 'hidden', id: 'preferences_id', name: '_id', value: this._id }, '',
156 'input', { type: 'hidden', id: 'preferences_revision', name: '_rev', value: this._rev }, ''
159 $(form).tplAppend(hidden_data, hidden_fields);
161 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>');
163 var sections_holder = $('<div id="preference_sections" />');
164 sections_holder.appendTo(form);
166 $.each($.ajatus.preferences.view.sections, function(i,s){
167 var section_holder = $('<div class="section" id="section_' + s + '" />');
168 section_holder.appendTo(sections_holder);
170 var form_items = $.ajatus.preferences.view.generate_section_form(s);
173 var title = $('<h3 />').html($.ajatus.i10n.get(s));
174 title.appendTo(section_holder);
176 form_items.appendTo(section_holder);
180 form_actions.appendTo(form);
182 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Save'), 'save.png', function(){
183 $('#preferences_form input[@type=submit][name*=save]').trigger("click", []);
185 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Cancel'), 'cancel.png', function(){
186 $('#preferences_form input[@type=submit][name*=cancel]').trigger("click", []);
189 $.ajatus.forms.register.custom(form, '$.ajatus.preferences.view.process_form');
192 process_form: function(form_data, form) {
193 // $.ajatus.debug('Process preferences');
194 // console.log(form_data);
201 var form_values = {};
204 $.each(form_data, function(i,row){
205 if (row.name.toString().match(/__(.*?)/)) {
208 if ( row.name == '_id'
209 && ( row.value != undefined
212 doc['_id'] = String(row.value);
213 form_id = doc['_id'];
215 else if( row.name == '_rev'
216 && ( typeof row.value != "undefined"
219 doc['_rev'] = String(row.value);
221 if (row.name != 'submit') {
222 if (row.name == '_type') {
224 else if ( row.name.substr(0,6) != "widget"
225 && row.name.substr(0,8) != "metadata")
229 var prev_val = false;
231 var name_parts_count = 0;
232 if (row.name.toString().match(/;/g)) {
233 name_parts = row.name.toString().split(";");
234 name_parts_count = name_parts.length;
237 $.each(form_data, function(x,r){
238 if (r.name == 'widget['+row.name+':name]') {
239 widget['name'] = r.value;
240 } else if (r.name == 'widget['+row.name+':config]') {
241 widget['config'] = $.ajatus.converter.parseJSON(r.value);
242 } else if (r.name == 'widget['+row.name+':prev_val]') {
243 prev_val = $.ajatus.converter.parseJSON(r.value);
247 var wdgt = new $.ajatus.widget(widget['name'], widget['config']);
248 item['val'] = wdgt.value_on_save(row.value, prev_val);
249 item['widget'] = widget;
251 if (name_parts_count > 0) {
253 for (var i=0; i < name_parts_count; i++) {
254 var arr_keys = false;
257 if (name_parts[i].match(/\[/g)) {
258 var arr_keys = name_parts[i].split('[');
260 name_parts[i] = name_parts[i].replace(/\[/g, '"][');
261 var key = '["'+name_parts[i];
263 var key = "['"+name_parts[i]+"']";
266 if (prevs.length > 0) {
267 $.each(prevs, function(pi, pk){
268 key_prefix = "['" + pk + "']" + key_prefix;
270 key = key_prefix + key;
274 var tmp_key = key_prefix + "['" + arr_keys[0] + "']";
275 if (typeof eval("form_values"+tmp_key) == 'undefined') {
276 eval("form_values"+tmp_key+"=[];");
280 var multiple = false;
281 if (typeof eval("form_values"+key) == 'undefined') {
282 if (key_prefix != '') {
283 eval("form_values"+key+"=new Array();");
285 eval("form_values"+key+"={};");
291 prevs.push(name_parts[i]);
292 if (i == name_parts_count-1) {
293 if (item.val == '') {
297 if (typeof item == 'object') {
298 // eval("form_values"+key+".push('"+item.toSource()+"');");
299 eval("form_values"+key+".push('"+item.val+"');");
301 eval("form_values"+key+".push('"+item.val+"');");
304 if (typeof item == 'object') {
305 // eval("form_values"+key+"="+item.toSource()+";");
306 eval("form_values"+key+"='"+item.val+"';");
308 eval("form_values"+key+"='"+item.val+"';");
314 form_values[row.name] = item;
317 else if (row.name.substr(0,8) == "metadata")
319 if (!form_values['metadata'])
321 form_values['metadata'] = {};
324 var re = /\bmetadata\[([a-z]+)\b/;
325 var results = re.exec(row.name);
326 var key = results[1];
328 form_values['metadata'][key] = {
335 doc['value'] = form_values;
337 doc = new $.ajatus.document(doc);
339 $.ajatus.preferences.view.save(doc.value);
342 save: function(preferences) {
345 var on_success = function(data) {
347 $.ajatus.preferences.revision = data.rev;
348 $.ajatus.events.named_lock_pool.decrease('unsaved');
349 window.location.reload();
354 prefs_doc = new $.ajatus.document({
356 _rev: $.ajatus.preferences.revision,
359 prefs_doc.value._type = 'preferences';
362 revised: $.ajatus.formatter.date.js_to_iso8601(new Date()),
363 revisor: $.ajatus.preferences.local.user.email
365 prefs_doc = $.ajatus.document.modify_metadata(prefs_doc, new_metadata);
368 metadata: prefs_doc.value.metadata
370 $.each($.ajatus.preferences.local_defaults, function(k,data){
371 if (typeof prefs[k] == 'undefined') {
375 if (typeof data == 'object') {
376 $.each(data, function(sk, sdata){
377 if (typeof prefs_doc.value[k] == 'undefined') {
378 prefs[k][sk] = sdata;
380 if (typeof prefs_doc.value[k][sk] == 'undefined') {
381 prefs[k][sk] = sdata;
383 if (typeof prefs_doc.value[k][sk]['val'] == 'undefined') {
384 prefs[k][sk] = prefs_doc.value[k][sk];
386 prefs[k][sk] = prefs_doc.value[k][sk].val;
392 if (typeof prefs_doc.value[k] == 'undefined') {
395 if (typeof prefs_doc.value[k]['val'] == 'undefined') {
396 prefs[k] = prefs_doc.value[k];
398 prefs[k] = prefs_doc.value[k].val;
403 prefs_doc.value = prefs;
405 $.jqCouch.connection('doc', on_success).save($.ajatus.preferences.client.application_database, prefs_doc);
410 generate_section_form: function(section) {
411 if (typeof $.ajatus.preferences.local[section] == 'undefined') {
416 var section_data = $.ajatus.preferences.local[section];
418 function auto_gen(section, data) {
419 var rows = $('<ul/>');
421 $.each(data, function(i,s){
422 // console.log("i: "+i+" s: ");
424 var s_type = typeof(s);
425 // console.log('type: '+s_type);
427 var normalized_name = i.toString().replace('_', ' ');
431 wdgt = $.ajatus.widget('boolean');
434 wdgt = $.ajatus.widget('integer');
438 wdgt = $.ajatus.widget('text');
442 'li', { id: '_setting_'+section+'_'+i, className: 'row' }, [
443 'label', { id: '_setting_'+section+'_'+i+'_label' }, $.ajatus.i10n.get(normalized_name),
444 'div', { id: '_setting_'+section+'_'+i+'_widget', className: wdgt.name }, wdgt.get_edit_tpl(section+';'+i, {val: s}),
445 'br', { className: 'clear_fix' }, ''
448 wdgt.init($('#_setting_'+i+'_widget',rows), true);
451 $('li:first', rows).addClass('first');
452 $('li:last', rows).addClass('last');
457 function gen_localization(data) {
458 var rows = $('<ul/>');
459 var langs = $.ajatus.i10n.get_available_langs();
462 $.each(langs, function(i,l) {
466 wdgt = $.ajatus.widget('selection', {
470 'li', { id: '_setting_localization_language', className: 'row' }, [
471 'label', { id: '_setting_localization_language_label' }, $.ajatus.i10n.get('Language'),
472 'div', { id: '_setting_localization_language_widget', className: wdgt.name }, wdgt.get_edit_tpl('localization;language', {val: data.language}),
473 'br', { className: 'clear_fix' }, ''
476 wdgt.init($('#_setting_localization_language_widget',rows), true);
481 function gen_layout(data) {
482 var rows = $('<ul/>');
483 var themes = $.ajatus.preferences.client.themes.available;
486 $.each(themes, function(i,l) {
487 theme_opts[i] = $.ajatus.i10n.get(l);
490 wdgt = $.ajatus.widget('selection', {
494 'li', { id: '_setting_layout_theme_name', className: 'row' }, [
495 'label', { id: '_setting_layout_theme_name_label' }, $.ajatus.i10n.get('Theme'),
496 'div', { id: '_setting_layout_theme_name_widget', className: wdgt.name }, wdgt.get_edit_tpl('layout;theme_name', {val: data.theme_name}),
497 'br', { className: 'clear_fix' }, ''
500 wdgt.init($('#_setting_layout_theme_name_widget', rows), true);
505 // Example how to handle arrays with checkboxes
506 // function gen_types(data) {
507 // var rows = $('<ul />');
509 // $.each($.ajatus.types.available, function(key,type){
510 // // console.log("key: "+key);
511 // // console.log(type);
513 // var selected = false;
514 // $.each(data.active, function(i,t){
520 // var widget = new $.ajatus.widget('boolean', {
524 // rows.createAppend(
525 // 'li', { id: '_setting_types_active_'+key, className: 'row' }, [
526 // 'label', { id: '_setting_types_active_'+key+'_label' }, $.ajatus.i10n.get(type.title),
527 // 'div', { id: '_setting_types_active_'+key+'_widget', className: widget.name }, widget.get_edit_tpl('types;active', {val: (selected ? key : '')}), //["'+key+'"]
528 // 'br', { className: 'clear_fix' }, ''
531 // widget.init($('#_setting_types_active_'+key+'_widget',rows), true);
539 items = gen_localization(section_data);
542 items = gen_layout(section_data);
549 items = auto_gen(section, section_data);