# HG changeset patch
# Parent  1106caa859a8b94429b630e6f8f082967189301b
CardsPlugin: Allow selecting card colors.
(see #13248)

diff -r 1106caa859a8 cards/core.py
--- a/cards/core.py	Sun Dec 18 15:53:11 2016 +0100
+++ b/cards/core.py	Sun Jul 30 15:39:27 2017 +0200
@@ -18,7 +18,7 @@
 
 
 PLUGIN_NAME = 'CardsPlugin'
-PLUGIN_VERSION = 2
+PLUGIN_VERSION = 3
 
 
 def serialized_stacks_by_name(stacks, stack_names):
@@ -94,7 +94,8 @@
             int(req.args.get('id')),
             req.args.get('stack'),
             int(req.args.get('rank')),
-            req.args.get('title'))
+            req.args.get('title'),
+            req.args.get('color'))
 
     def _parse_stack(self, req, old=False):
         return CardStack(
diff -r 1106caa859a8 cards/htdocs/css/cards.css
--- a/cards/htdocs/css/cards.css	Sun Dec 18 15:53:11 2016 +0100
+++ b/cards/htdocs/css/cards.css	Sun Jul 30 15:39:27 2017 +0200
@@ -29,13 +29,13 @@
     overflow: auto; 
 }
 .trac-card-slot
- {
+{
     display: block;
     margin: 1px;
 }
 
 .trac-card
- {
+{
     display: block;
     
     margin: 5px;
diff -r 1106caa859a8 cards/htdocs/js/cards.js
--- a/cards/htdocs/js/cards.js	Sun Dec 18 15:53:11 2016 +0100
+++ b/cards/htdocs/js/cards.js	Sun Jul 30 15:39:27 2017 +0200
@@ -19,6 +19,7 @@
                 stack: card.stack,
                 rank: card.rank,
                 title: card.title,
+                color: card.color,
                 stack_version: board_data.stacks_by_name[card.stack].version,
                 old_stack_name: card.stack,
                 old_stack_version: board_data.stacks_by_name[card.stack].version
@@ -33,6 +34,7 @@
         var $card_slot = $("<div class='trac-card-slot'><div class='trac-card'></div></div>");
         var $card_element = $card_slot.find(".trac-card");
         $card_element.attr('id', 'trac-card-' + card.id);
+        $card_element.css('background', card.color);
         $card_element.html(card.title_html);
         $card_element.dblclick(showEditCardDialog);
         $stack_sorter.append($card_slot);
@@ -183,8 +185,9 @@
     });
     
     // Edit / delete
-    var editCardDialog = $("<div title='Edit card'><form><label for='title'>Title:</label><br /><textarea name='title' rows='5' cols='30'></textarea></form></div>");
+    var editCardDialog = $("<div title='Edit card'><form><label for='title'>Title:</label><br /><textarea name='title' rows='5' cols='30'></textarea><br /><label>Color: <input type='color' name='color' /></label></form></div>");
     var ctrlEditTitle = editCardDialog.find("textarea[name='title']");
+    var ctrlEditColor = editCardDialog.find("input[name='color']");
     function showEditCardDialog() {
     
         var board_data = get_board_data($(this));
@@ -194,6 +197,7 @@
         var card_id = get_card_id($card_element);
         var card = board_data.cards_by_id[card_id];
         ctrlEditTitle.val(card.title);
+        ctrlEditColor.val(card.color || '#ffffe0');
         editCardDialog.dialog({
             modal: true,
             dialogClass: "trac-cards-dialog",
@@ -201,12 +205,14 @@
                 "Edit": function() {
                     var post_data = serialized_post_data(card, 'update_card', board_data);
                     post_data.title = ctrlEditTitle.val();
+                    post_data.color = ctrlEditColor.val();
                     var current_dialog = $(this);
                     $.post(board_data.api_url, post_data, function(updated_card) {
                         card.title_html = updated_card.title_html;
                         card.title = updated_card.title;
                         $card_element.html(card.title_html);
-
+                        card.color = updated_card.color;
+                        $card_element.css('background', card.color);
                         current_dialog.dialog("close");
                     }).done(function() {
                         board_data.stacks_by_name[card.stack].version += 1;
@@ -251,6 +257,7 @@
                         stack: stack_id,
                         rank: -1, // Initialized server-side
                         title: ctrlCreateTitle.val(),                        
+                        color: '#fffffe0', // default: yellow
                     };
                     var post_data = serialized_post_data(proposed_card, 'add_card', board_data);
                     var current_dialog = $(this);
diff -r 1106caa859a8 cards/model.py
--- a/cards/model.py	Sun Dec 18 15:53:11 2016 +0100
+++ b/cards/model.py	Sun Jul 30 15:39:27 2017 +0200
@@ -9,6 +9,7 @@
         Column('stack'),
         Column('rank', type='int64'),
         Column('title'),
+        Column('color'),
         Index(['stack', 'rank']),
     ],
     Table('cards_stacks', key='name')[
@@ -20,11 +21,12 @@
 
 class Card(object):
 
-    def __init__(self, id, stack, rank, title):
+    def __init__(self, id, stack, rank, title, color):
         self.id = id
         self.stack = stack
         self.rank = rank
         self.title = title
+        self.color = color
 
     def serialized(self, env, context):
         return {
@@ -33,6 +35,7 @@
             'rank': self.rank,
             'title': self.title, 
             'title_html': format_to_html(env, context, self.title),
+            'color': self.color,
         }
 
     @classmethod
@@ -49,9 +52,9 @@
             card.rank = cursor.fetchone()[0]
             cursor.execute("""
             INSERT INTO cards
-                        (stack, rank, title)
-                 VALUES (%s, %s, %s)
-            """, (card.stack, card.rank, card.title))
+                        (stack, rank, title, color)
+                 VALUES (%s, %s, %s, %s)
+            """, (card.stack, card.rank, card.title, card.color))
             card.id = db.get_last_id(cursor, 'cards')
             return True
 
@@ -97,9 +100,9 @@
 
             cursor.execute("""
                 UPDATE cards
-                SET stack=%s, rank=%s, title=%s
+                SET stack=%s, rank=%s, title=%s, color=%s
                 WHERE id=%s
-            """, (card.stack, card.rank, card.title, card.id))
+            """, (card.stack, card.rank, card.title, card.color, card.id))
             return True
 
     @classmethod
@@ -128,12 +131,12 @@
     @classmethod
     def select_by_stack(cls, env, stack):
         rows = env.db_query("""
-                SELECT id, stack, rank, title
+                SELECT id, stack, rank, title, color
                 FROM cards
                 WHERE stack=%s
                 ORDER BY rank
                 """, (stack,))
-        return [Card(id, stack, rank, title) for id, stack, rank, title in rows]
+        return [Card(id, stack, rank, title, color) for id, stack, rank, title, color in rows]
 
     @classmethod
     def select_by_stacks(cls, env, stacks):
@@ -141,12 +144,12 @@
             return []
         stack_holder = ','.join(['%s'] * len(stacks))
         rows = env.db_query("""
-                SELECT id, stack, rank, title
+                SELECT id, stack, rank, title, color
                 FROM cards
                 WHERE stack in (%s)
                 ORDER BY stack, rank
                 """ % stack_holder, list(stacks))
-        return [Card(id, stack, rank, title) for id, stack, rank, title in rows]
+        return [Card(id, stack, rank, title, color) for id, stack, rank, title, color in rows]
 
 
 class CardStack(object):
diff -r 1106caa859a8 cards/upgrades/db3.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cards/upgrades/db3.py	Sun Jul 30 15:39:27 2017 +0200
@@ -0,0 +1,15 @@
+from trac.db import Table, Column, Index, DatabaseManager
+
+
+new_table = Table('cards', key='id')[
+    Column('id', auto_increment=True),
+    Column('stack'),
+    Column('rank', type='int64'),
+    Column('title'),
+    Column('color'),
+    Index(['stack', 'rank']),
+]
+
+
+def do_upgrade(env, ver, cursor):
+    DatabaseManager(env).upgrade_tables([new_table])
