babludcv / trimpath

Automatically exported from code.google.com/p/trimpath
0 stars 0 forks source link

Need string truncate modifier #26

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Here's my recent modifier,  stolen from prototypejs and JST-ified,  dumped
here until
I get on the trimpath project.  The problem is mailto: links are finally
broken if
the amount of data you try to stuff in them gets too large,  had to truncate a
description parameter to 150 chars:

var detail_results_container_temp = { matter: results[0], status_history:
resultsStatusHistory };

var myModifiers = {
            truncate : function(str, length, truncation) {
                length = length || 30;
                truncation = (truncation==undefined) ? '...' : truncation;
                return str.length > length ?
                  str.slice(0, length - truncation.length) + truncation : str;
              }
        };

detail_results_container_temp._MODIFIERS = myModifiers;

var email_detail_body_stuff =
this.parsedEmailDetailBodyTemplate.process(detail_results_container_temp);

Original issue reported on code.google.com by kucerari...@gmail.com on 18 Jun 2009 at 4:09

GoogleCodeExporter commented 8 years ago
Here is recent update of this set of modifiers (used in enterprise ajax 
portlet):

        this.detailModifiers = {
                truncate : function(str, length, truncation) {
                    length = length || 30;
                    truncation = (truncation==undefined) ? '...' : truncation;
                    return str.length > length ?
                      str.slice(0, length - truncation.length) + truncation : str;
                  },
                ifempty : function(str, substitute) {
                        return (str==undefined||str==null||str==""||str=="null")?substitute:str;
                      },
                mtai_format_date : function( dateStr, fmt ) {
                      if (dateStr==undefined||dateStr=='') {
                          return '';
                      }
                      if (fmt==undefined||fmt=='') {
                          fmt = "M d, Y";
                      }
                      return Jel.Date.format(Jel.Date.parse(dateStr, "Y-m-d"), fmt);
                  }
        };

I have attached a modified JEL for this purpose (a stripped down version,  lots 
of
stuff removed,  bloat removed,  esp. the stuff that didn't work in IE,  nice 
little
library,  just a bit overreaching,  the original author has stopped support and 
the
library has gone missing).

Original comment by kucerari...@gmail.com on 8 Jul 2009 at 2:31

Attachments:

GoogleCodeExporter commented 8 years ago
I should mention that in the stripped down version attached above, the JEL 
dependency
on prototypejs has also been removed (the portal would not put prototypejs on
production front end).

Original comment by kucerari...@gmail.com on 8 Jul 2009 at 2:33

GoogleCodeExporter commented 8 years ago
Yet another revision...  users are picky.  They wanted to reorder the name 
field of
something to be firstname first instead of lastname, firstname.  Well I guess it
makes sense,  but don't want to pull over two more database fields in addition
wrestling with the data model and the database guy just for this change.

        this.detailModifiers = {
                truncate : function(str, length, truncation) {
                    length = length || 30;
                    truncation = (truncation==undefined) ? '...' : truncation;
                    return str.length > length ?
                      str.slice(0, length - truncation.length) + truncation : str;
                  },
                ifempty : function(str, substitute) {
                        return (str==undefined||str==null||str==""||str=="null")?substitute:str;
                      },
                splitswap : function(str, splitchar) {
                          if (str==undefined||str==null||str==""||str=="null") {
                              return str;
                          } else {
                              splitchar = (splitchar==undefined||splitchar==null)?',':splitchar;
                              var halves = str.split( splitchar );
                              return Jel.String.trim(halves[1])+" "+Jel.String.trim(halves[0]);
                          }
                     },
                mtai_format_date : function( dateStr, fmt ) {
                      if (dateStr==undefined||dateStr=='') {
                          return '';
                      }
                      if (fmt==undefined||fmt=='') {
                          fmt = "M d, Y";
                      }
                      return Jel.Date.format(Jel.Date.parse(dateStr, "Y-m-d"), fmt);
                  }
        };

Once again JEL comes in handy,  it is attached to this issue.

Original comment by kucerari...@gmail.com on 8 Jul 2009 at 4:47

GoogleCodeExporter commented 8 years ago
Added prefix,  to solve ever-present problem of you don't want a label if you 
don't
have a value:

        this.detailModifiers = {
                truncate : function(str, length, truncation) {
                    length = length || 30;
                    truncation = (truncation==undefined) ? '...' : truncation;
                    return str.length > length ?
                      str.slice(0, length - truncation.length) + truncation : str;
                  },
                ifempty : function(str, substitute) {
                        return (str==undefined||str==null||str==""||str=="null")?substitute:str;
                      },
                prefix : function(str, prefix) {
                            return (str==undefined||str==null||str==""||str=="null")?"":prefix+str;
                },
                splitswap : function(str, splitchar) {
                          if (str==undefined||str==null||str==""||str=="null") {
                              return str;
                          } else {
                              splitchar = (splitchar==undefined||splitchar==null)?',':splitchar;
                              var halves = str.split( splitchar );
                              return Jel.String.trim(halves[1])+" "+Jel.String.trim(halves[0]);
                          }
                     },
                format_date : function( dateStr, fmt ) {
                      if (dateStr==undefined||dateStr=='') {
                          return '';
                      }
                      if (fmt==undefined||fmt=='') {
                          fmt = "M d, Y";
                      }
                      return Jel.Date.format(Jel.Date.parse(dateStr, "Y-m-d"), fmt);
                  }
        };

Original comment by kucerari...@gmail.com on 5 Oct 2009 at 2:10

GoogleCodeExporter commented 8 years ago
Splitswap had a bug,  added code to just return the string if the splitchar is 
not found:

splitswap : function(str, splitchar) {
          if (str==undefined||str==null||str==""||str=="null") {
              return str;
          } else {
              splitchar = (splitchar==undefined||splitchar==null)?',':splitchar;
              if ( str.indexOf( splitchar ) != -1 ) {
                  var halves = str.split( splitchar );
                  return Jel.String.trim(halves[1])+" "+Jel.String.trim(halves[0]);
              } else {
                  return str;
              }
          }
     },

Original comment by kucerari...@gmail.com on 23 Oct 2009 at 8:25