Commits

Rhys ! committed 657a194

cart now uses cookies

Comments (0)

Files changed (7)

site/booklist_hit2316.shpaml

 {{py: global left_insert; left_insert = {'Booklists': bins()} }}
 {{def title}}HIT2316 Usability{{enddef}}
 {{def stylesheet}}booklists{{enddef}}
+{{def script}}cart{{enddef}}
 ul.breadcrumb
     li
         >
         a href=# | HIT2316 Usability
 h1 | HIT2316 Usability
 h2 | Semester Two, 2011, Hawthorn Campus
-form.table method=get action=cart.html
+form.table.cartadd method=get action=cart.html
     table
         tr
             th | Add
+window.cart_items = ->
+    cart = $.cookie 'cart'
+    if cart
+        return cart.split ';'
+    else
+        return []
+
+window.add_to_cart = (code) ->
+    cart = cart_items()
+    if code not in cart
+        cart.push code
+        $.cookie 'cart', cart.join ';'
+
+window.store_item = (code, props) ->
+    localStorage.setItem code, props.join ';'
+
+window.retr_item = (code) ->
+    return localStorage.getItem(code).split ';'
+
+window.remove_from_cart = (code) ->
+    cart = cart_items()
+    loc = cart.indexOf code
+    if loc >= 0
+        cart = cart.slice(0, loc).concat cart.slice loc + 1
+        $.cookie 'cart', cart.join ';'
+
+item_row = (code) ->
+    data = retr_item code
+    if data
+        row = $ '<tr><td><input type="text" name="item" /></td></tr>'
+        row.find('input').attr('id', code).attr 'value', data[0]
+        row.append $('<td />').text val for val in data.slice 1
+        return row
+    else
+        alert "cannot find #{code}"
+
+map_to_items = (form, func) ->
+    func item for item in $(form).find 'input[name="item"]:checked'
+    null
+
+store_item_from_row = (checkbox) ->
+    props = ($(td).text().trim() for td in $(checkbox).closest('td').siblings())
+    props.unshift 1
+    store_item checkbox.value, props
+
+$ ->
+    $('form.cartadd').submit ->
+        map_to_items this, store_item_from_row
+        map_to_items this, (item) -> add_to_cart item.value
+    $('table.cart').each ->
+        table = $(this)
+        if cart_items().length
+            table.append row for row in (item_row code for code in cart_items())
+            table.find('td:last-child').attr 'class', 'number'
+            table.find('td:nth-child(6)').attr 'class', 'number'
+        else
+            table.closest('form').replaceWith '<p>Your shopping cart is empty.</p>'
+            $('form.table').remove()
+    $('form.cartremove input[name="item"]').change ->
+        if /^[0-9]+$/.exec this.value
+            if this.value is '0'
+                remove_from_cart this.id
+                $(this).closest('tr').attr('class', 'grey')
+            else
+                props = retr_item this.id
+                props[0] = this.value
+                store_item this.id, props
+                add_to_cart this.id
+                $(this).closest('tr').attr('class', '')
+        else
+            alert 'Please enter a valid quantity.'
+            this.value = this.defaultValue
 {{inherit "wrapper.tmpl"}}
 {{py: global left_insert; left_insert = {} }}
 {{def title}}Shopping cart{{enddef}}
+{{def script}}cart{{enddef}}
 ul.breadcrumb
     li
         &gt;
         a href=cart.html | Shopping cart
 
 h1 | Shopping cart
-form.table method=get action=construction.html
-    table
+form.table.cartremove method=get action=purchase.html
+    p | Set the quantity of an item to 0 to remove it.
+    table.cart
         tr
-            th | Remove
+            th | Quantity
             th | Item
             th | Type
             th | Author/Supplier
             th | ISBN/Item No.
             th | In Stock
             th | Price A$
-        tr
-            td
-                > input type=checkbox name=item value=9780120884360
-            td
-                a href=construction.html | User Interface Design & Evaluation
-            td | Prescribed Text
-            td | Stone & Woodroffe
-            td.number | 9780120884360
-            td.number | 6
-            td.number | $112.95
-    > input.left type=submit value="Remove from cart"
-form.table method=get action=purchase.html
     > input type=submit value="Complete purchase"

site/compiled/jquery.cookie.js

+/**
+ * jQuery Cookie plugin
+ *
+ * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
+ * Dual licensed under the MIT and GPL licenses:
+ * http://www.opensource.org/licenses/mit-license.php
+ * http://www.gnu.org/licenses/gpl.html
+ *
+ */
+jQuery.cookie = function (key, value, options) {
+
+    // key and at least value given, set cookie...
+    if (arguments.length > 1 && String(value) !== "[object Object]") {
+        options = jQuery.extend({}, options);
+
+        if (value === null || value === undefined) {
+            options.expires = -1;
+        }
+
+        if (typeof options.expires === 'number') {
+            var days = options.expires, t = options.expires = new Date();
+            t.setDate(t.getDate() + days);
+        }
+
+        value = String(value);
+
+        return (document.cookie = [
+            encodeURIComponent(key), '=',
+            options.raw ? value : encodeURIComponent(value),
+            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
+            options.path ? '; path=' + options.path : '',
+            options.domain ? '; domain=' + options.domain : '',
+            options.secure ? '; secure' : ''
+        ].join(''));
+    }
+
+    // key and possibly options given, get cookie...
+    options = value || {};
+    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
+    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
+};
         &:hover
             background-color: #f9f7f3
             cursor: pointer
+    input[type="text"]
+        width: 2em
     input.left
         float: left
         position: relative
         top: 2px
+    .grey
+        color: #bbb
 
 #right
     margin-top: 1px

site/purchase.shpaml

 {{py: global left_insert; left_insert = {} }}
 {{def title}}Complete purchase{{enddef}}
 {{def stylesheet}}purchase{{enddef}}
+{{def script}}cart{{enddef}}
 ul.breadcrumb
     li
         &gt;
         a href=# | Complete purchase
 
 h1 | Complete purchase
-table
+table.cart
     tr
         th | Item
         th | Type
         th | ISBN/Item No.
         th | In Stock
         th | Price A$
-    tr
-        td
-            a href=construction.html | User Interface Design & Evaluation
-        td | Prescribed Text
-        td | Stone & Woodroffe
-        td.number | 9780120884360
-        td.number | 6
-        td.number | $112.95
 
 form.table#shipping method=get action=receipt.html
     table

site/wrapper.shpaml

       {{endif}}
         > link rel="shortcut icon" type=image/x-icon href=http://www.swinburne.edu.au/favicon.ico
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
+        <script src="jquery.cookie.js"></script>
         <script src="global.min.js"></script>
+      {{if self.get.script}}
+        <script src="{{self.script}}.js"></script>
+      {{endif}}
     body
         #midbar
             #contentbar