Commit 1622517c authored by Sven Franck's avatar Sven Franck

erp5_officejs_afs_directory: switch from local JSON to fetching remotely from Github

parent e92290e6
/*
* Copyright 2016, Nexedi SA
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
*/
/*jslint nomen: true*/ /*jslint nomen: true*/
/*global jIO, RSVP, DOMParser, Blob */ /*global jIO, RSVP, JSON */
(function (jIO, RSVP, JSON) {
(function (jIO, RSVP, JSON, UriTemplate) {
"use strict"; "use strict";
var DOCUMENT_URL = "{document_id}",
document_url_template = UriTemplate.parse(DOCUMENT_URL);
function ajax(storage, options) {
if (options === undefined) {
options = {};
}
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax(options);
});
}
function PublisherStorage(spec) { function PublisherStorage(spec) {
// NOTE: requires Website Layout Configuration CSP modification
// => connect-src 'self' https://raw.githubusercontent.com https://api.github.com data:;
//https://api.github.com/repos/Nexedi/awesome-free-software
//https://api.github.com/repos/Nexedi/awesome-free-software/contents/?ref=master
//https://api.github.com/repos/Nexedi/awesome-free-software/contents/alfresco.json
//https://raw.githubusercontent.com/Nexedi/awesome-free-software/master/alfresco.json
this._href = spec.href || 'https://api.github.com/repos/';
this._user = spec.user || 'Nexedi';
this._repo = spec.repo || 'awesome-free-software';
} }
PublisherStorage.prototype.get = function (id) { PublisherStorage.prototype.get = function (id) {
var context = this;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return ajax(context, { return jIO.util.ajax({type: "GET", url: id, dataType: "text"});
type: "GET",
url: document_url_template.expand({document_id: id}),
dataType: "text",
});
}) })
.push(function (response) { .push(
return JSON.parse(response.target.response || response.target.responseText); function (response) {
}, function (error) { return JSON.parse(response.target.response || response.target.responseText);
if ((error.target !== undefined) && },
(error.target.status === 404)) { function (error) {
throw new jIO.util.jIOError("Cannot find document", 404); if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
throw error;
} }
throw error; );
});
}; };
PublisherStorage.prototype.hasCapacity = function (name) { PublisherStorage.prototype.hasCapacity = function (name) {
...@@ -54,32 +42,32 @@ ...@@ -54,32 +42,32 @@
}; };
PublisherStorage.prototype.buildQuery = function (options) { PublisherStorage.prototype.buildQuery = function (options) {
var context = this, var url = this._href + this._user + '/' + this._repo + '/contents/?ref=master';
rows = [];
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
return ajax(context, { return jIO.util.ajax({"type": "GET", "url": url});
type: "GET", })
url: document_url_template.expand({document_id: 'publisher_list.txt'}) .push(function (data) {
}); var data_list = JSON.parse(data.target.response || data.target.responseText),
}) result_list = [],
.push(function (response) { data_entry,
var response_list = (response.target.response || response.target.responseText).split('\n'); len,
i;
for (var entry in response_list) {
if (response_list[entry] !== "") { for (i = 0, len = data_list.length; i < len; i += 1) {
rows.push({ data_entry = data_list[i];
id: response_list[entry], if (data_entry.path !== "README.md") {
value: {} result_list.push({
}); id: data_entry.download_url,
value: {}
});
}
} }
} return result_list;
});
return rows;
});
}; };
jIO.addStorage('publisher_storage', PublisherStorage); jIO.addStorage('publisher_storage', PublisherStorage);
}(jIO, RSVP, JSON, UriTemplate)); }(jIO, RSVP, JSON));
\ No newline at end of file \ No newline at end of file
...@@ -337,7 +337,7 @@ ...@@ -337,7 +337,7 @@
</item> </item>
<item> <item>
<key> <string>configuration_content_security_policy</string> </key> <key> <string>configuration_content_security_policy</string> </key>
<value> <string>default-src \'self\'; img-src \'self\' * data:; media-src \'self\' blob:; connect-src \'self\' data:; script-src \'self\' \'unsafe-eval\'; font-src \'self\'; style-src \'self\' data:; frame-src \'self\' data:</string> </value> <value> <string>default-src \'self\'; img-src \'self\' * data:; media-src \'self\' blob:; connect-src \'self\' https://raw.githubusercontent.com https://api.github.com data:; script-src \'self\' \'unsafe-eval\'; font-src \'self\'; style-src \'self\' data:; frame-src \'self\' data:</string> </value>
</item> </item>
<item> <item>
<key> <string>configuration_default_view_action_reference</string> </key> <key> <string>configuration_default_view_action_reference</string> </key>
...@@ -614,7 +614,7 @@ ...@@ -614,7 +614,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>953.59466.37233.9915</string> </value> <value> <string>954.25873.23492.36983</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -632,7 +632,7 @@ ...@@ -632,7 +632,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1473847779.13</float> <float>1475681337.69</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment