-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
162 lines (149 loc) · 5.28 KB
/
index.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
<html>
<head>
<title>Anki Deck Builder</title>
<script src="bower_components/tinymce/tinymce.min.js"></script>
<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/angular-route/angular-route.min.js"></script>
<script src="bower_components/angular-ui-tinymce/src/tinymce.js"></script>
<script src="bower_components/mgo-mousetrap/mousetrap.js"></script>
<script src="bower_components/mgo-mousetrap/wMousetrap.js"></script>
<script src="bower_components/ng-file-upload/angular-file-upload.min.js"></script>
<script src="bower_components/underscore/underscore-min.js"></script>
<script src="bower_components/Blob.js/Blob.js"></script>
<script src="bower_components/FileSaver.js/FileSaver.min.js"></script>
<script src="bower_components/angular-local-storage/dist/angular-local-storage.min.js"></script>
<script>
angular.module('ankiDeckBuilder', ['ui.tinymce', 'mgo-mousetrap', 'angularFileUpload', 'LocalStorageModule', 'ngRoute'])
.config(['$compileProvider', 'localStorageServiceProvider', '$routeProvider',
function($compileProvider, localStorageServiceProvider, $routeProvider) {
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|blob):/);
localStorageServiceProvider.setPrefix('ankiDeckBuilder');
$routeProvider.when('/deck/:deck', {
templateUrl: 'partials/deck.html',
controller: 'deckBuilder'
}).when('/decks', {
templateUrl: 'partials/decks.html',
controller: 'decksController'
}).otherwise({
redirectTo: '/decks'
});
}]);
</script>
<script>
angular.module('ankiDeckBuilder').factory('deckStorage', ['localStorageService',
function(localStorageService) {
return {
get: function(deckName) {
return localStorageService.get(deckName);
},
set: function(deckName, deck) {
return localStorageService.set(deckName, deck);
},
decks: function() {
return localStorageService.keys();
},
bind: function(scope, scopePropertyName, deckName) {
return localStorageService.bind(scope, scopePropertyName, undefined, deckName);
}
}
}]);
</script>
<script>
angular.module('ankiDeckBuilder').controller('decksController', ['$scope', 'deckStorage', '$location',
function($scope, deckStorage, $location) {
$scope.decks = deckStorage.decks();
$scope.createDeck = function() {
var deckName = prompt('Deck name: ');
if(deckName === "")
alert('Deck cannot have an empty name');
else if(_.contains($scope.decks, deckName))
alert('The name ' + deckName + ' is already in use.');
else if (!!deckName) {
deckStorage.set(deckName, [{}]);
$location.path('/deck/' + deckName);
}
}
}
]);
</script>
<script>
angular.module('ankiDeckBuilder').controller('deckBuilder',
['$scope', '$timeout', '$routeParams', 'deckStorage',
function ($scope, $timeout, $routeParams, deckStorage) {
$scope.add = addCard;
$scope.delete = deleteCard;
$scope.downloadCSVBlob = downloadCSVBlob;
$scope.deck = deckStorage.get($routeParams.deck);
deckStorage.bind($scope, 'deck', $routeParams.deck);
var addFromEditor = {
setup: setupToAddFromEditor
}
$scope.addFromEditor = addFromEditor;
$scope.focusOnInit = angular.extend({
init_instance_callback: focusOnInit
}, addFromEditor);
$scope.onFileSelect = onFileSelect;
function onFileSelect(files) {
var reader = new FileReader();
reader.onload = function(readEvent) {
$timeout(
function() {
var lines = readEvent.currentTarget.result.trim().split("\n");
$scope.deck = _.map(lines,
function(line) {
var questionAndAnswer = line.split("\t");
return {
question: questionAndAnswer[0],
answer: questionAndAnswer[1]
};
})
}
);
}
reader.readAsText(files[0]);
}
function addCard() {
$scope.deck.push({});
}
function setupToAddFromEditor(editor) {
editor.on('keyup', function(e) {
if(e.altKey && e.keyCode == 34) {
addCard();
$scope.$apply();
}
});
}
function deleteCard(index) {
var cardToDelete = $scope.deck[index];
if(confirm("Are you sure you want to delete the card '" + cardToDelete.question +"'?"))
$scope.deck.splice(index, 1)
}
function downloadCSVBlob() {
var lineFeedEncoding = encoding('\n', '');
var carriageReturnEncoding = encoding('\r', '');
var commaEncoding = encoding(',', '%2C');
var cards = _.map($scope.deck, function (card) { return encode(card.question) + "," + encode(card.answer) })
var csv = _.reduce(cards, function (cards, card) { return cards + "\r\n" + card; })
var blob = new Blob([csv], {type: 'text/csv'});
saveAs(blob, $routeParams.deck + ".csv");
function encoding(toEncode, toEncodeAs) {
return function (value) {
return value.replace(new RegExp(toEncode, "g"), toEncodeAs);
}
};
function encode(value) {
var encoded = lineFeedEncoding(carriageReturnEncoding(commaEncoding(value)));
return encoded
};
}
function focusOnInit(tinyMceInstance) {
tinyMceInstance.focus();
}
}]);
</script>
</head>
<body ng-app="ankiDeckBuilder" ng-controller="deckBuilder" w-mousetrap="{'alt+pagedown': add}">
<div ng-view>
</div>
</body>
</html>