forked from hackfoldr/hackfoldr
-
Notifications
You must be signed in to change notification settings - Fork 27
/
controllers.ls
139 lines (127 loc) · 4.49 KB
/
controllers.ls
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
angular.module 'app.controllers' <[ui.state]>
.controller AppCtrl: <[$scope $location $resource $rootScope]> ++ (s, $location, $resource, $rootScope) ->
s <<< {$location}
s.$watch '$location.path()' (activeNavId or '/') ->
s <<< {activeNavId}
s.getClass = (id) ->
if s.activeNavId.substring 0 id.length is id
'active'
else
''
.controller HackFolderCtrl: <[$scope $state HackFolder]> ++ ($scope, $state, HackFolder) ->
$scope <<< do
hasViewMode: -> it.match /g(doc|present|draw)/
sortableOptions: do
update: -> console?log \notyetupdated
iframes: HackFolder.iframes
docs: HackFolder.docs
tree: HackFolder.tree
activate: HackFolder.activate
HackFolder: HackFolder
reload: (hackId) -> HackFolder.getIndex hackId, true ->
$scope.$watch 'hackId' (hackId) ->
<- HackFolder.getIndex hackId, false
$scope.$watch 'docId' (docId) -> HackFolder.activate docId if docId
unless $scope.docId
if HackFolder.docs.0?id
$state.transitionTo 'hack.doc', { docId: that, hackId: $scope.hackId }
$scope.hackId = if $state.params.hackId => that else 's8r4l008sk'
$scope.$watch '$state.params.docId' (docId) ->
$scope.docId = encodeURIComponent encodeURIComponent docId if docId
.directive 'resize' <[$window]> ++ ($window) ->
(scope) ->
scope.width = $window.innerWidth
scope.height = $window.innerHeight
angular.element $window .bind 'resize' ->
scope.$apply ->
scope.width = $window.innerWidth
scope.height = $window.innerHeight
.directive \ngxNoclick ->
($scope, element, attrs) ->
$ element .click -> it.preventDefault!; false
.directive \ngxFinal ->
($scope, element, attrs) ->
$ element .click -> it.stopPropagation();
.factory HackFolder: <[$http]> ++ ($http) ->
iframes = {}
docs = []
tree = []
var hackId
do
iframes: iframes
collapsed: false
docs: docs
tree: tree
activate: (id, edit=false) ->
[{type}:doc] = [d for d in docs when d.id is id]
mode = if edit => \edit else \view
src = match type
| \gdoc =>
"https://docs.google.com/document/d/#id/#mode"
| \gsheet =>
"https://docs.google.com/spreadsheet/ccc?key=#id"
| \gpresent =>
"https://docs.google.com/presentation/d/#id/#mode"
| \gdraw =>
"https://docs.google.com/drawings/d/#id/#mode"
| \gsheet =>
"https://docs.google.com/spreadsheet/ccc?key=#id"
| \hackpad =>
"https://hackpad.com/#id"
| \ethercalc =>
"http://ethercalc.org/#id"
| \url => decodeURIComponent decodeURIComponent id
if iframes[id]
that <<< {src, mode}
else
iframes[id] = {src, doc, mode}
getIndex: (id, force, cb) ->
return cb docs if hackId is id and !force
csv <- $http.get "http://www.ethercalc.org/_/#{id}/csv"
.success
hackId := id
docs.length = 0
entries = for line in csv.split /\n/ when line
[url, title, ...rest] = line.split /,/
title -= /"/g
[_, prefix, url] = url.match /^"?(\s*)(\S+)"?$/
entry = { url, title, indent: prefix.length } <<< match url
| // ^https?:\/\/www\.ethercalc\.(?:com|org)/(.*) //
type: \ethercalc
id: that.1
| // https:\/\/docs\.google\.com/document/(?:d/)?([^/]+)/ //
type: \gdoc
id: that.1
| // https:\/\/docs\.google\.com/spreadsheet/ccc\?key=([^/?&]+) //
type: \gsheet
id: that.1
| // https:\/\/docs\.google\.com/drawings/(?:d/)?([^/]+)/ //
type: \gdraw
id: that.1
| // https:\/\/docs\.google\.com/presentation/(?:d/)?([^/]+)/ //
type: \gpresent
id: that.1
| // https?:\/\/hackpad\.com/(?:.*?-)?([\w]+)(\#.*)?$ //
type: \hackpad
id: that.1
| // ^(https?:\/\/[^/]+) //
type: \url
id: encodeURIComponent encodeURIComponent url
icon: "http://g.etfv.co/#{ that.1 }"
| otherwise => console?log \unrecognized url
entry.icon ?= "img/#{ entry.type }.png"
entry
last-parent = 0
docs.splice 0, -1, ...(entries.filter -> it?)
nested = for entry, i in docs when i > 0
if entry.indent
entries[last-parent]
..children ?= []
..children.push entry
..collapsed = true
null
else
last-parent = i
entry
tree.splice 0, -1, ...(nested.filter -> it?)
cb docs