add some git stuff and put ajax in plain js

This commit is contained in:
Henrique Dias 2016-03-06 12:39:36 +00:00
parent 7c1a1c4f23
commit 8043610590
5 changed files with 146 additions and 46 deletions

File diff suppressed because one or more lines are too long

View File

@ -24,25 +24,29 @@ $(document).on('page:browse', function() {
$('#content').on('submit', remove.selector, function(event) { $('#content').on('submit', remove.selector, function(event) {
event.preventDefault(); event.preventDefault();
$.ajax({ var request = new XMLHttpRequest();
type: 'DELETE', request.open("DELETE", remove.button.data("file"));
url: remove.button.data("file") request.send();
}).done(function(data) { request.onreadystatechange = function() {
$(foreground).fadeOut(200); if (request.readyState == 4) {
remove.form.fadeOut(200); if (request.status == 200) {
remove.row.fadeOut(200); $(foreground).fadeOut(200);
notification({ remove.form.fadeOut(200);
text: remove.button.data("message"), remove.row.fadeOut(200);
type: 'success', notification({
timeout: 5000 text: remove.button.data("message"),
}); type: 'success',
}).fail(function(data) { timeout: 5000
notification({ });
text: 'Something went wrong.', } else {
type: 'error' notification({
}); text: 'Something went wrong.',
console.log(data); type: 'error'
}); });
console.log(request.responseText);
}
}
}
return false; return false;
}); });
@ -152,32 +156,38 @@ $(document).on('page:browse', function() {
return false; return false;
} }
var content = '{"filename": "' + filename + '", "archetype": "' + archetype + '"}'; var content = {
filename: filename,
archetype: archetype
}
$.ajax({ var request = new XMLHttpRequest();
type: 'POST', request.open("POST", window.location.pathname);
url: window.location.pathname, request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
data: content, request.send(JSON.stringify(content));
dataType: 'json', request.onreadystatechange = function() {
encode: true, if (request.readyState == 4) {
}).done(function(data) { if (request.status == 200) {
notification({ var data = JSON.parse(request.responseText);
text: "File created successfully.",
type: 'success',
timeout: 5000
});
$.pjax({ notification({
url: data.Location, text: "File created successfully.",
container: '#content' type: 'success',
}) timeout: 5000
}).fail(function(data) { });
notification({ $.pjax({
text: 'Something went wrong.', url: data.Location,
type: 'error' container: '#content'
}); })
console.log(data); } else {
}); notification({
text: 'Something went wrong.',
type: 'error'
});
console.log(request.responseText);
}
}
}
return false; return false;
}); });
@ -253,6 +263,78 @@ $(document).on('page:browse', function() {
return false; return false;
}); });
/* GIT ACTIONS */
var git = new Object();
git.selector = 'form#git';
git.form = $(git.selector);
git.input = git.selector + ' input[type="text"]';
$('#content').on('click', 'button.git', function(event) {
event.preventDefault();
$(foreground).fadeIn(200);
git.form.fadeIn(200);
return false;
});
$('#content').on('keypress', git.input, function(event) {
if (event.keyCode == 13) {
event.preventDefault();
$(git.form).submit();
return false;
}
});
$('#content').on('submit', git.selector, function(event) {
event.preventDefault();
var value = git.form.find('input[type="text"]').val();
if (value == "") {
notification({
text: "You have to write something. If you want to close the box, click outside of the box.",
type: 'warning',
timeout: 5000
});
return false;
}
var request = new XMLHttpRequest();
request.open("POST", "/admin/git");
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.send(JSON.stringify({
command: value
}));
/*$.ajax({
type: 'POST',
url: window.location.pathname,
data: content,
dataType: 'json',
encode: true,
}).done(function(data) {
notification({
text: "File created successfully.",
type: 'success',
timeout: 5000
});
$.pjax({
url: data.Location,
container: '#content'
})
}).fail(function(data) {
notification({
text: 'Something went wrong.',
type: 'error'
});
console.log(data);
}); */
return false;
});
/* $(foreground) AND STUFF */ /* $(foreground) AND STUFF */
$('#content').on('click', '.close', function(event) { $('#content').on('click', '.close', function(event) {
@ -268,6 +350,7 @@ $(document).on('page:browse', function() {
create.form.fadeOut(200); create.form.fadeOut(200);
rename.form.fadeOut(200); rename.form.fadeOut(200);
remove.form.fadeOut(200); remove.form.fadeOut(200);
git.form.fadeOut(200);
return false; return false;
}); });
}); });

View File

@ -1,6 +1,8 @@
package config package config
import ( import (
"os"
"path/filepath"
"strings" "strings"
"github.com/hacdias/caddy-hugo/hugo" "github.com/hacdias/caddy-hugo/hugo"
@ -14,6 +16,7 @@ type Config struct {
Styles string // Admin styles path Styles string // Admin styles path
Args []string // Hugo arguments Args []string // Hugo arguments
Hugo string // Hugo executable path Hugo string // Hugo executable path
Git bool // Is this site a git repository
} }
// ParseHugo parses the configuration file // ParseHugo parses the configuration file
@ -21,6 +24,7 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
conf := &Config{ conf := &Config{
Public: strings.Replace(c.Root, "./", "", -1), Public: strings.Replace(c.Root, "./", "", -1),
Path: "./", Path: "./",
Git: false,
} }
conf.Hugo = hugo.GetPath() conf.Hugo = hugo.GetPath()
@ -59,5 +63,9 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
} }
} }
if _, err := os.Stat(filepath.Join(conf.Path, ".git")); err == nil {
conf.Git = true
}
return conf, nil return conf, nil
} }

View File

@ -1,6 +1,6 @@
//go:generate go get github.com/jteeuwen/go-bindata //go:generate go get github.com/jteeuwen/go-bindata
//go:generate go install github.com/jteeuwen/go-bindata/go-bindata //go:generate go install github.com/jteeuwen/go-bindata/go-bindata
//go:generate go-bindata -pkg assets -o assets/assets.go templates/ assets/css/ assets/js/ assets/fonts/ //go:generate go-bindata -debug -pkg assets -o assets/assets.go templates/ assets/css/ assets/js/ assets/fonts/
// Package hugo makes the bridge between the static website generator Hugo // Package hugo makes the bridge between the static website generator Hugo
// and the webserver Caddy, also providing an administrative user interface. // and the webserver Caddy, also providing an administrative user interface.

View File

@ -7,9 +7,9 @@
<span id="site-title">Path: {{ $path }}</span> <span id="site-title">Path: {{ $path }}</span>
<div class="go-right"> <div class="go-right">
<input type="file" value="Upload" multiple> <input type="file" value="Upload" multiple>
<!-- <button class="darker"><i class="fa fa-refresh"></i></button> -->
<button id="upload">Upload <i class="fa fa-cloud-upload"></i></button> <button id="upload">Upload <i class="fa fa-cloud-upload"></i></button>
<button class="default new">New <i class="fa fa-plus"></i></button> <button class="default new">New <i class="fa fa-plus"></i></button>
{{ if .User.Git }}<button class="darker git"><i class="fa fa-git"></i></button>{{ end }}
</div> </div>
</div> </div>
</div> </div>
@ -82,4 +82,13 @@
<input type="submit" value="Rename"> <input type="submit" value="Rename">
</p> </p>
</form> </form>
<form class="popup hidden" id="git">
<h3>Git</h3>
<p>Write down the <code>git</code> command you want to execute. You don't need to write <code>git</code>.</p>
<input type="text" placeholder="git push origin master">
<p class="right">
<input type="submit" value="Execute">
</p>
</form>
{{ end }} {{ end }}