From 8dd2beb82ea879b6cf65eeab1dd8806aecde9749 Mon Sep 17 00:00:00 2001 From: Dima Malishev Date: Sat, 6 Aug 2011 16:19:52 +0300 Subject: [PATCH] JS updates. Added list / edit / add / delete functionality. Added Helpers methods. --- web/js/__init__.js | 8 +- web/js/_settings.js | 10 +- web/js/actions.js | 55 +++++++-- web/js/helpers.js | 81 ++++++++++---- web/js/html.js | 266 +++++++++++++++++++++++++++++++------------- web/js/model.js | 34 +++++- web/js/templates.js | 246 ++++++++++++++++++++++++++++++++-------- 7 files changed, 539 insertions(+), 161 deletions(-) diff --git a/web/js/__init__.js b/web/js/__init__.js index e4c133cba..655eee055 100644 --- a/web/js/__init__.js +++ b/web/js/__init__.js @@ -1,17 +1,15 @@ $(document).ready(function(){ try{ App.Utils.detectBrowser(); - - App.Env.world = 'USER'; + App.Env.world = 'CRON'; App.Pages.init(); - App.Ref.init(); - + //App.View.start(); App.Core.listen(); App.Core.initMenu(); App.Helpers.liveValidate(); - + }catch(e){ fb.error(e); } diff --git a/web/js/_settings.js b/web/js/_settings.js index 2f14e0333..7f7b4cac4 100644 --- a/web/js/_settings.js +++ b/web/js/_settings.js @@ -3,16 +3,18 @@ App.i18n.EN = {}; App.i18n.EN.incorrect_ip = 'Incorrect ip'; App.i18n.EN.confirm = 'Are you sure?'; App.i18n.getMessage = function(key) -{ +{ return 'undefined' != typeof App.i18n[App.Env.lang][key] ? App.i18n[App.Env.lang][key] : ''; } // Constants -App.Constants.IP_FORM_ID = 'ip-form'; -App.Constants.DNS_FORM_ID = 'dns-form'; -App.Constants.USER_FORM_ID = 'user-form'; +App.Constants.IP_FORM_ID = 'ip-form'; +App.Constants.DNS_FORM_ID = 'dns-form'; +App.Constants.USER_FORM_ID = 'user-form'; +App.Constants.WEB_DOMAIN_FORM_ID = 'web_domain-form'; +App.Constants.DB_FORM_ID = 'db-form'; App.Settings.ajax_url = 1; App.Settings.uri = location.href.replace('index.html', ''); diff --git a/web/js/actions.js b/web/js/actions.js index 314d8dbc7..1b2ec1156 100644 --- a/web/js/actions.js +++ b/web/js/actions.js @@ -104,14 +104,9 @@ App.Actions.save_form = function(evt) { var source = $(elm).find('.source').val(); var values = App.Helpers.getFormValues(elm); if(App.Validate.form(values, $('#'+elm_id))) { - App.Model.add(values, source); - //var form_id = App.Constants[App.Env.world + '_FORM_ID']; - //$('#'+form_id).remove(); + App.Model.add(values, source); App.Model.update(values, source, elm); - } - /*if(App.Validate.form(values, $('#'+elm_id))) { - App.Model.update(values, source); - }*/ + } } } @@ -148,6 +143,16 @@ App.Actions.cancel_form = function(evt, params) { App.Helpers.updateScreen(); } +App.Actions.suspend = function(evt) +{ + alert('Suspend?'); +} + +App.Actions.unsuspend = function(evt) +{ + alert('Unsuspend?'); +} + // do_action_form_help App.Actions.showFormHelp = function(evt) { var tpl_name = App.Env.world + '_form'; @@ -188,3 +193,39 @@ App.Actions.generate_pass = function() { $('.password').val(App.Helpers.generatePassword()); } + +App.Actions.toggle_section = function(evt) +{ + var elm = $(evt.target); + var ref = $(elm).parents('.form-options-group:first'); + fb.log(ref); + if ($('.sub_section:first', ref).hasClass('hidden')) { + $('.sub_section:first', ref).removeClass('hidden'); + $('.group-header:first', ref).removeClass('collapsed').addClass('expanded'); + } + else { + $('.sub_section:first', ref).addClass('hidden'); + $('.group-header:first', ref).removeClass('expanded').addClass('collapsed'); + } +} + +App.Actions.close_inner_popup = function(evt) +{ + App.Helpers.closeInnerPopup(); +} + +App.Actions.open_inner_popup = function(evt) +{ + var elm = $(evt.target); + App.Helpers.openInnerPopup(elm, $(elm).next('.inner-popup-html').val()); +} + +App.Actions.add_db_user = function(evt) +{ + alert('TODO'); +} + +App.Actions.backup_db = function(evt) +{ + alert('TODO'); +} diff --git a/web/js/helpers.js b/web/js/helpers.js index 529cb27c2..a858fdace 100644 --- a/web/js/helpers.js +++ b/web/js/helpers.js @@ -1,3 +1,44 @@ + + +App.Helpers.getMbHumanMeasure = function(val) +{ + return App.Helpers.getMbHuman(val, true); +} + +/** + * Currently no bytes are used, minimal value is in MB + * uncomment in case we will use bytes instead + */ +App.Helpers.getMbHuman = function(val, only_measure) +{ + var bytes = val * 1024 * 1024; + var kilobyte = 1024; + var megabyte = kilobyte * 1024; + var gigabyte = megabyte * 1024; + var terabyte = gigabyte * 1024; + var precision = 0; + + /*if ((bytes >= 0) && (bytes < kilobyte)) { + return bytes + ' B'; + + } else if ((bytes >= kilobyte) && (bytes < megabyte)) { + return (bytes / kilobyte).toFixed(precision) + ' KB'; + + } else */ + if ((bytes >= megabyte) && (bytes < gigabyte)) { + return only_measure ? 'MB' : (bytes / megabyte).toFixed(precision); + + } else if ((bytes >= gigabyte) && (bytes < terabyte)) { + return only_measure ? 'GB' : (bytes / gigabyte).toFixed(precision); + + } else if (bytes >= terabyte) { + return only_measure ? 'TB' : (bytes / terabyte).toFixed(precision); + + } else { + return only_measure ? 'MB' : bytes; + } +} + App.Helpers.getFirst = function(obj) { try{ // TODO: remove try / catch @@ -101,22 +142,6 @@ App.Helpers.showConsoleHint = function() // TODO: } - -// UTILS -App.Utils.generatePasswordHash = function(length) -{ - var length = length || 11; - var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!~."; - var pass = ""; - for(var x=0;x.')); tpl.set(':CHECKED', ''); @@ -194,35 +162,39 @@ App.HTML.Build.dns_entry = function(o, is_new) var now = new Date(); tpl.set(':DATE', now.format("d.mm.yyyy")); } - + if (App.Constants.SUSPENDED_YES == o.SUSPEND) { - var sub_tpl = App.Templates.get('SUSPENDED_TPL_ENABLED', 'dns'); + var sub_tpl = App.Templates.get('SUSPENDED_TPL_NOT_SUSPENDED', 'general'); } else { - var sub_tpl = App.Templates.get('SUSPENDED_TPL_DISABLED', 'dns'); + var sub_tpl = App.Templates.get('SUSPENDED_TPL_SUSPENDED', 'general'); } - + tpl.set(':SUSPENDED_TPL', sub_tpl.finalize()); - + return tpl.finalize(); } App.HTML.Build.user_entry = function(o, key) { var processed_data = { - 'NICKNAME': key, - 'BANDWIDTH_PERCENTS': 90, - 'U_DISK_PERCENTS': 80 + 'NICKNAME' : key, + 'U_DISK_PERCENTAGE' : o.U_DISK > 0 ? o.U_DISK / o.DISK_QUOTA * 100 : 0.01, + 'U_BANDWIDTH_PERCENTAGE': o.U_BANDWIDTH > 0 ? o.U_BANDWIDTH / o.BANDWIDTH * 100 : 0.01, + 'DISK_QUOTA_MEASURE' : App.Helpers.getMbHumanMeasure(o.DISK_QUOTA), + 'BANDWIDTH_MEASURE' : App.Helpers.getMbHumanMeasure(o.BANDWIDTH), + 'BANDWIDTH' : App.Helpers.getMbHuman(o.BANDWIDTH), + 'DISK_QUOTA' : App.Helpers.getMbHuman(o.DISK_QUOTA) }; var o = $.extend(o, processed_data); var tpl = App.Templates.get('ENTRY', 'user'); tpl = App.HTML.setTplKeys(tpl, o); if (App.Constants.SUSPENDED_YES == o.SUSPENDED) { - var sub_tpl = App.Templates.get('SUSPENDED_TPL_ENABLED', 'ip'); + var sub_tpl = App.Templates.get('SUSPENDED_TPL_SUSPENDED', 'general'); } else { - var sub_tpl = App.Templates.get('SUSPENDED_TPL_DISABLED', 'ip'); + var sub_tpl = App.Templates.get('SUSPENDED_TPL_NOT_SUSPENDED', 'general'); } tpl.set(':SUSPENDED_TPL', sub_tpl.finalize()); @@ -258,27 +230,47 @@ App.HTML.Build.user_form = function(options, id) App.HTML.Build.web_domain_entry = function(o, key) { - // TODO: - /*~!:ALIAS~!,\ - naumov-socolov.org.md,\ - naumov-socolov.to\*/ var processed_data = { DOMAIN: key }; var o = $.extend(o, processed_data); - //fb.info(o); var tpl = App.Templates.get('ENTRY', 'web_domain'); tpl = App.HTML.setTplKeys(tpl, o); - /*if (App.Constants.SUSPENDED_YES == o.SUSPENDED) { - var sub_tpl = App.Templates.get('SUSPENDED_TPL_ENABLED', 'ip'); + if (App.Constants.SUSPENDED_YES == o.SUSPENDED) { + var sub_tpl = App.Templates.get('SUSPENDED_TPL_SUSPENDED', 'general'); } else { - var sub_tpl = App.Templates.get('SUSPENDED_TPL_DISABLED', 'ip'); + var sub_tpl = App.Templates.get('SUSPENDED_TPL_NOT_SUSPENDED', 'general'); + } + tpl.set(':SUSPENDED_TPL', sub_tpl.finalize()); + + return tpl.finalize(); +} + +App.HTML.Build.web_domain_form = function(options, id) +{ + if('undefined' == typeof App.Env.initialParams) { + return alert('PLease wait a bit. Some background processes are not yet executed. Thank you for patience.'); + } + var tpl = App.Templates.get('FORM', 'web_domain'); + tpl.set(':source', options); + tpl.set(':id', id || ''); + options = App.Helpers.evalJSON(options) || {}; + if (App.Helpers.isEmpty(options)) { + tpl.set(':title', 'New user'); + tpl.set(':save_button', 'ADD'); + } + else { + tpl.set(':title', 'Edit user'); + tpl.set(':save_button', 'SAVE'); } - tpl.set(':SUSPENDED_TPL', sub_tpl.finalize()); - */ + options = !App.Helpers.isEmpty(options) ? options : {'CONTACT':'', 'PASSWORD':'','LOGIN_NAME':'','NS':''}; + + tpl = App.HTML.setTplKeys(tpl, options, true); + tpl = App.HTML.Build.user_selects(tpl, options); + return tpl.finalize(); } @@ -295,17 +287,57 @@ App.HTML.Build.mail_entry = function(o, key) } App.HTML.Build.db_entry = function(o, key) -{ +{ + var user_list_html = []; + $(o['USERS']).each(function(i, o) + { + var tpl = App.Templates.get('USER_ITEM', 'db'); + tpl.set(':NAME', o); + user_list_html.push(tpl.finalize()); + }); + var wrapper = App.Templates.get('USER_ITEMS_WRAPPER', 'db'); + wrapper.set(':CONTENT', user_list_html.done()); var processed_data = { - DOMAIN: key + 'USER_LIST': wrapper.finalize(), + 'USERS': o['USERS'].length || 0, + 'U_DISK_PERCENTAGE' : o.U_DISK > 0 ? o.U_DISK / o.DISK * 100 : 0.01, + 'DISK_MEASURE': App.Helpers.getMbHumanMeasure(o.DISK), + 'DISK': App.Helpers.getMbHuman(o.DISK) }; var o = $.extend(o, processed_data); + var tpl = App.Templates.get('ENTRY', 'db'); tpl = App.HTML.setTplKeys(tpl, o); return tpl.finalize(); } +App.HTML.Build.db_form = function(options, id) +{ + if('undefined' == typeof App.Env.initialParams) { + return alert('PLease wait a bit. Some background processes are not yet executed. Thank you for patience.'); + } + var tpl = App.Templates.get('FORM', 'db'); + tpl.set(':source', options); + tpl.set(':id', id || ''); + options = App.Helpers.evalJSON(options) || {}; + if (App.Helpers.isEmpty(options)) { + tpl.set(':title', 'New database'); + tpl.set(':save_button', 'ADD'); + } + else { + tpl.set(':title', 'Edit database "'+options.DB+'"'); + tpl.set(':save_button', 'SAVE'); + } + + options = !App.Helpers.isEmpty(options) ? options : {'DB':'', 'USER':'','FORM':'', 'PASSWORD': ''}; + + tpl = App.HTML.setTplKeys(tpl, options, true); + tpl = App.HTML.Build.db_selects(tpl, options); + + return tpl.finalize(); +} + App.HTML.Build.cron_entry = function(o, key) { var processed_data = { @@ -313,7 +345,15 @@ App.HTML.Build.cron_entry = function(o, key) }; var o = $.extend(o, processed_data); var tpl = App.Templates.get('ENTRY', 'cron'); - tpl = App.HTML.setTplKeys(tpl, o); + tpl = App.HTML.setTplKeys(tpl, o); + + if (App.Constants.SUSPENDED_YES == o.SUSPENDED) { + var sub_tpl = App.Templates.get('SUSPENDED_TPL_SUSPENDED', 'general'); + } + else { + var sub_tpl = App.Templates.get('SUSPENDED_TPL_NOT_SUSPENDED', 'general'); + } + tpl.set(':SUSPENDED_TPL', sub_tpl.finalize()); return tpl.finalize(); } @@ -354,6 +394,7 @@ App.HTML.Build.user_selects = function(tpl, options) var tpl = App.Templates.get('select_option', 'general'); tpl.set(':VALUE', val); tpl.set(':TEXT', pkg[val]); + tpl.set(':SELECTED', val == options.PACKAGE ? 'selected="selected"' : ''); acc[acc.length++] = tpl.finalize(); }); tpl.set(':PACKAGE_OPTIONS', acc.done()); @@ -365,6 +406,7 @@ App.HTML.Build.user_selects = function(tpl, options) var tpl = App.Templates.get('select_option', 'general'); tpl.set(':VALUE', val); tpl.set(':TEXT', roles[val]); + tpl.set(':SELECTED', val == options.ROLE ? 'selected="selected"' : ''); acc[acc.length++] = tpl.finalize(); }); tpl.set(':ROLE_OPTIONS', acc.done()); @@ -376,6 +418,7 @@ App.HTML.Build.user_selects = function(tpl, options) var tpl = App.Templates.get('select_option', 'general'); tpl.set(':VALUE', val); tpl.set(':TEXT', shell[val]); + tpl.set(':SELECTED', val == options.SHELL ? 'selected="selected"' : ''); acc[acc.length++] = tpl.finalize(); }); tpl.set(':SHELL_OPTIONS', acc.done()); @@ -383,3 +426,70 @@ App.HTML.Build.user_selects = function(tpl, options) return tpl; } +App.HTML.Build.db_selects = function(tpl, options) +{ + var acc = []; + // PACKAGE + var items = App.Env.initialParams.DB.TYPE; + $.each(items, function(val) + { + var tpl = App.Templates.get('select_option', 'general'); + tpl.set(':VALUE', val); + tpl.set(':TEXT', items[val]); + tpl.set(':SELECTED', val == options.TYPE ? 'selected="selected"' : ''); + acc[acc.length++] = tpl.finalize(); + }); + tpl.set(':TYPE_OPTIONS', acc.done()); + // ROLE + acc = []; + var items = App.Env.initialParams.DB.HOST; + $.each(items, function(val) + { + var tpl = App.Templates.get('select_option', 'general'); + tpl.set(':VALUE', val); + tpl.set(':TEXT', items[val]); + tpl.set(':SELECTED', val == options.HOST ? 'selected="selected"' : ''); + acc[acc.length++] = tpl.finalize(); + }); + tpl.set(':HOST_OPTIONS', acc.done()); + + return tpl; +} + +App.HTML.Build.ip_selects = function(tpl, options) +{ + // OWNER + var users = App.Env.initialParams.IP.SYS_USERS || ['Skid']; + var opts = App.HTML.Build.options(users, options.OWNER); + tpl.set(':owner_options', opts); + + // STATUS + var opts = App.HTML.Build.options(App.Env.initialParams.IP.STATUSES, options.STATUS); + tpl.set(':status_options', opts); + + // INTERFACE + var opts = App.HTML.Build.options(App.Env.initialParams.IP.INTERFACES, options.INTERFACE); + tpl.set(':interface_options', opts); + + return tpl; +} + +App.HTML.Build.dns_selects = function(tpl, options) +{ + try { + // TPL + var obj = App.Env.initialParams.DNS.TPL; + var opts = App.HTML.Build.options(obj, options.TPL); + tpl.set(':TPL', opts); + tpl.set(':TPL_DEFAULT_VALUE', options.TPL || App.Helpers.getFirstKey(obj)); + } + catch (e) { + return ''; + } + + return tpl; +} + + + + diff --git a/web/js/model.js b/web/js/model.js index b68caed49..cc1cf46c3 100644 --- a/web/js/model.js +++ b/web/js/model.js @@ -20,12 +20,40 @@ App.Model.WEB_DOMAIN.loadList = function() App.Model.MAIL.loadList = function() { - App.Ajax.request('MAIL.getList', {}, App.View.listItems); + //App.Ajax.request('MAIL.getList', {}, App.View.listItems); + App.Ref.CONTENT.html('

Under maintanance

'); } App.Model.DB.loadList = function() { - App.Ajax.request('DB.getList', {}, App.View.listItems); + App.Ajax.request('DB.getList', {}, function(reply) + { + var acc = []; + var build_method = App.Env.getWorldName() + '_entry'; + var data = reply.data; + // TODO: fix it data.data + $.each(data, function(key) + { + var db_list = data[key]; + fb.warn('KEY: %o', key); + fb.warn('DATA: %o', data[key]); + var tpl_divider = App.Templates.get('DIVIDER', 'db'); + tpl_divider.set(':TYPE', key); + acc[acc.length++] = tpl_divider.finalize(); + $(db_list).each(function(i, o) + { + acc[acc.length++] = App.HTML.Build[build_method](o, key); + }); + + /*var o = data[key]; + fb.warn(key); + acc[acc.length++] = App.HTML.Build[build_method](o, key);*/ + }); + + var html = acc.done().wrapperize('ENTRIES_WRAPPER', App.Env.getWorldName()); + App.Ref.CONTENT.html(html); + App.Helpers.updateScreen(); + }); } App.Model.CRON.loadList = function() @@ -71,7 +99,7 @@ App.Model.remove = function(world, elm) } App.Model.update = function(values, source_json, elm) -{ alert(source_json); +{ var method = App.Settings.getMethodName('update'); var build_method = App.Env.getWorldName() + '_entry'; App.Ajax.request(method, { diff --git a/web/js/templates.js b/web/js/templates.js index 4f36cf634..3e1f322db 100644 --- a/web/js/templates.js +++ b/web/js/templates.js @@ -16,13 +16,23 @@ App.Templates.html = { "Well," the farmer said, "I didn\'t have anymore rope, so I took off my belt and tied her tail to the rafter. In that moment, my pants fell down and my wife walked in ... Some things you just can\'t explain."'] }, general: { - loading: ['
\ + loading: ['
\
Loading...
\
'], popup: ['\ '], + inner_popup: ['
\ +
\ + ×\ +
\ + ~!:CONTENT~!\ +
\ +
\ +
'], select_option: [''], - error_elm: ['
~!:ERROR~!
'] + error_elm: ['
~!:ERROR~!
'], + SUSPENDED_TPL_NOT_SUSPENDED : ['enabled'], + SUSPENDED_TPL_SUSPENDED : ['disabled'] }, popup: { error: ['

Important: An Error Has Occured.


    Something went wrong and some of your actions can be not saved in system. Mostly, it happens when you have network connection errors.
,    However, please notify us about the situation. It would be helpfull if you will write us approximate time the error occured and last actions you were performing. You send your petition on this email: BLABLA,

Sorry for inconvinience. (We recommend you to reload the page)
'] @@ -70,13 +80,10 @@ App.Templates.html = {
\ \ Cancel\ - Help\ + Help\
\
' - ], - SUSPENDED_TPL_ENABLED : ['enabled\ - delete'], - SUSPENDED_TPL_DISABLED : ['suspended'], + ], ENTRIES_WRAPPER: ['
~!:content~!
'], ENTRY: ['
\ \ @@ -162,35 +169,35 @@ App.Templates.html = { FORM: ['\
\ \ + \
~!:title~!
\ -
\ -
\ +
\ +
\
\ \ \
\
\ \ - vesta\ - \ ~!:owner_options~!\ \
\
\ \ - shared\ - \ ~!:status_options~!\ \
\
\ \ - \ + \
\
\ \ - eth1\ - \ ~!:interface_options~!\ \
\ @@ -203,7 +210,7 @@ App.Templates.html = {
\ \ Cancel\ - Help\ + Help\
\
\ '], @@ -216,6 +223,7 @@ App.Templates.html = {
\
\ ~!:SUSPENDED_TPL~!\ + delete\
\
\
\ @@ -224,7 +232,7 @@ App.Templates.html = {
\
\
\ - ~!:IP_ADDRESS~!\ + ~!:IP_ADDRESS~!\
\ \ netmask:\ @@ -273,7 +281,7 @@ App.Templates.html = { \ \
~!:title~!
\ -