]> _ Git - fluidbook-html5.git/commitdiff
wip #1926 @6
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 11 Apr 2018 17:17:13 +0000 (19:17 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 11 Apr 2018 17:17:13 +0000 (19:17 +0200)
js/libs/cube/util.js
js/libs/fluidbook/cart/fluidbook.cart.remarkable.js [new file with mode: 0644]
js/libs/fluidbook/fluidbook.cart.js [new file with mode: 0644]
js/libs/fluidbook/fluidbook.js
js/libs/fluidbook/fluidbook.nav.js
style/fluidbook.less

index 93f3c1227495461b9db2a6dc8605e9af3f5a71c6..b99dabed74874dcbb0f3c74ccfba2e8787bcb5af 100644 (file)
@@ -145,3 +145,12 @@ function getSVGDocument(element){
         return subdoc;
     }
 }
+
+Object.size = function(obj) {
+    var size = 0, key;
+    for (key in obj) {
+        if (obj.hasOwnProperty(key)) size++;
+    }
+    return size;
+};
+
diff --git a/js/libs/fluidbook/cart/fluidbook.cart.remarkable.js b/js/libs/fluidbook/cart/fluidbook.cart.remarkable.js
new file mode 100644 (file)
index 0000000..8142a78
--- /dev/null
@@ -0,0 +1,49 @@
+function FluidbookCartRemarkable(cart) {
+    this.cart = cart;
+    this.fluidbook = this.cart.fluidbook;
+    this.items = {};
+    this.init();
+}
+
+FluidbookCartRemarkable.prototype = {
+    init: function () {
+
+    },
+    addToCart: function (ref, quantity) {
+        this.setQuantity(ref, quantity, 'add');
+    },
+    removeFromCart: function (ref) {
+        this.setQuantity(ref, 0, 'set');
+    },
+    setQuantity: function (ref, quantity, operator) {
+        if (operator === undefined) {
+            operator = 'set';
+        }
+        if (quantity === undefined) {
+            quantity = 1;
+        }
+        if (this.items[ref] == null) {
+            this.items[ref] = 0;
+        }
+        var q = this.items[ref];
+        if (operator == 'set') {
+            q = quantity;
+        } else if (operator == 'add') {
+            q += quantity;
+        }
+
+        if (q <= 0) {
+            delete this.items[ref];
+        } else {
+            this.items[ref] = q;
+        }
+        this.updateIcon();
+    },
+    getItemsNumbers: function () {
+        return Object.size(this.items);
+    },
+    updateIcon: function () {
+        $(this.fluidbook).trigger('fluidbook.cart.updateIcon', {number: this.getItemsNumbers()});
+    },
+
+};
\ No newline at end of file
diff --git a/js/libs/fluidbook/fluidbook.cart.js b/js/libs/fluidbook/fluidbook.cart.js
new file mode 100644 (file)
index 0000000..22b4318
--- /dev/null
@@ -0,0 +1,30 @@
+function FluidbookCart(fluidbook) {
+    this.fluidbook = fluidbook;
+    this.enabled = false;
+    this.instance = null;
+    this.init();
+}
+
+FluidbookCart.prototype = {
+    init: function () {
+        if (!this.fluidbook.datas.basket) {
+            return false;
+        }
+        var $this = this;
+        this.enabled = true;
+        this.instance = this.createInstance();
+
+        $(document).on('click', '[data-cart-ref]', function () {
+            $this.instance.addToCart($(this).data('cart-ref'));
+            return false;
+        });
+    },
+    createInstance: function () {
+        switch (this.fluidbook.datas.basketManager) {
+            case "Remarkable":
+                return new FluidbookCartRemarkable(this);
+            default:
+                return null;
+        }
+    },
+}
\ No newline at end of file
index 1fad66fa34222a544c7b3db321abb5f85957b25d..47031ea056f1980c71b2ee2ab4b54876b0083657 100644 (file)
@@ -53,6 +53,7 @@ Fluidbook.prototype = {
         this.tooltip = new FluidbookTooltip(this);
         this.audiodescription = new FluidbookAudioDescription(this);
         this.sound = new FluidbookSound(this);
+        this.cart =new FluidbookCart(this);
 
         if (this.datas.form == 'bulle') {
             this.form = new FluidbookBulleForm(this);
@@ -88,6 +89,7 @@ Fluidbook.prototype = {
         this.initTheme();
         this.initKeyboardShortcuts();
     },
+
     initTheme: function () {
         if (this.datas.arrowsTheme) {
             $('html').addClass('sharp');
index 02f245e5316a84bd59dc850c3af7c0b1622a200c..68a873fd10c4fb0824212d81a11c31248d7a908a 100644 (file)
@@ -322,7 +322,6 @@ FluidbookNav.prototype = {
             var link2 = null;
 
             if (icon == 'home' && !skipHome) {
-
                 var homeURL = this.fluidbook.datas.home;
                 if (this.fluidbook.datas.home == '%apphome%' || forceHome && DATAS.phonegap) {
                     homeURL = decodeURIComponent(window.localStorage.getItem('apphome'));
@@ -393,6 +392,22 @@ FluidbookNav.prototype = {
                 link = this.addLink(navType, 'nav-download', '#', 'download', 'download', 'download');
             } else if (icon == 'print' && this.fluidbook.datas.print) {
                 link = this.addLink(navType, 'nav-print', '#', 'print', 'print', 'print');
+            } else if (icon == 'basket' && this.fluidbook.cart.enabled) {
+                link = this.addLink(navType, 'nav-basket', '#', 'cart', 'basket', 'basket');
+                $(this.fluidbook).on('fluidbook.cart.updateIcon', {link: link.attr('id')}, function (e, data) {
+                    var n = data.number;
+                    var l = $("#" + e.data.link);
+
+                    if (n === 0) {
+                        $(l).find('span.number').remove();
+                    } else {
+                        if ($(l).find('span.number').length === 0) {
+                            $(l).append('<span class="number"></span>');
+                        }
+                        console.log($(l).find('span.number'));
+                        $(l).find('span.number').text(n);
+                    }
+                });
             } else if (icon == 'lang' && this.fluidbook.l10n.multilangEnabled) {
                 // Note: the "!" at the beginning of the title/help parameters means that we don't want these strings translated
                 link = this.addLink(navType, 'nav-locales', '#/locales', 'locales', '!' + this.fluidbook.l10n.getCurrentLanguageName(), '!Select Language');
index 942e04d8b62012cdeb8127778ddc9046d1c82056..c4d2a9ba3eed8de53297a2fc3a79563f75d20a20 100644 (file)
@@ -707,6 +707,10 @@ header {
 #nav {
        position: relative;
        white-space: nowrap;
+
+       #locales {
+               background-color: @icon-color;
+       }
 }
 
 input[type="search"]::-webkit-search-decoration,
@@ -841,6 +845,20 @@ a.bookmark {
        }
 }
 
+/* Cart */
+.icon-cart {
+       position: relative;
+       span.number {
+               position: absolute;
+               top: 0.7em;
+               left: 0;
+               color: @icon-color;
+               text-align: center;
+               width: 100%;
+               font-size: 0.7em;
+       }
+}
+
 // Hack for #1433
 html.ios body.portrait #interface {
        -moz-transition: none;