From fe319f033b888c4f0b53d4d7bb8e32630fc9a393 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 13 Sep 2006 22:04:54 +0000 Subject: [PATCH] Schema and Item Type Manager updates to handle item type templates Note that there's no code for user types and fields yet -- just the schema (actually there's a tiny bit of code in the item type manager, since we'll probably use some of the same methods for managing user types, but not much) Templates for primary item types are currently only used by the item type manager to make creating new types easier and to prevent the removal of fields from an item type that are associated with its template item type -- the fields are all still recorded in itemTypeFields, since they might have different orders or default visibility settings from their templates --- .../content/scholar/admin/itemTypeManager.css | 4 + .../content/scholar/admin/itemTypeManager.js | 167 +++++++++++++++--- .../content/scholar/admin/itemTypeManager.xul | 25 ++- system.sql | 35 ++-- user.sql | 35 +++- 5 files changed, 218 insertions(+), 48 deletions(-) diff --git a/chrome/chromeFiles/content/scholar/admin/itemTypeManager.css b/chrome/chromeFiles/content/scholar/admin/itemTypeManager.css index 380b4a4b2..3647405fd 100644 --- a/chrome/chromeFiles/content/scholar/admin/itemTypeManager.css +++ b/chrome/chromeFiles/content/scholar/admin/itemTypeManager.css @@ -1,3 +1,7 @@ +listitem[isTemplateField=true] { + color:brown; +} + listitem[isHidden=true] { color:lightblue; } diff --git a/chrome/chromeFiles/content/scholar/admin/itemTypeManager.js b/chrome/chromeFiles/content/scholar/admin/itemTypeManager.js index 96bb0560b..8f27ccad3 100644 --- a/chrome/chromeFiles/content/scholar/admin/itemTypeManager.js +++ b/chrome/chromeFiles/content/scholar/admin/itemTypeManager.js @@ -1,6 +1,8 @@ var Scholar_ItemTypeManager = new function(){ this.init = init; this.handleTypeSelect = handleTypeSelect; + this.buildTypeContextMenu = buildTypeContextMenu; + this.setTemplate = setTemplate; this.addFieldToType = addFieldToType; this.removeFieldFromType = removeFieldFromType; this.handleShowHide = handleShowHide; @@ -13,12 +15,14 @@ var Scholar_ItemTypeManager = new function(){ this.createSQLDump = createSQLDump; var _typesList; + var _typeTemplates; + var _typeFieldsList; var _fieldsList; - function init(){ // Populate the listbox with item types _typesList = document.getElementById('item-type-list'); + _typeTemplates = document.getElementById('scholar-type-template-menu'); _typeFieldsList = document.getElementById('item-type-fields-list'); _fieldsList = document.getElementById('fields-list'); @@ -44,6 +48,19 @@ var Scholar_ItemTypeManager = new function(){ } } + // Update the context menu + while (_typeTemplates.childNodes[2]){ + _typeTemplates.removeChild(_typeTemplates.childNodes[2]); + } + for (var i in types){ + var item = document.createElement('menuitem'); + item.setAttribute('label', types[i]['name']); + item.setAttribute('value', types[i]['id']); + item.setAttribute('type', 'checkbox'); + item.setAttribute('oncommand', "Scholar_ItemTypeManager.setTemplate(document.popupNode.value, this.value)"); + _typeTemplates.appendChild(item); + } + if (_typesList.selectedIndex==-1){ _typesList.selectedIndex=0; } @@ -64,6 +81,87 @@ var Scholar_ItemTypeManager = new function(){ _populateFieldsList(id); } + function buildTypeContextMenu(){ + var id = _typesList.selectedItem.value; + var template = _getItemTypeTemplate(id); + + if (!template){ + _typeTemplates.childNodes[0].setAttribute('checked', true); + } + else { + _typeTemplates.childNodes[0].setAttribute('checked', false); + } + + for (var i=2, len=_typeTemplates.childNodes.length; i=1000 ? 'userItemTypes' : 'itemTypes'; + var sql = "SELECT templateItemTypeID FROM " + table + sql += " WHERE itemTypeID=?"; + return Scholar.DB.valueQuery(sql, itemTypeID); + } + /** * Add a field to an item type @@ -74,17 +172,7 @@ var Scholar_ItemTypeManager = new function(){ Scholar.debug('Adding field ' + item.value + ' to item type ' + _getCurrentTypeID()); - Scholar.DB.beginTransaction(); - - // Get the next available position - var sql = "SELECT IFNULL(MAX(orderIndex)+1,1) FROM itemTypeFields " - + "WHERE itemTypeID=?"; - var nextIndex = Scholar.DB.valueQuery(sql, [_getCurrentTypeID()]); - - var sql = "INSERT INTO itemTypeFields VALUES (?,?,?,?)"; - Scholar.DB.query(sql, [_getCurrentTypeID(), item.value, null, nextIndex]); - - Scholar.DB.commitTransaction(); + _DBMapField(_getCurrentTypeID(), item.value); var pos = _fieldsList.getIndexOfItem(item); _fieldsList.removeItemAt(pos); @@ -162,7 +250,6 @@ var Scholar_ItemTypeManager = new function(){ return _handleAddEvent(box, event, 'field'); } - function _handleAddEvent(box, event, target){ if (target=='type'){ var existsFunc = _typeExists; @@ -283,7 +370,10 @@ var Scholar_ItemTypeManager = new function(){ for (var i in types){ sql += prefix + "INSERT INTO itemTypes VALUES (" - + types[i]['itemTypeID'] + ",'" + types[i]['typeName'] + "');\n" + + types[i]['itemTypeID'] + ", '" + types[i]['typeName'] + "', " + + (types[i]['templateItemTypeID'] + ? types[i]['templateItemTypeID'] : 'NULL') + + ");\n" } sql += "\n"; @@ -329,9 +419,14 @@ var Scholar_ItemTypeManager = new function(){ * Populate the listbox of fields used by this item type **/ function _populateTypeFieldsList(itemTypeID){ - var sql = 'SELECT fieldID, hide FROM itemTypeFields ' - + 'WHERE itemTypeID=' + itemTypeID + ' ORDER BY orderIndex'; - var fields = Scholar.DB.query(sql); + // TODO: show template fields for user types + + var sql = "SELECT fieldID, hide, " + + "(fieldID IN (SELECT fieldID FROM itemTypeFields WHERE itemTypeID=" + + "(SELECT templateItemTypeID FROM itemTypes WHERE itemTypeID=?1))) " + + "AS isTemplateField FROM itemTypeFields ITF WHERE itemTypeID=?1 " + + "ORDER BY orderIndex"; + var fields = Scholar.DB.query(sql, itemTypeID); // Clear fields box while (_typeFieldsList.getRowCount()){ @@ -341,11 +436,21 @@ var Scholar_ItemTypeManager = new function(){ for (var i in fields){ var item = _typeFieldsList.appendItem(_getFieldName(fields[i]['fieldID']), fields[i]['fieldID']); - item.addEventListener('dblclick', new function(){ - return function(){ - Scholar_ItemTypeManager.removeFieldFromType(this); - } - }, true); + if (fields[i]['isTemplateField']){ + item.setAttribute('isTemplateField', true); + item.addEventListener('dblclick', new function(){ + return function(){ + alert('You cannot remove template fields.'); + } + }, true); + } + else { + item.addEventListener('dblclick', new function(){ + return function(){ + Scholar_ItemTypeManager.removeFieldFromType(this); + } + }, true); + } item.setAttribute('isHidden', !!fields[i]['hide']); } @@ -391,6 +496,24 @@ var Scholar_ItemTypeManager = new function(){ } + /* + * Map a field to an item type in the DB + */ + function _DBMapField(itemTypeID, fieldID){ + Scholar.DB.beginTransaction(); + + // Get the next available position + var sql = "SELECT IFNULL(MAX(orderIndex)+1,1) FROM itemTypeFields " + + "WHERE itemTypeID=?"; + var nextIndex = Scholar.DB.valueQuery(sql, itemTypeID); + + var sql = "INSERT INTO itemTypeFields VALUES (?,?,?,?)"; + Scholar.DB.query(sql, [itemTypeID, fieldID, null, nextIndex]); + + Scholar.DB.commitTransaction(); + } + + /* * Unmap a field from an item type in the DB */ diff --git a/chrome/chromeFiles/content/scholar/admin/itemTypeManager.xul b/chrome/chromeFiles/content/scholar/admin/itemTypeManager.xul index eb7139be8..4e1eeb3ba 100644 --- a/chrome/chromeFiles/content/scholar/admin/itemTypeManager.xul +++ b/chrome/chromeFiles/content/scholar/admin/itemTypeManager.xul @@ -15,18 +15,28 @@ - - + + + + + + + + + + - + - @@ -66,11 +76,10 @@ - Double-click a field to add it to or remove it from the currently selected item type. + Double-click a field to add it to or remove it from the currently selected item type. Hit Return on a mapped field to toggle its default visibility. - - - Hit Return on a mapped field to toggle its default visibility. (Light blue fields are hidden.) + + Light blue fields are hidden. Brown fields are template fields and can only be removed by changing the template.