erp5_officejs: block runing if application files synchronize
integrate text, spreadsheet, presentation router to one officejs_router add global settings gadget configuration string speedup replication modification_date use
Showing
/*global window, rJS */ | ||
/*global window, rJS, jIO, URI, location, console, document, RSVP, loopEventListener, navigator */ | ||
/*jslint nomen: true, indent: 2, maxerr: 3*/ | ||
(function (window, rJS) { | ||
"use strict"; | ||
var gadget_klass = rJS(window), | ||
NAME, | ||
MAIN_PAGE_PREFIX = "gadget_officejs_", | ||
DEFAULT_PAGE = "document_list", | ||
REDIRECT_TIMEOUT = 5000; | ||
function get_jio_cache_storage(name) { | ||
return { | ||
type: "query", | ||
sub_storage: { | ||
type: "uuid", | ||
sub_storage: { | ||
type: "indexeddb", | ||
database: 'officejs_' + name + '_cache_erp5' | ||
} | ||
} | ||
}; | ||
} | ||
function get_jio_replicate_storage(name, modification_date) { | ||
var erp5_query, | ||
sdk_name; | ||
erp5_query = [ | ||
"ooffice/apps/common/%", | ||
"ooffice/vendor/%", | ||
"ooffice/sdkjs/common/%" | ||
]; | ||
switch (name) { | ||
case "spreadsheet": | ||
sdk_name = "cell"; | ||
erp5_query.push("ooffice/apps/" + name + "editor/main/%"); | ||
erp5_query.push("ooffice/sdkjs/word/%"); | ||
break; | ||
case "text": | ||
sdk_name = "word"; | ||
erp5_query.push("ooffice/apps/documenteditor/main/%"); | ||
erp5_query.push("ooffice/sdkjs/cell/model/%"); | ||
erp5_query.push("ooffice/sdkjs/cell/utils/%"); | ||
break; | ||
case "presentation": | ||
sdk_name = "slide"; | ||
erp5_query.push("ooffice/apps/" + name + "editor/main/%"); | ||
erp5_query.push("ooffice/sdkjs/word/%"); | ||
erp5_query.push("ooffice/sdkjs/cell/%"); | ||
break; | ||
} | ||
erp5_query.push("ooffice/sdkjs/" + sdk_name + "/%"); | ||
erp5_query = erp5_query.map(function (currentValue) { | ||
return 'url_string: "' + currentValue + '"'; | ||
}).join(' OR '); | ||
erp5_query = erp5_query + ' OR ' + [ | ||
"ooffice/gadget_ooffice.js", | ||
"ooffice/gadget_ooffice.html", | ||
"ooffice/apps/require.js", | ||
"ooffice/apps/css.js", | ||
"ooffice/apps/underscore.js", | ||
"ooffice/apps/backbone.js", | ||
"ooffice/apps/bootstrap.js", | ||
"ooffice/apps/text.js", | ||
"ooffice/apps/xregexp-all-min.js", | ||
"ooffice/apps/jquery.mousewheel.js" | ||
].map(function (currentValue) { | ||
return 'url_string: ="' + currentValue + '"'; | ||
}).join(' OR '); | ||
erp5_query = erp5_query + ' OR ' + [ | ||
"gadget_officejs_spreadsheet_router.html", | ||
|
||
"gadget_officejs_router.js", | ||
"gadget_officejs_page_document_list.html", | ||
"gadget_officejs_page_document_list.js", | ||
"gadget_officejs_jio_" + name + "_view.html", | ||
"gadget_officejs_jio_spreadsheet_view.js", | ||
"gadget_officejs_page_add_document.html", | ||
"gadget_officejs_page_add_document.js", | ||
"gadget_erp5_editor_panel.html", | ||
"gadget_erp5_editor_panel.js", | ||
"URI.js", | ||
"dygraph.js", | ||
"gadget_erp5.css", | ||
"gadget_global.js", | ||
"gadget_jio.html", | ||
"gadget_jio.js", | ||
"gadget_translate.html", | ||
"gadget_translate.js", | ||
"i18next.js", | ||
"jiodev.js", | ||
"jquery.js", | ||
"jquerymobile.css", | ||
"jquerymobile.js", | ||
"renderjs.js", | ||
"rsvp.js", | ||
"gadget_officejs_header.html", | ||
"gadget_officejs_jio.html", | ||
"gadget_officejs_page_add_text_document.html", | ||
|
||
"gadget_officejs_page_jio_configurator.html", | ||
"gadget_officejs_page_jio_dav_configurator.html", | ||
"gadget_officejs_page_login.html", | ||
"gadget_officejs_page_logout.html", | ||
"gadget_officejs_page_share_webrtc_jio.html", | ||
"gadget_officejs_page_sync.html", | ||
"gadget_translation.html", | ||
"gadget_officejs_webrtc_jio.html", | ||
"gadget_officejs_widget_listbox.html", | ||
"gadget_officejs_header.js", | ||
"gadget_officejs_jio.js", | ||
"gadget_officejs_page_add_text_document.js", | ||
"gadget_officejs_page_jio_configurator.js", | ||
"gadget_officejs_page_jio_dav_configurator.js", | ||
"gadget_officejs_page_login.js", | ||
"gadget_officejs_page_logout.js", | ||
"gadget_officejs_page_share_webrtc_jio.js", | ||
"gadget_officejs_page_sync.js", | ||
"gadget_officejs_application_panel.html", | ||
"gadget_officejs_application_panel.js", | ||
"gadget_translation.js", | ||
"gadget_translation_data.js", | ||
"gadget_officejs_webrtc_jio.js", | ||
"gadget_officejs_widget_listbox.js", | ||
"erp5_launcher.js", | ||
"erp5_launcher.html" | ||
].map(function (currentValue) { | ||
return '(reference: ="' + currentValue + '")'; | ||
}).join(' OR '); | ||
erp5_query = "(" + erp5_query + ")"; | ||
if (modification_date) { | ||
modification_date = ' AND modification_date:>="' | ||
+ modification_date + '" ' | ||
} else { | ||
modification_date = ""; | ||
} | ||
return jIO.createJIO({ | ||
type: "replicate", | ||
query: { | ||
query: '(portal_type: ("Web Style", "Web Page", "Web Script")) AND ' + | ||
erp5_query + modification_date, | ||
limit: [0, 1234567890] | ||
}, | ||
use_remote_post: true, | ||
conflict_handling: 2, | ||
check_local_modification: false, | ||
check_local_creation: false, | ||
check_local_deletion: false, | ||
check_remote_modification: true, | ||
check_remote_creation: true, | ||
check_remote_deletion: true, | ||
local_sub_storage: { | ||
type: "attachasproperty", | ||
map: { | ||
text_content: "text_content", | ||
data: "data" | ||
}, | ||
sub_storage: get_jio_cache_storage(name) | ||
}, | ||
remote_sub_storage: { | ||
type: "erp5", | ||
url: (new URI("hateoasnoauth")) | ||
.absoluteTo(location.href) | ||
.toString(), | ||
default_view_reference: "jio_view" | ||
} | ||
}); | ||
} | ||
function listenHashChange(gadget) { | ||
function extractHashAndDispatch(evt) { | ||
var hash = (evt.newURL || window.location.toString()).split('#')[1], | ||
... | ... | @@ -38,7 +199,7 @@ |
} | ||
var result = loopEventListener(window, 'hashchange', false, | ||
extractHashAndDispatch), | ||
extractHashAndDispatch), | ||
event = document.createEvent("Event"); | ||
event.initEvent('hashchange', true, true); | ||
event.newURL = window.location.toString(); | ||
... | ... | @@ -58,7 +219,9 @@ |
}); | ||
}) | ||
.declareMethod("getCommandUrlFor", function(options) { | ||
.declareAcquiredMethod('getSetting', 'getSetting') | ||
.declareAcquiredMethod('setSetting', 'setSetting') | ||
.declareMethod("getCommandUrlFor", function (options) { | ||
var prefix = '', | ||
result, | ||
key; | ||
... | ... | @@ -121,13 +284,13 @@ |
} | ||
return { | ||
url: MAIN_PAGE_PREFIX + "jio_" | ||
+ base_portal_type | ||
+ "_" + args.page + ".html", | ||
+ base_portal_type | ||
+ "_" + args.page + ".html", | ||
options: sub_options | ||
}; | ||
}); | ||
}) | ||
.declareAcquiredMethod('jio_get', 'jio_get') | ||
.declareAcquiredMethod('renderApplication', 'renderApplication') | ||
.declareMethod('start', function () { | ||
... | ... | @@ -142,6 +305,9 @@ |
function push(a, b) { | ||
queue.push(function () { | ||
if (a == "portal_type") { | ||
NAME = b.toLowerCase(); | ||
} | ||
return gadget.setSetting(a, b); | ||
}); | ||
} | ||
... | ... | @@ -159,6 +325,53 @@ |
.push(function () { | ||
return gadget.props.start_deferred.promise; | ||
}) | ||
.push(function () { | ||
if (('serviceWorker' in navigator) && (NAME != "web page")) { | ||
return jIO.createJIO(get_jio_cache_storage(NAME)).allDocs({ | ||
query: '', | ||
sort_on: [ | ||
['modification_date', 'descending'] | ||
], | ||
limit: [0, 1], | ||
select_list: ['modification_date'] | ||
}) | ||
.push(function (data) { | ||
var modification_date, | ||
jio_store; | ||
if (data.data.total_rows == 1) { | ||
modification_date = data.data.rows[0].value.modification_date | ||
} | ||
jio_store = get_jio_replicate_storage(NAME); | ||
return gadget.getSetting('jio_' + NAME + '_cache_description') | ||
.push(function (query) { | ||
if (jio_store.__storage._query_options.query == query) { | ||
return get_jio_replicate_storage(NAME, modification_date).repair(); | ||
} else { | ||
return gadget.setSetting( | ||
'jio_' + NAME + '_cache_description', jio_store.__storage._query_options.query | ||
) | ||
.push(function () { | ||
return jio_store.repair(); | ||
}); | ||
} | ||
}) | ||
.push(function () { | ||
navigator.serviceWorker.register('gadget_officejs_' + NAME + '_serviceworker.js') | ||
.then(function (reg) { | ||
// registration worked | ||
console.log('Registration succeeded. Scope is ' + reg.scope); | ||
}) | ||
.then(undefined, function (error) { | ||
// registration failed | ||
console.log('Registration failed with ' + error); | ||
}); | ||
return {} | ||
}); | ||
}); | ||
} else { | ||
return {}; | ||
} | ||
}) | ||
.push(function () { | ||
return listenHashChange(gadget); | ||
}); | ||
... | ... |