forked from clarklab/chowdown
-
Notifications
You must be signed in to change notification settings - Fork 3
/
new.html
212 lines (181 loc) · 8.68 KB
/
new.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
---
layout: default
---
<div class="container">
<h2 class="center mb3 blue" style="margin-bottom: 15px; margin-top: 30px;">New Recipe</h2>
<div class="col-12">
<div class="row justify-content-md-center">
<input type="text" class="form-control center col-12 col-md-10 col-lg-8" id="title" placeholder="Title"
aria-label="Title" style="margin-bottom:5px;">
<textarea class="form-control center col-12 col-md-10 col-lg-8" id="description" rows="3" placeholder="Description" style="margin-bottom:5px;"></textarea>
<input type="text" class="form-control center col-12 col-md-10 col-lg-8" id="tags" placeholder="Tags (space separated)"
aria-label="Tags" style="margin-bottom:5px;">
<input type="text" class="form-control center col-12 col-md-10 col-lg-8" id="servings" placeholder="Serving Size"
aria-label="Tags" style="margin-bottom:5px;">
</div>
</div>
<h4 class="mb3 blue" style="margin-bottom: 15px; margin-top: 15px;">Ingredients
<button type="button" id="new-ingredient" class="btn" style="margin-bottom:6px; padding-left: 6px;">
{% include plus.svg %}
</button>
</h4>
<div id="ingredients" class="col-12" style="margin-bottom: 20px;"> </div>
<h4 class="mb3 blue" style="margin-bottom: 15px;">Directions
<button type="button" id="new-direction" class="btn" style="margin-bottom:6px; padding-left: 6px;">
{% include plus.svg %}
</button>
</h4>
<div id="directions" class="col-12" style="margin-bottom: 20px;"> </div>
<h4 class="mb3 blue" style="margin-bottom: 15px;">Components
<button type="button" id="new-component" class="btn" style="margin-bottom:6px; padding-left: 6px;">
{% include plus.svg %}
</button>
</h4>
<div id="components" class="col-12" style="margin-bottom: 20px;"> </div>
<div class="col-12">
<div class="row justify-content-md-center">
<button type="button" class="btn btn-primary col-12 col-md-8 col-lg-6" id="save">Save Recipe</button>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="loginModal" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<form>
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Provide Github Credentials</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" aria-describedby="usernameHelp"
placeholder="Enter username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
<div class="modal fade" id="loadingModal" tabindex="-1" role="dialog" aria-labelledby="loginModal" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="d-flex justify-content-center" style="margin-top: 25px; margin-bottom: 15px;">
<div class="spinner-border" role="status" style="width: 4rem; height: 4rem;">
<span class="sr-only">Loading...</span>
</div>
</div>
<p>Creating commit and pushing to Github...<br><br><small>It may take a few minutes for the backend to update and show the change file.</small></p>
</div>
</div>
</div>
</div>
<script src="https://unpkg.com/github-api/dist/GitHub.bundle.min.js" type="text/javascript"></script>
<!-- Configuration -->
<script>
$('#save').on('click', function () {
$('#loginModal form').trigger("reset");
$('#loginModal').modal('show');
});
$('#loginModal form').submit(e => {
$('#loginModal').modal('hide');
$('#loadingModal').modal('show');
e.preventDefault();
var github = new GitHub({
username: $('#username').val(),
password: $('#password').val(),
auth: 'basic'
});
var repository = github.getRepo($('#username').val(), 'chowdown');
var file = constructFile();
// Creates a new file (or updates it if the file already exists)
// with the content provided
repository.writeFile(
'gh-pages', // e.g. 'master'
'_recipes/' + file.name, // e.g. 'blog/index.md'
file.content, // e.g. 'Hello world, this is my new content'
'Added new recipe file: ' + file.name, // e.g. 'Created new index'
function (err) {
if (err) {
console.error('An error occurred when submitting the file');
alert('An error occurred when submitting the file');
} else {
window.location.href = '/';
}
$('#loadingModal').modal('hide');
}
);
});
$('#new-ingredient').on('click', function () {
$('#ingredients').append('<div style="display: flex; margin-bottom: 5px;">' +
'<input type="text" class="form-control ingredient" placeholder="Ingredient" aria-label="Ingredient">' +
'<button type="button" class="btn btn-warning remove-item" style="margin-left: 10px;">Remove</button>' +
'</div>');
});
$('#new-direction').on('click', function () {
$('#directions').append('<div style="display: flex; margin-bottom: 5px;">' +
'<input type="text" class="form-control direction" placeholder="Direction" aria-label="Direction">' +
'<button type="button" class="btn btn-warning remove-item" style="margin-left: 10px;">Remove</button>' +
'</div>');
});
$('#new-component').on('click', function () {
$('#components').append('<div style="display: flex; margin-bottom: 5px;">' +
'<input type="text" class="form-control component" placeholder="Component" aria-label="Component">' +
'<button type="button" class="btn btn-warning remove-item" style="margin-left: 10px;">Remove</button>' +
'</div>');
});
$(document).on('click', '.remove-item', function () {
$(this).parent().remove();
});
function constructFile() {
const name = $('#title').val().trim().toLowerCase().replace(/\s+/g, '-').replace(/,/g,'').replace(/\'/, '').replace(/:/g, '').replace(/ä/g, 'ae').replace(/ö/g, 'oe').replace(/ü/g, 'ue').replace(/ß/g, 'ss');
let result = '---\n';
result += '\n';
// Basics
result += 'layout: recipe\n';
result += 'title: ' + $('#title').val().replace(/:/g, '') + '\n';
result += 'image: ' + name + '.jpg\n';
result += 'tags: ' + $('#tags').val().toLowerCase().replace(/:/g, '') + '\n';
result += 'recipeYield: ' + $('#servings').val().replace(/:/g, '') + '\n';
result += '\n';
// ingredients
result += 'ingredients:\n';
$('.ingredient').each(function (index) {
if($(this).val() !== '')
result += '- ' + $(this).val().replace(/:/g, '') + '\n';
});
result += '\n';
// directions
result += 'directions:\n';
$('.direction').each(function (index) {
if($(this).val() !== '')
result += '- ' + $(this).val().replace(/:/g, '') + '\n';
});
result += '\n';
// components
result += 'components:\n'
$('.component').each(function (index) {
if($(this).val() !== '')
result += '- ' + $(this).val().replace(/:/g, '') + '\n';
});
result += '\n';
result += '---\n';
result += $('#description').val().replace(/:/g, '');
return {
name: name + '.md',
content: result
};
}
</script>