Marketcircle    Forums  Hop To Forum Categories  Extending Daylite  Hop To Forums  Creating HUD Widgets    Possible to search a field for a sub-string?
Go
New
Find
Notify
Tools
Reply
  
-star Rating Rate It!  Login/Join 
Pro
Picture of Andy Warwick
Posted
I'm trying to tweak the HUD Notes Widget I've put in the Share Zone, so that it will report back on any notes that have the string ‘HUD’ anywhere in the title, rather than having that exact title.

I thought I might approach it using some Javascript in the template, thus:

	<$foreach note model.notesSortedByCreateDateDescending do$>
		
	<$identify hasHUDSubstring = '<script type="text/javascript">
	<!-- 
	var title = "<$note.title$>" ;
	if ( title.indexOf("HUD") != -1 ) { document.write("1") } else { document.write("0") }
	//-->
	</script>'$>
		
	<!-- If the Note contains the string 'HUD' display it -->
		
	<$if hasHUDSubstring == '1'$>


So if the <$note.title$> has the string ‘HUD’, <$identify hasHUDSubstring $> gets set to one via the document.write, and the next chunk of code executes.

Unfortunately, despite appearing to document.write the correct value out in each case, the code doesn't seem to work; presumably because the Javascript is executed after the merge ‘if’ command has already taken effect?

Is it possible to search a field for a substring and act on that using the merge syntax?


--
Andy Warwick
www.creed.co.uk
 
Posts: 216 | Location: Nottingham, UK | Registered: June 22, 2006Reply With QuoteEdit or Delete MessageReport This Post
Marketcircle Team
Picture of Ali Lalani
Posted Hide Post
Andy, you are correct, the merge takes place before the page is even loaded. Basically how it works is we read in the textual information that makes up your template(at this point it doesn't matter if it's html, rtf, txt, etc), merge out the data for the keys we find in it, and then, in the case of the HUD, we pass it to the WebKit stuff to load the text as an html page.

So integrating javascript with merge results is not possible if you depend on javascript during the merge.

Also there is no way that i know of to do a substring search using the merge stuff.

This is somewhat more complicated but perhaps during the merge you could dynamically create some javascript that assigns the titles and subjects to variables and then in the body tag "onLoad" you could execute a function to search through the array of these values to find ones with subject that contains HUD?
(my javascript/css/dhmtl skills are not great so i'm not sure exactly how you'd achieve this, but i'm sure it is possible...)

Hope that helps a bit!
 
Posts: 108 | Registered: June 15, 2006Reply With QuoteEdit or Delete MessageReport This Post
Pro
Picture of Andy Warwick
Posted Hide Post
Ali

Thought the ‘execution order’ might be the case. smile

As for your suggestion, I'll have to think about that a bit more, when my brain isn’t quite so frazzled as it is today. There probably is a way to do most (all?) of the logic in Javascript, using the merge stuff to set variables right at the start, but I’d have no way of figuring it out after the day I’ve had so far...

Cheers


--
Andy Warwick
www.creed.co.uk
 
Posts: 216 | Location: Nottingham, UK | Registered: June 22, 2006Reply With QuoteEdit or Delete MessageReport This Post
Pro
Picture of Andy Warwick
Posted Hide Post
I nearly have the code working for searching the Note’s title for a substring and only printing out the Note’s details if it’s found.

<script type="text/javascript" language="Javascript">
  
  var search_string = " {HUD}" ;
		
  /* Create Objects to hold an associative array of Note titles and contents  */
		
  var noteTitles = new Object() ;
  var noteContents = new Object() ;
		
  /* Load the Objects by writing out each Note's details using the standard merge keys  */

  <$foreach note model.notesSortedByCreateDateDescending do$>
    noteTitles["<$note.noteID$>"] = "<$note.title.htmlSafe$>" ;
    noteContents["<$note.noteID$>"] = "<$note.title.htmlSafe$>" ;
  <$endforeach do$>	
		
  /* Un-comment next line to debug */
  /* for ( i in noteTitles ) { document.write( "<p>" + noteTitles[i] + ": " + noteContents[i] + "</p>" ) } ; */

  for ( id in noteTitles )
  { 
    if ( noteTitles[id].indexOf(search_string) != -1 ) /* Search title for string, and display contents if found */
    {				
      document.write( 
        '<table>' +
          '<thead><tr><th colspan="2">' + noteTitles[id].replace( search_string, '' ) + '</th></tr></thead>' +
            '<tbody>' +
              '<tr>' +
                '<td class="note_contents">' + noteContents[id] + '</td>' +
                '<td class="edit_note">' + '<a href="daylite://ShowObject/?Entity=Note&ID=' + id + '">' + 'Edit' + '</a>' + '</td>' +
              '</tr>' +
            '</tbody>' +
        '</table>' 
      ) ;
    }
  }

</script>


While the above code works as expected, when I change the reference in the second line of the loop that loads the data:

noteContents["<$note.noteID$>"] = "<$note.title.htmlSafe$>" ;


to the required:

noteContents["<$note.noteID$>"] = "<$note.plainTextRepresentation.htmlSafe$>" ;


it stops working.

Any idea why it should go wrong?

Any other merge key in that position seems to be okay; it's only when I actually try and get the contents of the Note that the code seems to break.

Thoughts?


--
Andy Warwick
www.creed.co.uk
 
Posts: 216 | Location: Nottingham, UK | Registered: June 22, 2006Reply With QuoteEdit or Delete MessageReport This Post
Pro
Picture of Andy Warwick
Posted Hide Post
Looks like it is something to do with line breaks and/or whitespace.

If I change the htmlSafe to rtfSafe:

noteContents["<$note.noteID$>"] = "<$note.plainTextRepresentation.rtfSafe$>" ;


I see the contents of the Note, but all the lines run together.

Is there any documentation on the difference between rtfSafe and htmlSafe, and what are they returning?

Ideally, I'd like to be able to parse out the plainTextRepresentation in a form that can be read by a Widget, complete with line-breaks and special characters, without worrying too much about it.


--
Andy Warwick
www.creed.co.uk
 
Posts: 216 | Location: Nottingham, UK | Registered: June 22, 2006Reply With QuoteEdit or Delete MessageReport This Post
Pro
Picture of Andy Warwick
Posted Hide Post
Is there any way of seeing the parsed results that are passed to WebKit, and hence displayed in the HUD?

It would make these things so much easier to debug if there was a way to see exactly what the HUD was attempting to render, once the merge keys have done their thing.

(Like a control-click>View Source in Safari.)

Right now, if something goes wrong with the syntax, all you get is a blank HUD or a crash; it would be so much better if there was some way of viewing the code that the HUD was trying to render.

Thoughts?


--
Andy Warwick
www.creed.co.uk
 
Posts: 216 | Location: Nottingham, UK | Registered: June 22, 2006Reply With QuoteEdit or Delete MessageReport This Post
Pro
Picture of Andy Warwick
Posted Hide Post
Bingo grin

Looks like the answer is to use htmlSafe and rtfSafe:

noteContents["<$note.noteID$>"] = "<$note.plainTextRepresentation.htmlSafe.rtfSafe$>" ;


Would still like some documentation on what these are doing behind the scenes (along with stringByRemovingReturns and stringByRemovingSurroundingWhitespace), but for now it looks like I've got it working.

Now to test it, parcel it up and release it…


--
Andy Warwick
www.creed.co.uk
 
Posts: 216 | Location: Nottingham, UK | Registered: June 22, 2006Reply With QuoteEdit or Delete MessageReport This Post
Pro
Picture of Andrew Migliore
Posted Hide Post
Seems like you want some function that does newlines to <BR /> and not rtfSafe.

Looking through this thread I also see you chose to use Javascript because the macro/replacement language provided does not have string searching.

This is a creative use but I worry about how inefficient this is given the code has to load all notes that are note email associated with the contact whether or no it has {HUD} pattern in the title.

So if I have 1000 notes and only 1 of them has {HUD} in them then all 1000 are loaded into memory.

cheers


Andrew Migliore
Lurker Films
http://www.lurkerfilms.com
 
Posts: 129 | Location: Portland, OR | Registered: August 15, 2007Reply With QuoteEdit or Delete MessageReport This Post
 Previous Topic | Next Topic powered by eve community  
 

Marketcircle    Forums  Hop To Forum Categories  Extending Daylite  Hop To Forums  Creating HUD Widgets    Possible to search a field for a sub-string?

© Copyright 2006 Marketcircle Inc. All rights reserved.