From 7cee5b3b60381a96c7250c8a18b649d374d08708 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 4 Oct 2006 00:47:55 +0000 Subject: [PATCH] Fixes #333, Access date needs special handling in item pane for webpage item type - Currently requires user to enter dates in SQL format if they want to change the access date, but at least it doesn't mangle the dates anymore - Uses new function ScholardammitZotero.Date.dateToSQL(Date date [, Boolean toUTC]) - Utilities.lpad() now forces _string_ to a string so that .length exists - Unrelated: Item.save() now returns false if the item didn't change --- chrome/content/zotero/itemPane.js | 39 +++++++++++++------ chrome/content/zotero/xpcom/data_access.js | 2 +- chrome/content/zotero/xpcom/utilities.js | 1 + chrome/content/zotero/xpcom/zotero.js | 45 ++++++++++++++++++++++ 4 files changed, 75 insertions(+), 12 deletions(-) diff --git a/chrome/content/zotero/itemPane.js b/chrome/content/zotero/itemPane.js index fe3d48e72..fce559d9c 100644 --- a/chrome/content/zotero/itemPane.js +++ b/chrome/content/zotero/itemPane.js @@ -207,13 +207,6 @@ var ZoteroItemPane = new function() var val = _itemBeingEdited.getField(fieldNames[i]); - // Convert dates from UTC - if (fieldNames[i]=='dateAdded' || fieldNames[i]=='dateModified' - || fieldNames[i]=='accessDate'){ - var date = Zotero.Date.sqlToDate(val, true); - val = date ? date.toLocaleString() : ''; - } - // Start tabindex at 1000 after creators var tabindex = editable ? (i>0 ? _tabIndexMinFields + i : 1) : 0; @@ -652,9 +645,19 @@ var ZoteroItemPane = new function() valueElement.setAttribute('onclick', 'ZoteroItemPane.showEditor(this)'); valueElement.className = 'clicky'; - if (fieldName=='tag') + switch (fieldName) { - _tabIndexMaxTagsFields = Math.max(_tabIndexMaxTagsFields, tabindex); + case 'tag': + _tabIndexMaxTagsFields = Math.max(_tabIndexMaxTagsFields, tabindex); + break; + + // Convert dates from UTC + case 'dateAdded': + case 'dateModified': + case 'accessDate': + var date = Zotero.Date.sqlToDate(valueText, true); + valueText = date ? date.toLocaleString() : ''; + break; } } @@ -730,6 +733,13 @@ var ZoteroItemPane = new function() { var value = _itemBeingEdited.getField(fieldName); var itemID = _itemBeingEdited.getID(); + + // Access date needs to be converted from UTC + if (fieldName=='accessDate') + { + var localDate = Zotero.Date.sqlToDate(value, true); + var value = Zotero.Date.dateToSQL(localDate); + } } var t = document.createElement("textbox"); @@ -988,9 +998,16 @@ var ZoteroItemPane = new function() // Fields else { + // Access date needs to be converted to UTC + if (fieldName=='accessDate') + { + var localDate = Zotero.Date.sqlToDate(value); + var value = Zotero.Date.dateToSQL(localDate, true); + } + if(saveChanges) modifyField(fieldName,value); - + elem = createValueElement(_itemBeingEdited.getField(fieldName), fieldName, tabindex); } @@ -1011,7 +1028,7 @@ var ZoteroItemPane = new function() function modifyField(field, value) { _itemBeingEdited.setField(field,value); - _itemBeingEdited.save(); + return _itemBeingEdited.save(); } function _getFieldValue(field) diff --git a/chrome/content/zotero/xpcom/data_access.js b/chrome/content/zotero/xpcom/data_access.js index d6f5adf87..e6dd914fb 100644 --- a/chrome/content/zotero/xpcom/data_access.js +++ b/chrome/content/zotero/xpcom/data_access.js @@ -421,7 +421,7 @@ Zotero.Item.prototype.setField = function(field, value, loadIn){ Zotero.Item.prototype.save = function(){ if (!this.hasChanged()){ Zotero.debug('Item ' + this.getID() + ' has not changed', 4); - return !!this.getID(); + return false; } // diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js index 835975f79..d5f2e98d4 100644 --- a/chrome/content/zotero/xpcom/utilities.js +++ b/chrome/content/zotero/xpcom/utilities.js @@ -134,6 +134,7 @@ Zotero.Utilities.prototype.inArray = Zotero.inArray; * pads a number or other string with a given string on the left */ Zotero.Utilities.prototype.lpad = function(string, pad, length) { + string = string + ''; while(string.length < length) { string = pad + string; } diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index b039c20f8..479a5c0e0 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -632,6 +632,7 @@ Zotero.Hash.prototype.has = function(in_key){ Zotero.Date = new function(){ this.sqlToDate = sqlToDate; + this.dateToSQL = dateToSQL; this.strToDate = strToDate; this.formatDate = formatDate; this.getFileDateString = getFileDateString; @@ -672,6 +673,50 @@ Zotero.Date = new function(){ } } + + /** + * Convert a JS Date object to an SQL date in the form '2006-06-13 11:03:05' + * + * If _toUTC_ is true, creates a UTC date + **/ + function dateToSQL(date, toUTC) + { + try { + if (toUTC){ + var year = date.getUTCFullYear(); + var month = date.getUTCMonth(); + var day = date.getUTCDate(); + var hours = date.getUTCHours(); + var minutes = date.getUTCMinutes(); + var seconds = date.getUTCSeconds(); + } + else { + var year = date.getFullYear(); + var month = date.getMonth(); + var day = date.getDate(); + var hours = date.getHours(); + var minutes = date.getMinutes(); + var seconds = date.getSeconds(); + } + + var utils = new Zotero.Utilities(); + year = utils.lpad(year, '0', 4); + month = utils.lpad(month + 1, '0', 2); + day = utils.lpad(day, '0', 2); + hours = utils.lpad(hours, '0', 2); + minutes = utils.lpad(minutes, '0', 2); + seconds = utils.lpad(seconds, '0', 2); + + return year + '-' + month + '-' + day + ' ' + + hours + ':' + minutes + ':' + seconds; + } + catch (e){ + Zotero.debug(date + ' is not a valid JS date', 2); + return ''; + } + } + + /* * converts a string to an object containing: * day: integer form of the day