From: Vincent Vanwaelscappel Date: Wed, 3 Jun 2015 09:52:59 +0000 (+0000) Subject: Add storage polyfill X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=44f7a33792014dcdb9ebecba6ba708bd79ef8b3d;p=fluidbook-html5.git Add storage polyfill --- diff --git a/js/libs/fluidbook/fluidbook.cache.js b/js/libs/fluidbook/fluidbook.cache.js index 5ef4632e..fd757ce7 100644 --- a/js/libs/fluidbook/fluidbook.cache.js +++ b/js/libs/fluidbook/fluidbook.cache.js @@ -1,67 +1,67 @@ function FluidbookCache(options) { - this._cache = {}; - this.options = options; - this._prefix = 'fluidbook.' + this.options.id + '.'; - this._support = false; - this._date = this.options.cacheDate; - this._support = Modernizr.localstorage; - this.init(); + this._cache = {}; + this.options = options; + this._prefix = 'fluidbook.' + this.options.id + '.'; + this._support = false; + this._date = this.options.cacheDate; + this._support = Modernizr.localstorage; + this.init(); } FluidbookCache.prototype = { - init: function() { - if (this._support) { - this.checkValidity(); - } - }, - clear: function() { - if (this._support) { - for (var i = localStorage.length - 1; i >= 0; i--) { - var key = localStorage.key(i); - if (key.indexOf(this._prefix) == 0) { - localStorage.removeItem(key); - } - } - } else { - this._cache = {}; - } - }, - isset: function(key) { - if (this._support) { - var res = localStorage.getItem(this._prefix + key) != null; - return res; - } else { - return this._cache[key] != null; - } - }, - get: function(key) { - var res; - if (this._support) { - res = localStorage.getItem(this._prefix + key); - } else { - res = this._cache[key]; - } - var f = res.substr(0, 1); - if (f == '[' || f == '{') { - res = json_parse(res); - } - return res; - }, - set: function(key, value) { - if (typeof value !== "string") { - value = JSON.stringify(value); - } - if (this._support) { - localStorage.setItem(this._prefix + key, value); - } else { - this._cache[key] = value; - } - }, - checkValidity: function() { - if (!this.isset('validity') || this.get('validity') != this._date) { - this.clear(); - this.set('validity', this._date); - } - } + init: function () { + if (this._support) { + this.checkValidity(); + } + }, + clear: function () { + if (this._support) { + for (var i = localStorage.length - 1; i >= 0; i--) { + var key = localStorage.key(i); + if (key.indexOf(this._prefix) == 0) { + localStorage.removeItem(key); + } + } + } else { + this._cache = {}; + } + }, + isset: function (key) { + if (this._support) { + var res = localStorage.getItem(this._prefix + key) != null; + return res; + } else { + return this._cache[key] != null; + } + }, + get: function (key) { + var res; + if (this._support) { + res = localStorage.getItem(this._prefix + key); + } else { + res = this._cache[key]; + } + var f = res.substr(0, 1); + if (f == '[' || f == '{') { + res = json_parse(res); + } + return res; + }, + set: function (key, value) { + if (typeof value !== "string") { + value = JSON.stringify(value); + } + if (this._support) { + localStorage.setItem(this._prefix + key, value); + } else { + this._cache[key] = value; + } + }, + checkValidity: function () { + if (!this.isset('validity') || this.get('validity') != this._date) { + this.clear(); + this.set('validity', this._date); + } + } } \ No newline at end of file diff --git a/js/libs/fluidbook/fluidbook.l10n.js b/js/libs/fluidbook/fluidbook.l10n.js index 2d8f998a..dd070f1b 100644 --- a/js/libs/fluidbook/fluidbook.l10n.js +++ b/js/libs/fluidbook/fluidbook.l10n.js @@ -1,140 +1,145 @@ function FluidbookL10N(fluidbook, lang) { - this.translations = {}; - this.multilang = []; - this.multilangEnabled = false; - this.fluidbook = fluidbook; + this.translations = {}; + this.multilang = []; + this.multilangEnabled = false; + this.fluidbook = fluidbook; - this.load(lang); - this.initMultilang(); + this.load(lang); + this.initMultilang(); } FluidbookL10N.prototype = { - load: function(lang) { - if (lang == undefined || lang == null || lang == '') { - lang = 'default'; - } - this.lang = lang; + load: function (lang) { + if (lang == undefined || lang == null || lang == '') { + lang = 'default'; + } + this.lang = lang; - this.dir = (this.getActiveLang() == 'ar' || this.getActiveLang() == 'he') ? 'rtl' : 'ltr'; - $('html').attr('dir', this.dir); - if (this.dir == 'rtl') { - $('html').removeClass('ltr').addClass('rtl'); - } else { - $("html").removeClass('rtl').addClass('ltr'); - } + this.dir = (this.getActiveLang() == 'ar' || this.getActiveLang() == 'he') ? 'rtl' : 'ltr'; + $('html').attr('dir', this.dir); + if (this.dir == 'rtl') { + $('html').removeClass('ltr').addClass('rtl'); + } else { + $("html").removeClass('rtl').addClass('ltr'); + } - this.translations = this.fluidbook.datas.l10n[lang]; - this.updateTranslations(); - }, - getActiveLang: function() { - if (this.lang != 'default') { - return this.lang; - } - return this.fluidbook.datas.defaultLang; - }, - initMultilang: function() { - var forceLocales = window.sessionStorage.getItem('locales'); - var force = false; - if (forceLocales === undefined || forceLocales === null || forceLocales === 'undefined') { - force = false; - } else { - force = true; - try { - forceLocales = json_parse(forceLocales); - } catch (err) { - force = false; - } - } + this.translations = this.fluidbook.datas.l10n[lang]; + this.updateTranslations(); + }, + getActiveLang: function () { + if (this.lang != 'default') { + return this.lang; + } + return this.fluidbook.datas.defaultLang; + }, + initMultilang: function () { + var forceLocales; + try { + forceLocales = window.sessionStorage.getItem('locales'); + } catch (err) { - if (!force) { - if (this.fluidbook.datas.multilang == '') { - return; - } - if (this.fluidbook.datas.multiApp) { - return; - } - } + } + var force = false; + if (forceLocales === undefined || forceLocales === null || forceLocales === 'undefined') { + force = false; + } else { + force = true; + try { + forceLocales = json_parse(forceLocales); + } catch (err) { + force = false; + } + } - this.multilangEnabled = true; - var $this = this; - var ml = this.fluidbook.datas.multilang.replace(/\r/g, "\n").replace(/\n+/g, "\n"); + if (!force) { + if (this.fluidbook.datas.multilang == '') { + return; + } + if (this.fluidbook.datas.multiApp) { + return; + } + } - var e = ml.split("\n"); - $.each(e, function(k, v) { - if (v == '') { - return; - } - var l = v.split(','); - if (l[0] == '') { - return; - } + this.multilangEnabled = true; + var $this = this; + var ml = this.fluidbook.datas.multilang.replace(/\r/g, "\n").replace(/\n+/g, "\n"); - var url = l[2]; - if (force) { - var publications = forceLocales[l[0]]; - var pubid = publications[0]; + var e = ml.split("\n"); + $.each(e, function (k, v) { + if (v == '') { + return; + } + var l = v.split(','); + if (l[0] == '') { + return; + } - try { - url = pubid; - } catch (err) { - url = ''; - } - } + var url = l[2]; + if (force) { + var publications = forceLocales[l[0]]; + var pubid = publications[0]; - var o = {lang: l[0], flag: l[1], url: url, langName: l[3], countryName: l[4]}; + try { + url = pubid; + } catch (err) { + url = ''; + } + } - $this.multilang[$this.multilang.length] = o; - }); - }, - getActiveFlag: function() { - var active = this.getActiveLang(); - var res = []; - for (var i = 0; i < this.multilang.length; i++) { - var l = this.multilang[i]; - if (l.lang == active) { - res.push(l); - } - } + var o = {lang: l[0], flag: l[1], url: url, langName: l[3], countryName: l[4]}; - if (res.length == 1) { - return res[0].flag; - } else if (res.length == 0) { - return false; - } else { - if (this.fluidbook.datas.country != '') { - for (var i in res) { - if (res[i].flag == this.fluidbook.datas.country) { - return res[i].flag; - } - } - return res[0].flag; - } - } - }, - updateTranslations: function() { - var $this = this; - $("#q").attr('placeholder', this.__('search')); - $("i.l10n").each(function() { - var t = $this.fluidbook.l10n.__($(this).attr('str')); - $(this).replaceWith(t); - }); - }, - __: function(str, markupIfNonAvailable) { - if (str === undefined) { - return; - } - if (str.substr(0, 1) == '!') { - return str.substr(1); - } + $this.multilang[$this.multilang.length] = o; + }); + }, + getActiveFlag: function () { + var active = this.getActiveLang(); + var res = []; + for (var i = 0; i < this.multilang.length; i++) { + var l = this.multilang[i]; + if (l.lang == active) { + res.push(l); + } + } + if (res.length == 1) { + return res[0].flag; + } else if (res.length == 0) { + return false; + } else { + if (this.fluidbook.datas.country != '') { + for (var i in res) { + if (res[i].flag == this.fluidbook.datas.country) { + return res[i].flag; + } + } + return res[0].flag; + } + } + }, + updateTranslations: function () { + var $this = this; + $("#q").attr('placeholder', this.__('search')); + $("i.l10n").each(function () { + var t = $this.fluidbook.l10n.__($(this).attr('str')); + $(this).replaceWith(t); + }); + }, + __: function (str, markupIfNonAvailable) { + if (str === undefined) { + return; + } + if (str.substr(0, 1) == '!') { + return str.substr(1); + } - if (this.translations[str] == undefined || this.translations[str] == null || this.translations[str] == '') { - if (markupIfNonAvailable == undefined || markupIfNonAvailable) { - return '~ ' + str + ' ~'; - } else { - return str; - } - } - return this.translations[str]; - } + + if (this.translations[str] == undefined || this.translations[str] == null || this.translations[str] == '') { + if (markupIfNonAvailable == undefined || markupIfNonAvailable) { + return '~ ' + str + ' ~'; + } else { + return str; + } + } + return this.translations[str]; + } }; \ No newline at end of file diff --git a/js/libs/fluidbook/fluidbook.nav.js b/js/libs/fluidbook/fluidbook.nav.js index 3a51c81c..9f4e256c 100644 --- a/js/libs/fluidbook/fluidbook.nav.js +++ b/js/libs/fluidbook/fluidbook.nav.js @@ -1,236 +1,244 @@ function FluidbookNav(fluidbook) { - this.fluidbook = fluidbook; - this._dimensions = this.fluidbook.datas.iconsDimensions; - this._inited = false; - this.setNav(); + this.fluidbook = fluidbook; + this._dimensions = this.fluidbook.datas.iconsDimensions; + this._inited = false; + this.setNav(); } FluidbookNav.prototype = { - getIcon: function(name) { - var src = 'data/images/' + name + '.'; - if (this.fluidbook.support.SVG) { - src += 'svg'; - } else { - src += 'png'; - } - if (this._dimensions[name] == undefined) { - return ''; - } - return this.fluidbook.loader.getImage(src, this._dimensions[name][0], this._dimensions[name][1]); - }, - addLink: function(name, href, id, help, before, c) { - if ($("#nav").find('#' + id).length > 0) { - return; - } - var l = this.getLink(name, href, id, help, c); - if (before == undefined) { - $("#nav").append(l); - } else { - $("#nav #" + before).before(l); - } - }, - getLink: function(name, href, id, help, className, icon) { - if (icon == undefined) { - icon = true; - } - var res = ' 0) { + return; + } + var l = this.getLink(name, href, id, help, c); + if (before == undefined) { + $("#nav").append(l); + } else { + $("#nav #" + before).before(l); + } + }, + getLink: function (name, href, id, help, className, icon) { + if (icon == undefined) { + icon = true; + } + var res = ' 0) { - this.addLink('nav-sommaire', '#/chapters', 'chapters', 'chapters'); - } - } - } else if (icon == 'friend') { - if (this.fluidbook.datas.share) { - this.addLink('nav-friend', '#/share', 'share', 'share'); - $("#share").click(function() { - if ($this.fluidbook.datas.phonegap != 'android') { - return true; - } - $this.fluidbook.intentShare(); - return false; - }); - } - } else if (icon == 'bookmark') { - if (this.fluidbook.datas.bookmark) { - this.addLink('nav-bookmark', '#/bookmark', 'bookmarks', 'bookmarks'); - $("#bookmarks").click(function() { - if (!$this.fluidbook.bookmarks.hasBookmarkedPages()) { - var message = $this.fluidbook.l10n.__("you don't have any bookmarks"); - if ($this.fluidbook.datas.phonegap) { - navigator.notification.alert(message, function() { - }, $this.fluidbook.l10n.__('bookmarks')); - } else { - window.alert(message); - } - return false; - } - }); - } - } else if (icon == 'pdf' || icon == 'print') { - if ($("#print").length == 0 && (this.fluidbook.datas.print || this.fluidbook.datas.pdf)) { - this.addLink('nav-print', '#', 'print', '!' + this.fluidbook.l10n.__('print') + ' | ' + this.fluidbook.l10n.__('download pdf')); - $("#print").on('click', function() { - $this.fluidbook.print(); - return false; - }); - } - } else if (icon == 'lang') { - if (this.fluidbook.l10n.multilangEnabled) { - this.addMultilangLink(); - } - } else if (icon == 'archives') { - if (this.fluidbook.datas.archivesLink != '') { - this.addLink('nav-archives', this.fluidbook.datas.archivesLink, 'archives', '!' + this.fluidbook.datas.archivesLabel); - } else if (this.fluidbook.datas.externalArchives != '') { - this.addLink('nav-archives', '#/archives', 'archives', '!' + this.fluidbook.datas.archivesLabel); - } - } else if (icon == 'help') { - this.addLink('nav-help', '#', 'help', ''); - } else if (icon == 'zoom' && !this.fluidbook.support.isMobile) { - this.addLink('nav-zoomin', '#', 'zoomin', 'zoom in'); - this.addLink('nav-zoomout', '#', 'zoomout', 'zoom out'); + for (var e in this.fluidbook.datas.navOrder) { + var icon = this.fluidbook.datas.navOrder[e]; + if (icon == 'home' && !skipHome) { - $("#zoomin").click(function(e) { - $this.fluidbook.desktop.clickZoom(e, 'in'); - return false; - }); + var homeURL = this.fluidbook.datas.home; + if (this.fluidbook.datas.home == '%apphome%') { + homeURL = decodeURIComponent(); + } + try { + if (homeURL != '') { + this.addLink('nav-home', window.sessionStorage.getItem('apphome'), 'home', 'home'); + } + } catch (err) { - $("#zoomout").click(function() { - $this.fluidbook.desktop.clickZoom(e, 'out'); - return false; - }); - } else if (icon == 'fullscreen' && Modernizr.fullscreen) { - this.addLink('nav-fullscreen', '#', 'fullscreen', 'switch between fullscreen and normal'); - $("#fullscreen").click(function() { - screenfull.toggle(); - return false; - }) - } - } + } + } else if (icon == 'index') { + this.addLink('nav-index', '#/index', 'index', 'overview'); + } else if (icon == 'chapters') { + if (this.fluidbook.datas.displayChaptersIcon) { + if (this.fluidbook.datas.chaptersPage != '') { + this.addLink('nav-sommaire', '#/page/' + this.fluidbook.datas.chaptersPage, 'chapters', 'chapters'); + } else if (this.fluidbook.datas.chapters.length > 0) { + this.addLink('nav-sommaire', '#/chapters', 'chapters', 'chapters'); + } + } + } else if (icon == 'friend') { + if (this.fluidbook.datas.share) { + this.addLink('nav-friend', '#/share', 'share', 'share'); + $("#share").click(function () { + if ($this.fluidbook.datas.phonegap != 'android') { + return true; + } + $this.fluidbook.intentShare(); + return false; + }); + } + } else if (icon == 'bookmark') { + if (this.fluidbook.datas.bookmark) { + this.addLink('nav-bookmark', '#/bookmark', 'bookmarks', 'bookmarks'); + $("#bookmarks").click(function () { + if (!$this.fluidbook.bookmarks.hasBookmarkedPages()) { + var message = $this.fluidbook.l10n.__("you don't have any bookmarks"); + if ($this.fluidbook.datas.phonegap) { + navigator.notification.alert(message, function () { + }, $this.fluidbook.l10n.__('bookmarks')); + } else { + window.alert(message); + } + return false; + } + }); + } + } else if (icon == 'pdf' || icon == 'print') { + if ($("#print").length == 0 && (this.fluidbook.datas.print || this.fluidbook.datas.pdf)) { + this.addLink('nav-print', '#', 'print', '!' + this.fluidbook.l10n.__('print') + ' | ' + this.fluidbook.l10n.__('download pdf')); + $("#print").on('click', function () { + $this.fluidbook.print(); + return false; + }); + } + } else if (icon == 'lang') { + if (this.fluidbook.l10n.multilangEnabled) { + this.addMultilangLink(); + } + } else if (icon == 'archives') { + if (this.fluidbook.datas.archivesLink != '') { + this.addLink('nav-archives', this.fluidbook.datas.archivesLink, 'archives', '!' + this.fluidbook.datas.archivesLabel); + } else if (this.fluidbook.datas.externalArchives != '') { + this.addLink('nav-archives', '#/archives', 'archives', '!' + this.fluidbook.datas.archivesLabel); + } + } else if (icon == 'help') { + this.addLink('nav-help', '#', 'help', ''); + } else if (icon == 'zoom' && !this.fluidbook.support.isMobile) { + this.addLink('nav-zoomin', '#', 'zoomin', 'zoom in'); + this.addLink('nav-zoomout', '#', 'zoomout', 'zoom out'); - if (this.fluidbook.datas.search) { - this.setSearch(); - } - if (this.fluidbook.datas.afterSearch != '') { - this.setAfterSearch(); - } - this.setInterface(); - }, - addMultilangLink: function(langs) { - var l = ''; - var flag = this.fluidbook.l10n.getActiveFlag(); - if (flag === false) { - return; - } + $("#zoomin").click(function (e) { + $this.fluidbook.desktop.clickZoom(e, 'in'); + return false; + }); - $("#nav").append(l); - $("#nav #locales").css('background-image', 'url("images/flags/' + flag + '.png")'); - }, - setSearch: function() { - var $this = this; - var res = '
'; - res += ''; - res += this.getLink('interface-search', '#', 'submitSearch'); - res += '
'; - $("#search").append(res); - $("#submitSearch").click(function() { - $("#searchForm").submit(); - return false; - }) + $("#zoomout").click(function () { + $this.fluidbook.desktop.clickZoom(e, 'out'); + return false; + }); + } else if (icon == 'fullscreen' && Modernizr.fullscreen) { + this.addLink('nav-fullscreen', '#', 'fullscreen', 'switch between fullscreen and normal'); + $("#fullscreen").click(function () { + screenfull.toggle(); + return false; + }) + } + } - // Search form - $("#searchForm").submit(function() { - var q = $("#q").val(); - if (q == '') { - return false; - } - window.location.hash = '/search/' + q; - return false; - }); - $("#q").keyup(searchHints); - $(document).on('click', ".hint", function() { - var e = $("#q").val().split(' '); - e.pop(); - e.push($(this).attr('term')); - $("#q").val(e.join(' ')); - $("#searchForm").submit(); - return false; - }) + if (this.fluidbook.datas.search) { + this.setSearch(); + } + if (this.fluidbook.datas.afterSearch != '') { + this.setAfterSearch(); + } + this.setInterface(); + }, + addMultilangLink: function (langs) { + var l = ''; + var flag = this.fluidbook.l10n.getActiveFlag(); + if (flag === false) { + return; + } - $("#q").blur(function(e) { - setTimeout(function() { - $this.fluidbook.hideSearchHints(); - }, 250); - }); - $("#nav").append($("#search")); - }, - setAfterSearch: function() { - $("#nav").append('
' + this.fluidbook.loader.getImage('data/images/' + this.fluidbook.datas.afterSearch) + '
'); - }, - setInterface: function() { + $("#nav").append(l); + $("#nav #locales").css('background-image', 'url("images/flags/' + flag + '.png")'); + }, + setSearch: function () { + var $this = this; + var res = '
'; + res += ''; + res += this.getLink('interface-search', '#', 'submitSearch'); + res += '
'; + $("#search").append(res); + $("#submitSearch").click(function () { + $("#searchForm").submit(); + return false; + }) - var res = this.getLink('interface-previous', '#', 'previous', '', '', false); - res += this.getLink('interface-next', '#', 'next', '', '', false); - $('#interface').append(res); - $(document).on('click', '#next', goNextPage); - $(document).on('click', '#previous', goPreviousPage); - } + // Search form + $("#searchForm").submit(function () { + var q = $("#q").val(); + if (q == '') { + return false; + } + window.location.hash = '/search/' + q; + return false; + }); + $("#q").keyup(searchHints); + $(document).on('click', ".hint", function () { + var e = $("#q").val().split(' '); + e.pop(); + e.push($(this).attr('term')); + $("#q").val(e.join(' ')); + $("#searchForm").submit(); + return false; + }) + + $("#q").blur(function (e) { + setTimeout(function () { + $this.fluidbook.hideSearchHints(); + }, 250); + }); + $("#nav").append($("#search")); + }, + setAfterSearch: function () { + $("#nav").append('
' + this.fluidbook.loader.getImage('data/images/' + this.fluidbook.datas.afterSearch) + '
'); + }, + setInterface: function () { + + var res = this.getLink('interface-previous', '#', 'previous', '', '', false); + res += this.getLink('interface-next', '#', 'next', '', '', false); + $('#interface').append(res); + $(document).on('click', '#next', goNextPage); + $(document).on('click', '#previous', goPreviousPage); + } }; \ No newline at end of file