/
url.html
177 lines (153 loc) · 3.74 KB
/
url.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>another url redirecto</title>
<style>
body {
font-family: verdana;
font-size: large;
}
div#info,
label.title {
font-weight: bolder;
}
form > p > label,
p > label {
width: 150px;
}
form > p > label,
form > input,
p > input {
float: left;
}
</style>
<script
type="text/javascript"
src="../lib/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="../lib/underscore-min.js"></script>
<script type="text/javascript" src="../lib/backbone-min.js"></script>
<script
type="text/javascript"
src="../lib/backbone.marionette.min.js"></script>
<script type="text/javascript">
var app = app || {};
app.url = Backbone.Model.extend({
defaults: {
key: "",
url: ""
}
});
app.urls = Backbone.Collection.extend({
model: app.url
});
app.urlsView = Backbone.View.extend({
el: "#urls",
events: {
"click #add": "addUrl",
"click #get": "getUrl"
},
addUrl: function (e) {
e.preventDefault();
var data = {};
if ($("#newMnemonic").val() != "")
data["key"] = $("#newMnemonic").val();
else data["key"] = this.getId();
data["url"] = $("#newUrl").val();
$.ajax({
url: "https://api.mongolab.com/api/1/databases/test/collections/urls?apiKey=5062cef2e4b088b309ccc936",
data: JSON.stringify(data),
type: "POST",
contentType: "application/json",
success: function () {
$("#addedUrl").html(
"<a target='_blank' href='http://giral.do/" +
data["key"] +
"!'>http://giral.do/" +
data["key"] +
"!</a>"
);
$("#newUrl").html("");
}
});
},
getUrl: function (e) {
e.preventDefault();
$.ajax({
url:
'https://api.mongolab.com/api/1/databases/test/collections/urls?q={"key":"' +
$("#existingId").val() +
'"}&apiKey=5062cef2e4b088b309ccc936',
type: "GET",
success: function (data) {
$("#existingUrl").html(_.pluck(data, "url"));
}
});
},
getId: function () {
var yr = new Date().getYear();
var mo = new Date().getMonth();
var day = new Date().getDate();
var rnd = Math.floor(Math.random() * 99);
return yr + "" + mo + day + rnd;
}
});
app.allUrlsView = Backbone.View.extend({
el: "#allUrls",
events: {
"#getAll click": "getAll"
},
getAll: function (e) {
e.preventDefault();
alert("working");
}
});
$(document).ready(function () {
new app.urlsView();
new app.allUrlsView();
$("#newUrl").focus();
});
</script>
<script type="text/html" id="urlListTemplate">
<%= key %> ..::.. <%= url %><br />
</script>
</head>
<body>
<div id="urls">
<form id="addUrl" action="#">
<p>
<label class="title">url</label
><input id="newUrl" type="text" style="width: 500px" />
</p>
<br />
<p>
<label class="title">mnemonic</label
><input id="newMnemonic" type="text" style="width: 200px" />
</p>
<br />
<p><label> </label><button id="add">Add</button></p>
<div class="info" id="addedUrl"></div>
</form>
<hr />
<form id="getUrl" action="#">
<p>
<label class="title">which id?</label
><input id="existingId" type="text" style="width: 100px" />
</p>
<p>
<br />
</p>
<p><label> </label><button id="get">Get</button></p>
<div class="info" id="existingUrl"></div>
</form>
</div>
<div id="allUrls">
<hr />
<form id="getUrls" action="#">
<br />
<p><label> </label><button id="getAll">Get All</button></p>
<div class="info" id="allUrls"></div>
</form>
</div>
</body>
</html>