summaryrefslogtreecommitdiff
path: root/frontend/static/js/api.js
blob: 797823eff3e56620747b8f10111b462a9d9427fb (plain)
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
var API =
	{
	HEADER: "#@scanjam 0.3b",
	BASE_URL: "http://"+serverHost+":"+serverPort,
	URL:
		{
		auth:
			{
			login: "/api/auth/login",
			logout: "/api/auth/logout",
			checkin: "/api/auth/checkin",
			sneakin: "/api/auth/sneakin",
			},
		room:
			{
			join: "/api/room/join",
			list: "/api/room/list",
			view: "/api/room/view",
			poll: "/api/room/poll",
			watch: "/api/room/watch",
			say: "/api/room/say",
			settings: "/api/room/settings",
			stats: "/stats",
			},
		video:
			{
			date: "/api/video/date",
			like: "/api/video/like",
			unlike: "/api/video/unlike",
			remove: "/api/video/remove",
			search: "/api/video/search",
			},
		user:
			{
			settings: "/api/user/settings",
			videos: "/api/user/videos",
			likes: "/api/user/likes",
			},
		},
	error: function (s)
		{
		d.error("API: "+s)
		return false
		},
	parse: function (api, raw)
		{
		if (! raw)
			return API.error("no result")
		var lines = raw.split("\n")
		if (lines.shift() !== API.HEADER)
			return API.error("bad header")
		if (! lines.length)
			return API.error("no content")
		return lines
		},
	init: function ()
		{
		d.warn("INIT API")
		for (type in API.URL)
			{
			for (name in API.URL[type])
				{
				API.URL[type][name] = API.BASE_URL + API.URL[type][name] + "/"
				}
			}
		// $.ajaxSetup({ timeout: 1000 })
		}
	}
var Local =
	{
	support: false,
	hash: null,
	get: null,
	set: null,
	_html5_get: function (key)
		{
		var val = localStorage["scanjam."+key]
		if (val === "true") return true
		if (val === "false") return false
		if (val === "undefined") return undefined
		return val
		},
	_html5_set: function (key, val)
		{
		if (val === undefined)
			localStorage["scanjam."+key] = ""
		else
			localStorage["scanjam."+key] = val
		},
	_hash_get: function (key)
		{
		if (key in Local.hash)
			return Local.hash[key]
		},
	_hash_set: function (key, val)
		{
		Local.hash[key] = val
		},
	_supports_html5_storage: function ()
		{
		try
			{ return 'localStorage' in window && window['localStorage'] !== null; }
		catch (e)
			{ return false }
		},
	like: function (videoid)
		{ Local.set("like."+videoid, true) },
	unlike: function (videoid)
		{ Local.set("like."+videoid, false) },
	isLiked: function (videoid)
		{ return Local.get("like."+videoid) },
	init: function ()
		{
		Local.support = Local._supports_html5_storage()
		if (Local.support)
			{
			d.warn("SUPPORTS LOCAL STORAGE")
			Local.get = Local._html5_get
			Local.set = Local._html5_set
			}
		else
			{
			d.error("NO LOCAL STORAGE")
			Local.hash = {}
			Local.get = Local._hash_get
			Local.set = Local._hash_set
			}
		}
	}
API.init()
Local.init()