Browse Source

[search] Introduced fromnow function in templates

Enrico Berti 12 years ago
parent
commit
5672a53

+ 13 - 0
apps/search/src/search/templates/admin_core_template.mako

@@ -180,6 +180,7 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
                   <option title="${ _('Formats a date in the DD-MM-YYYY HH:mm:ss format') }" 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="${ _('Shows the relative time') }" value="{{#fromnow}} {{/fromnow}}">{{#fromnow}}</option>
                   <option title="${ _('Downloads the linked file') }" value="{{#downloadfile}} {{/downloadfile}}">{{#downloadfile}}</option>
                   <option title="${ _('Links to the file') }" value="{{#viewfile}} {{/viewfile}}">{{#viewfile}}</option>
                 </select>
@@ -229,6 +230,7 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
                   <option title="${ _('Formats a date in the DD-MM-YYYY HH:mm:ss format') }" 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="${ _('Shows the relative time') }" value="{{#fromnow}} {{/fromnow}}">{{#fromnow}}</option>
                   <option title="${ _('Downloads the linked file') }" value="{{#downloadfile}} {{/downloadfile}}">{{#downloadfile}}</option>
                   <option title="${ _('Links to the file') }" value="{{#viewfile}} {{/viewfile}}">{{#viewfile}}</option>
                 </select>
@@ -598,6 +600,17 @@ ${ commonheader(_('Search'), "search", user) | n,unicode }
               }
             }
           };
+          item.fromnow = function () {
+            return function (val) {
+              var d = moment(Mustache.render(val, item));
+              if (d.isValid()) {
+                return d.fromNow();
+              }
+              else {
+                return Mustache.render(val, item);
+              }
+            }
+          };
           $("<div>").addClass("preview-row").html(Mustache.render($("#content-editor").html(), item)).appendTo($("#preview-container"));
         });
       }

+ 11 - 0
apps/search/src/search/templates/search.mako

@@ -199,6 +199,17 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
               }
             }
           };
+          item.fromnow = function () {
+            return function (val) {
+              var d = moment(Mustache.render(val, item));
+              if (d.isValid()) {
+                return d.fromNow();
+              }
+              else {
+                return Mustache.render(val, item);
+              }
+            }
+          };
           $("<div>").addClass("result-row").html(
             Mustache.render($("#mustacheTmpl").text(), item)
           ).appendTo($("#result-container"));