From 08153a30712a1589cd665f30d2d780c49e97db8d Mon Sep 17 00:00:00 2001 From: Ricard Clau Date: Sat, 23 Aug 2014 12:24:50 +0100 Subject: [PATCH] support for localstorage --- Resources/views/layout.html.twig | 110 +++++++++++++++++++++++-------- 1 file changed, 83 insertions(+), 27 deletions(-) diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index 4eb48a7..9765b09 100755 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -43,6 +43,8 @@ {% if authentication.custom_endpoint %} api endpoint: {% endif %} + + {% endif %} {% endif %} @@ -92,6 +94,9 @@ }); elem.find('.toggler').click(); } + {% if enableSandbox %} + loadStoredAuthParams(); + {% endif %} }); $('.toggler').click(function(event) { @@ -108,38 +113,70 @@ }); {% if enableSandbox %} - var setParameterType = function ($context,setType) { - // no 2nd argument, use default from parameters - if (typeof setType == "undefined") { - setType = $context.parent().attr("data-dataType"); - $context.val(setType); - } + var getStoredValue, storeValue, deleteStoredValue; + var apiAuthKeys = ['api_key', 'api_login', 'api_pass', 'api_endpoint']; - $context.parent().find('.value').remove(); - var placeholder = ""; - if ($context.parent().attr("data-dataType") != "" && typeof $context.parent().attr("data-dataType") != "undefined") { - placeholder += "[" + $context.parent().attr("data-dataType") + "] "; - } - if ($context.parent().attr("data-format") != "" && typeof $context.parent().attr("data-format") != "undefined") { - placeholder += $context.parent().attr("data-dataType"); - } - if ($context.parent().attr("data-description") != "" && typeof $context.parent().attr("data-description") != "undefined") { - placeholder += $context.parent().attr("data-description"); + if ('localStorage' in window) { + var buildKey = function (key) { + return 'nelmio_' + key; + } + + getStoredValue = function (key) { + return localStorage.getItem(buildKey(key)); + } + + storeValue = function (key, value) { + localStorage.setItem(buildKey(key), value); + } + + deleteStoredValue = function (key) { + localStorage.removeItem(buildKey(key)); + } } else { - placeholder += "Value"; + getStoredValue = storeValue = deleteStoredValue = function (){}; } - switch(setType) { - case "boolean": - $('').insertAfter($context); - break; - case "file": - $('').insertAfter($context); - break; - default: - $('').insertAfter($context); + var loadStoredAuthParams = function() { + $.each(apiAuthKeys, function(_, value) { + var elm = $('#' + value); + if (elm.length) { + elm.val(getStoredValue(value)); + } + }); } - }; + + var setParameterType = function ($context,setType) { + // no 2nd argument, use default from parameters + if (typeof setType == "undefined") { + setType = $context.parent().attr("data-dataType"); + $context.val(setType); + } + + $context.parent().find('.value').remove(); + var placeholder = ""; + if ($context.parent().attr("data-dataType") != "" && typeof $context.parent().attr("data-dataType") != "undefined") { + placeholder += "[" + $context.parent().attr("data-dataType") + "] "; + } + if ($context.parent().attr("data-format") != "" && typeof $context.parent().attr("data-format") != "undefined") { + placeholder += $context.parent().attr("data-dataType"); + } + if ($context.parent().attr("data-description") != "" && typeof $context.parent().attr("data-description") != "undefined") { + placeholder += $context.parent().attr("data-description"); + } else { + placeholder += "Value"; + } + + switch(setType) { + case "boolean": + $('').insertAfter($context); + break; + case "file": + $('').insertAfter($context); + break; + default: + $('').insertAfter($context); + } + }; var toggleButtonText = function ($btn) { if ($btn.text() === 'Default') { @@ -205,6 +242,25 @@ return body; } + $('#save_api_auth').click(function(event) { + $.each(apiAuthKeys, function(_, value) { + var elm = $('#' + value); + if (elm.length) { + storeValue(value, elm.val()); + } + }); + }); + + $('#clear_api_auth').click(function(event) { + $.each(apiAuthKeys, function(_, value) { + deleteStoredValue(value); + var elm = $('#' + value); + if (elm.length) { + elm.val(''); + } + }); + }); + $('.tabs li').click(function() { var contentGroup = $(this).parents('.content');