Commit b25768be authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

erp5_officejs: Bookmark Dispatcher, update encoding

parent a097e772
/*globals window, RSVP, rJS*/ /*globals window, RSVP, rJS*/
/*jslint indent: 2, nomen: true, maxlen: 80*/ /*jslint indent: 2, nomen: true, maxlen: 80*/
(function (window, RSVP, rJS) { (function (window, RSVP, rJS) {
"use strict"; "use strict";
function getSearchedString() { function getSearchedString() {
var regex = new RegExp("[\\#?&]search=([^&]*)"), var regex = new RegExp("[\\#?&]search=([^&]*)"),
results = regex.exec(window.location.hash); results = regex.exec(window.location.hash);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
} }
function updateSearchUrl(event) { function updateSearchUrl(event) {
var gadget = this; var gadget = this;
makeOptionDict(gadget) makeOptionDict(gadget)
.push(function() { .push(function() {
return gadget.getSetting("option"); return gadget.getSetting("option");
}) })
.push(function(option) { .push(function(option) {
return gadget.getUrlFor(option); return gadget.getUrlFor(option);
}) })
.push(function(url) { .push(function(url) {
url = window.location.href + url; url = window.location.href + url;
gadget.props.element.getElementsByClassName("search-engine-url")[0].innerHTML = url; gadget.props.element.getElementsByClassName("search-engine-url")[0].innerHTML = url;
}); });
} }
rJS(window) rJS(window)
.ready(function (g) { .ready(function (g) {
g.props = {}; g.props = {};
return g.getElement() return g.getElement()
.push(function (element) { .push(function (element) {
g.props.element = element; g.props.element = element;
}); });
}) })
.declareAcquiredMethod("translate", "translate") .declareAcquiredMethod("translate", "translate")
.declareAcquiredMethod("getUrlFor", "getUrlFor") .declareAcquiredMethod("getUrlFor", "getUrlFor")
.declareAcquiredMethod("redirect", "redirect") .declareAcquiredMethod("redirect", "redirect")
.declareAcquiredMethod("updateHeader", "updateHeader") .declareAcquiredMethod("updateHeader", "updateHeader")
.declareAcquiredMethod('getSetting', 'getSetting') .declareAcquiredMethod('getSetting', 'getSetting')
.declareAcquiredMethod("jio_allDocs", "jio_allDocs") .declareAcquiredMethod("jio_allDocs", "jio_allDocs")
.declareAcquiredMethod("getSetting", "getSetting") .declareAcquiredMethod("getSetting", "getSetting")
.declareAcquiredMethod("setSetting", "setSetting") .declareAcquiredMethod("setSetting", "setSetting")
.declareMethod("render", function (options) { .declareMethod("render", function (options) {
var gadget = this, var gadget = this,
portal_type = null, portal_type = null,
option = { option = {
auto_redirect: false, auto_redirect: false,
search_engine: "" search_engine: ""
}; };
return new RSVP.Queue() return new RSVP.Queue()
.push(gadget.updateHeader({title: 'Search in Bookmarks'})) .push(gadget.updateHeader({title: 'Search in Bookmarks'}))
.push(function () { .push(function () {
return gadget.getSetting("portal_type") return gadget.getSetting("portal_type")
.push(function(result) { .push(function(result) {
portal_type = result; portal_type = result;
}); });
}) })
.push(function (){ .push(function (){
return gadget.getSetting("option") return gadget.getSetting("option")
.push(function(result) { .push(function(result) {
if (result) { if (result) {
option = result; option = result;
} }
}); });
}) })
.push(function () { .push(function () {
var search = window.decodeURIComponent(getSearchedString()), var search = window.decodeURIComponent(getSearchedString()),
query = ""; query = "";
if (search) { if (search) {
query = { query = {
query: '(title:"%' + search + '%" OR url_string:"%' + search + '%" OR description:"%' + search + '%") AND portal_type:"' + portal_type + '"', query: '(title:"%' + search + '%" OR url_string:"%' + search + '%" OR description:"%' + search + '%") AND portal_type:"' + portal_type + '"',
select_list: ['title', 'url_string', 'description'], select_list: ['title', 'url_string', 'description'],
}; };
return gadget.jio_allDocs(query) return gadget.jio_allDocs(query)
.push(function (query_result) { .push(function (query_result) {
var result_list_length = query_result.data.rows.length; var result_list_length = query_result.data.rows.length;
// if 0 result, let's search with a real search engine // if 0 result, let's search with a real search engine
if (result_list_length === 0 && option.search_engine !== '') { if (result_list_length === 0 && option.search_engine !== '') {
window.location.href = option.search_engine + window.encodeURIComponent(search); window.location.href = option.search_engine + window.encodeURIComponent(search);
} }
// if 1 result, we go there // if 1 result, we go there
else if (result_list_length === 1 && option.auto_redirect === true) { else if (result_list_length === 1 && option.auto_redirect === true) {
window.location.href = query_result.data.rows[0].value.url_string; window.location.href = query_result.data.rows[0].value.url_string;
} }
else { else {
return gadget.getUrlFor({page: "bookmark_list", search: window.encodeURIComponent(search)}) return gadget.getUrlFor({page: "bookmark_list", search: window.encodeURIComponent(search)})
.push(function (url) { .push(function (url) {
window.location.href = url; window.location.href = url;
}); });
} }
}); });
} }
}); });
}) })
.onEvent("submit", function () { .onEvent("submit", function () {
var gadget = this; var gadget = this;
//var option_parameter = gadget.getSetting("option"); //var option_parameter = gadget.getSetting("option");
var option_parameter = { var option_parameter = {
search: window.encodeURIComponent(gadget.props.element.getElementsByTagName('input')[0].value), search: window.encodeURIComponent(gadget.props.element.getElementsByTagName('input')[0].value),
page: 'bookmark_dispatcher' page: 'bookmark_dispatcher'
}; };
return gadget.getUrlFor(option_parameter) return gadget.getUrlFor(option_parameter)
.push(function (url) { .push(function (url) {
window.location.href = url; window.location.href = url;
}); });
}); });
}(window, RSVP, rJS)); }(window, RSVP, rJS));
\ No newline at end of file
...@@ -236,7 +236,7 @@ ...@@ -236,7 +236,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>956.64652.37486.36010</string> </value> <value> <string>960.32759.37420.3857</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -254,7 +254,7 @@ ...@@ -254,7 +254,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1485780744.68</float> <float>1499162399.03</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