From ece4fb49de400f970dc6ec02bc109196c16d1644 Mon Sep 17 00:00:00 2001 From: alvaro <alvaro@alia.(none)> Date: Tue, 9 Oct 2012 15:23:20 -0700 Subject: [PATCH] Create/delete components via GUI --- classes/modules/adminModule.php | 72 +++++++- .../originalComponents/static/js/editor.js | 158 ++++++++++++++++++ 2 files changed, 223 insertions(+), 7 deletions(-) create mode 100644 doc/examples/originalComponents/static/js/editor.js diff --git a/classes/modules/adminModule.php b/classes/modules/adminModule.php index 6a4693af..23887ff2 100644 --- a/classes/modules/adminModule.php +++ b/classes/modules/adminModule.php @@ -332,7 +332,7 @@ textarea{ font-family: Monaco,'Droid Sans Mono'; font-size: 80%} if($_SERVER['REQUEST_METHOD'] == 'GET'){ $nstable = ""; foreach($conf['ns'] as $k=>$v){ - $nstable .= "<tr><td>".$k."</td><td id='$k'>".$v."</td><td><button class='button edit-button' data-prefix='$k' data-ns='$v'>Edit</button></tr>"; + $nstable .= "<tr><td>".$k."</td><td id='$k'>".$v."</td><td><button class='button btn edit-button' data-prefix='$k' data-ns='$v'>Edit</button></tr>"; } echo $this->head." <div class='fluid-row'> @@ -399,7 +399,7 @@ textarea{ font-family: Monaco,'Droid Sans Mono'; font-size: 80%} if($_SERVER['REQUEST_METHOD'] == 'GET'){ $nstable = ""; foreach($conf['endpoint'] as $k=>$v){ - $nstable .= "<tr><td>".$k."</td><td id='$k'>".$v."</td><td><button class='button edit-button' data-prefix='$k' data-ns='$v'>Edit</button></tr>"; + $nstable .= "<tr><td>".$k."</td><td id='$k'>".$v."</td><td><button class='button btn edit-button' data-prefix='$k' data-ns='$v'>Edit</button></tr>"; } echo $this->head." <div class='fluid-row'> @@ -478,18 +478,20 @@ textarea{ font-family: Monaco,'Droid Sans Mono'; font-size: 80%} }else{ if(preg_match("/^\w/", $line) ){ $lastComponentType = trim($line); + $singleLastComponentType = preg_replace('/(.*)s$/', '\1', $lastComponentType); $menu .= "<ul class='nav nav-list'> - <li class='nav-header'>".$lastComponentType." <button class='btn btn-mini btn-info'>new</button></li>\n"; + <li class='nav-header'>".$lastComponentType." <button class='btn btn-mini btn-info new-button' style='float:right' data-type='$singleLastComponentType'>new</button></li>\n"; }else{ $componentName = trim($line); - $menu .= "<li><a href='#' class='lodspk-component' data-component-type='$lastComponentType' data-component-name='$componentName'>".$componentName."</a></li>\n"; + $menu .= "<li class='component-li'> <button type='button' class='close hide lodspk-delete-component' data-component-type='$singleLastComponentType' data-component-name='$componentName' style='align:left'>x</button> + <a href='#' class='lodspk-component' data-component-type='$lastComponentType' data-component-name='$componentName'>".$componentName."</a></li>\n"; } } } echo $this->head ." <script src='".$conf['basedir'] ."js/editor.js'></script> <div class='row-fluid'> - <div class='span3 well'>$menu</div> + <div class='span3 well'>$menu<div id='component-msg' class='alert hide'></div></div> <div class='bs-docs-template span9'> <textarea class='field span12' rows='8' cols='25' id='template-editor'></textarea> <button class='btn btn-info disabled' id='template-save-button' data-url=''>Save</button> @@ -501,14 +503,14 @@ textarea{ font-family: Monaco,'Droid Sans Mono'; font-size: 80%} <div class='container'> <div class='row-fluid'> <div class='span3 well'> - <legend>Templates <button class='btn btn-mini btn-info'>new</button></legend> + <legend>Templates <!-- button class='btn btn-mini btn-info'>new</button --></legend> <ul class='nav nav-list' id='template-list'> </ul> </div> </div> <div class='row-fluid'> <div class='span3 well'> - <legend>Queries <button class='btn btn-mini btn-info'>new</button></legend> + <legend>Queries <!-- button class='btn btn-mini btn-info'>new</button --></legend> <ul class='nav nav-list' id='query-list'> </ul> </div> @@ -541,6 +543,20 @@ textarea{ font-family: Monaco,'Droid Sans Mono'; font-size: 80%} HTTPStatus::send404($params[1]); } break; + case "create": + if(sizeof($params) > 2){ + $this->createComponent($params); + }else{ + HTTPStatus::send404($params[1]); + } + break; + case "delete": + if(sizeof($params) > 2){ + $this->deleteComponent($params); + }else{ + HTTPStatus::send404($params[1]); + } + break; default: HTTPStatus::send404($params[1]); } @@ -594,6 +610,48 @@ textarea{ font-family: Monaco,'Droid Sans Mono'; font-size: 80%} } } } + + protected function createComponent($params){ + $path = implode("/", array_slice($params, 1)); + if($_SERVER['REQUEST_METHOD'] == 'POST'){ + if(sizeof($params) != 3){ + HTTPStatus::send404(); + exit(0); + } + $return_var = 0; + exec ("utils/lodspk.sh create ".$params[1]." ".$params[2], &$output, $return_var); + //echo $return_var;exit(0); + if($return_var !== 0){ + HTTPStatus::send500($params[0]." ".$params[1]); + }else{ + echo json_encode(array('success' => true, 'size' => $result)); + } + }else{ + HTTPStatus::send406(); + exit(0); + } + } + + protected function deleteComponent($params){ + $path = implode("/", array_slice($params, 1)); + if($_SERVER['REQUEST_METHOD'] == 'POST'){ + if(sizeof($params) != 3){ + HTTPStatus::send404(); + exit(0); + } + $return_var = 0; + exec ("utils/lodspk.sh delete ".$params[1]." ".$params[2], &$output, $return_var); + if($return_var !== 0){ + HTTPStatus::send500($params[0]." ".$params[1]); + }else{ + echo json_encode(array('success' => true, 'size' => $result)); + } + }else{ + HTTPStatus::send406(); + exit(0); + } + } + protected function stopEndpoint(){ $return_var = 0; exec ("utils/modules/stop-endpoint.sh", &$output, $return_var); diff --git a/doc/examples/originalComponents/static/js/editor.js b/doc/examples/originalComponents/static/js/editor.js new file mode 100644 index 00000000..fc8b8259 --- /dev/null +++ b/doc/examples/originalComponents/static/js/editor.js @@ -0,0 +1,158 @@ +$(document).ready(function(){ + var relPos = "../lodspeakr/components/"; + var templateBuffer = ""; + var queryBuffer = ""; + $(".component-li").on({ + mouseenter: function(){ + $(this).children(".lodspk-delete-component").removeClass("hide"); + }, + mouseleave: function(){ + $(".lodspk-delete-component").addClass("hide"); + } + }); + $(".lodspk-delete-component").on({ + click: function(){ + var componentName = $(this).attr("data-component-name"); + var componentType = $(this).attr("data-component-type"); + var url = "components/delete/"+componentType+"/"+componentName; + if (confirm("Are you sure you want to delete this component?")) { + $.ajax({ + type: 'POST', + url: url, + data: {content: $("#template-editor").val()}, + success: function(data){if(data.success == true){ + $("#component-msg").removeClass('hide').addClass('alert-success').html("Component removed!").show().delay(2000).fadeOut("slow").removeClass('alert-success'); + setTimeout(window.location.reload(), 2000); + }}, + error: function(data){ + $("#component-msg").removeClass('hide').addClass('alert-error').html("Error removing component!").show().delay(2000).fadeOut("slow").removeClass('alert-error'); + }, + dataType: 'json' + }); + } + } + }); + + $(".new-button").on("click", function(e){ + var componentName = prompt("Please enter the name of the new component","newComponent"); + var url = "components/create/"+$(this).attr("data-type")+"/"+componentName; + $.ajax({ + type: 'POST', + url: url, + data: {content: $("#template-editor").val()}, + success: function(data){if(data.success == true){ + $("#component-msg").removeClass('hide').addClass('alert-success').html("Saved!").show().delay(2000).fadeOut("slow").removeClass('alert-success'); + setTimeout(window.location.reload(), 2000); + }}, + error: function(data){ + $("#component-msg").removeClass('hide').addClass('alert-error').html("Error creating a new service!").show().delay(2000).fadeOut("slow").removeClass('alert-error'); + }, + dataType: 'json' + }); + + }); + $(".lodspk-component").on("click", function(e){ + var componentType = $(this).attr("data-component-type"); + var componentName = $(this).attr("data-component-name"); + var url="components/details/"+componentType+"/"+componentName; + templateBuffer = ""; + queryBuffer = ""; + $("#template-editor").val(""); + $("#query-editor").val(""); + $.get(url, function(data){ + $("#template-list").empty() + $("#query-list").empty() + $.each(data.views, function(i, item){ + var viewUrl = relPos+componentType+"/"+componentName+"/"+item; + $("#template-list").append("<li><a class='lodspk-template' href='#template-save-button' data-url='"+viewUrl+"'>"+item+"</a></li>") ; + }); + $.each(data.models, function(i, item){ + var modelUrl = relPos+componentType+"/"+componentName+"/queries/"+item; + $("#query-list").append("<li><a href='#query-save-button' class='lodspk-query' data-url='"+modelUrl+"'>"+item+"</a></li>") + }); + updateEvents(); + }); + }); + function updateEvents(){ + $(".lodspk-template").on("click", function(e){ + var fileUrl = $(this).attr("data-url"); + $.ajax({ + cache: false, + url: fileUrl, + success: function(data){ + $("#template-editor").val(data); + templateBuffer = data; + $("#template-save-button").attr("data-url", fileUrl).addClass("disabled"); + } + }); + }); + $(".lodspk-query").on("click", function(e){ + var fileUrl = $(this).attr("data-url"); + $.ajax({ + cache: false, + url: fileUrl, + success: function(data){ + $("#query-editor").val(data); + queryBuffer = data; + $("#query-save-button").attr("data-url", fileUrl).addClass("disabled"); + } + }); + }); + //Turn 'save' buttons disable when no change has been made + $("#query-editor").on("keyup", function(e){ + if($("#query-editor").val() == queryBuffer){ + $("#query-save-button").addClass("disabled"); + }else{ + $("#query-save-button").removeClass("disabled"); + } + }); + $("#template-editor").on("keyup", function(e){ + if($("#template-editor").val() == templateBuffer){ + $("#template-save-button").addClass("disabled"); + }else{ + $("#template-save-button").removeClass("disabled"); + } + }); + //Save action + $("#template-save-button").on("click", function(e){ + if(!$("#template-save-button").hasClass("disabled")){ + var url = "components/save/"+$("#template-save-button").attr("data-url").replace(relPos, ""); + $.ajax({ + type: 'POST', + url: url, + data: {content: $("#template-editor").val()}, + success: function(data){if(data.success == true){ + $("#template-msg").removeClass('hide').html("Saved!").show().delay(2000).fadeOut("slow"); + }}, + dataType: 'json' + }); + + templateBuffer=$("#template-save-button").val(); + $("#template-save-button").addClass('disabled'); + } + }); + $("#query-save-button").on("click", function(e){ + if(!$("#query-save-button").hasClass("disabled")){ + var url = "components/save/"+$("#query-save-button").attr("data-url").replace(relPos, ""); + $.ajax({ + type: 'POST', + url: url, + data: {content: $("#query-editor").val()}, + success: function(data){if(data.success == true){ + $("#query-msg").removeClass('hide').html("Saved!").show().delay(2000).fadeOut("slow"); + }else{ + $("#query-msg").removeClass('hide').addClass('alert-fail').html("Error saving content").show().delay(2000).fadeOut("slow").removeClass('alert-fail'); + } + }, + error: function(data){if(data.success == true){ + $("#query-msg").removeClass('hide').addClass('alert-fail').html("An error ocurred in the server!").show().delay(2000).fadeOut("slow").removeClass('alert-fail'); + }}, + dataType: 'json' + }); + + queryBuffer=$("#query-save-button").val(); + $("#query-save-button").addClass('disabled'); + } + }); + } +}); -- GitLab