浏览代码

[search] Make date functions smarter

Romain Rigaux 12 年之前
父节点
当前提交
ea4b10477d

+ 3 - 3
apps/search/src/search/templates/admin_collection_template.mako

@@ -187,9 +187,9 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
               </div>
               <div class="widget-content">
                 <select id="visualFunctions" data-bind="value: selectedVisualFunction" class="input-large chzn-select">
-                  <option title="${ _('Formats a date in the DD-MM-YYYY format') }" value="{{#date}} {{/date}}">{{#date}}</option>
-                  <option title="${ _('Formats a date in the HH:mm:ss format') }" value="{{#time}} {{/time}}">{{#time}}</option>
-                  <option title="${ _('Formats a date in the DD-MM-YYYY HH:mm:ss format') }" value="{{#datetime}} {{/datetime}}">{{#datetime}}</option>
+                  <option title="${ _('Formats date or timestamp in DD-MM-YYYY') }" value="{{#date}} {{/date}}">{{#date}}</option>
+                  <option title="${ _('Formats date or timestamp in HH:mm:ss') }" value="{{#time}} {{/time}}">{{#time}}</option>
+                  <option title="${ _('Formats date or timestamp in DD-MM-YYYY HH:mm:ss') }" value="{{#datetime}} {{/datetime}}">{{#datetime}}</option>
                   <option title="${ _('Formats a date in the full format') }" value="{{#fulldate}} {{/fulldate}}">{{#fulldate}}</option>
                   <option title="${ _('Formats a date as a Unix timestamp') }" value="{{#timestamp}} {{/timestamp}}">{{#timestamp}}</option>
                   <option title="${ _('Formats a Unix timestamp as Ns, Nmin, Ndays... ago') }" value="{{#fromnow}} {{/fromnow}}">{{#fromnow}}</option>

+ 22 - 3
apps/search/static/js/search.utils.js

@@ -18,9 +18,26 @@ function addTemplateFunctions(item) {
   if (Mustache == "undefined") {
     return;
   }
-  function genericFormatDate(val, item, format) {
+
+  function genericDate(val, item) {
     var d = moment(Mustache.render(val, item));
     if (d.isValid()) {
+      return d;
+    }
+    else {
+      var number = parseInt(Mustache.render(val, item)) || '';
+      if (number) {
+        var d = moment(number * 1000); // timestamp * 1000
+        if (d.isValid()) {
+          return d;
+        }
+      }
+    }
+  }
+
+  function genericFormatDate(val, item, format) {
+    var d = genericDate(val, item);
+    if (d) {
       return d.format(format);
     }
     else {
@@ -28,6 +45,8 @@ function addTemplateFunctions(item) {
     }
   }
 
+  // Functions
+
   item.preview = function () {
     return function (val) {
       return '<a href="/filebrowser/view/' + $.trim(Mustache.render(val, item)) + '">' + $.trim(Mustache.render(val, item)) + '</a>';
@@ -76,8 +95,8 @@ function addTemplateFunctions(item) {
   };
   item.fromnow = function () {
     return function (val) {
-      var d = moment(Mustache.render(val, item));
-      if (d.isValid()) {
+      var d = genericDate(val, item);
+      if (d && d.isValid()) {
         return d.fromNow();
       }
       else {