156 |
157 |
Close
160 |
Settings
161 |
162 |
163 |
172 |
173 |
174 |
Version : 1.0
175 |
176 |
177 |
178 |
179 |
180 |
--------------------------------------------------------------------------------
/scripts/api.js:
--------------------------------------------------------------------------------
1 |
2 | // API functions
3 |
4 | define(['require','jquery','conf','router','models','utils'],
5 | function(require, $, conf, router, models, utils){
6 |
7 | // AJAX defaults
8 | $.ajaxSetup({
9 | url: conf.apiPath + 'api/',
10 | contentType: "application/json",
11 | dataType: 'json',
12 | cache: 'false',
13 | type: 'post',
14 | timeout: 15000 // 15s by default
15 | });
16 |
17 |
18 | /* AJAX error handler */
19 | function ajaxErrorHandler(event, jqXHR, ajaxSettings, thrownError){
20 | // state of the XHR
21 | var state = jqXHR.readyState;
22 |
23 | if (state == 4){
24 | //DONE
25 |
26 | if (jqXHR.status != 200){
27 | alert ("There is probably a configuration error." +
28 | " An API call returned: "+ jqXHR.status +
29 | " (" + jqXHR.statusText + ")");
30 | } else {
31 | // API errors go to the console
32 | utils.log('API error: ' + thrownError);
33 | }
34 | } else {
35 | // other states also go to the console too
36 | utils.log("API error with state " + state + ": " +
37 | thrownError);
38 | }
39 | }
40 |
41 | /* Most of the calls (except login, logout, isLoggedIn)
42 | require valid login session or will return this
43 | error object: {"error":"NOT_LOGGED_IN"} */
44 | function apiErrorHandler(msg){
45 | if (msg.error != "NOT_LOGGED_IN"){
46 | // real error
47 | alert('apiErrorHandler\nUnknown API error message' + msg.error);
48 |
49 | } else {
50 | // need to login
51 | if (! location.hash.startsWith("#login")){
52 |
53 | // before redirecting user to the login page
54 | // we need to test if TTRSS is in SINGLE USER MODE
55 | jQuery.ajax({
56 | data: JSON.stringify({op: "login"}),
57 | async: false
58 | }).done(function(data){
59 | if (data.status == 1){
60 | // we're really not logged in
61 | var dest = "login"; // new destination
62 |
63 | if (location.hash != ""){
64 | // we store where we're coming from in a query string
65 | dest += "?from=" + location.hash;
66 | }
67 | require('router').myRouter.navigate(dest, {trigger: true});
68 |
69 | } else {
70 | // SINGLE_USER_MODE
71 | require('models').settings.set("sid", data.content.session_id);
72 | require('models').settings.save();
73 |
74 | window.location.reload(true);
75 | }
76 | });
77 |
78 | } // else user is already where he needs to be
79 | }
80 | } // apiErrorHandler
81 |
82 | // my handler for AJAX errors
83 | $(document).ajaxError(ajaxErrorHandler);
84 |
85 | return {
86 |
87 | /* function to call TTRSS
88 | - req => the request as a JSON object
89 | - success => the success callback (one param the content)
90 | - async => async call? */
91 | ttRssApiCall: function(req, success, async){
92 | var data = req;
93 | // circular dependency for models
94 | var sid = require('models').settings.get("sid");
95 |
96 | if (sid != undefined){
97 | data.sid = sid;
98 | }
99 |
100 | jQuery.ajax(
101 | {
102 | data: JSON.stringify(data),
103 | async: async
104 | }
105 | )
106 | .done(function(data){
107 | if (data.status == 0){
108 | success(data.content);
109 | } else {
110 | apiErrorHandler(data.content);
111 | }
112 | });
113 | }, // ttRssApiCall
114 |
115 |
116 |
117 | /* to make a logout call */
118 | logout: function(){
119 | var msg = {
120 | 'op': 'logout'
121 | };
122 |
123 | this.ttRssApiCall(msg,
124 | function(){
125 | require('router').myRouter.navigate('login', {trigger: true});
126 | },
127 | function(m){
128 | alert('Could not logout :\n' + m);
129 | }, true
130 | );
131 | } //logout
132 |
133 | } //return
134 |
135 |
136 | }); //define
137 |
--------------------------------------------------------------------------------
/scripts/conf.js-dist:
--------------------------------------------------------------------------------
1 | /* Copy conf.js-dist to conf.js
2 | and set it up for your environment.
3 |
4 | DO NOT USE ANY FILESYSTEM PATH */
5 |
6 | define({
7 |
8 | /* URL to access your Tiny Tiny RSS installation */
9 | apiPath: "/tt-rss/"
10 |
11 | });
12 |
--------------------------------------------------------------------------------
/scripts/lib/require.js:
--------------------------------------------------------------------------------
1 | /*
2 | RequireJS 2.1.5 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
3 | Available via the MIT or new BSD license.
4 | see: http://github.com/jrburke/requirejs for details
5 | */
6 | var requirejs,require,define;
7 | (function(aa){function I(b){return"[object Function]"===L.call(b)}function J(b){return"[object Array]"===L.call(b)}function y(b,c){if(b){var d;for(d=0;d