Monday, October 31, 2016

Sticky notes in your webpage... Part 2 with Database

In part 1, we discussed most of the functions in postitall. Here we discuss how we can save them in the database so that we can show them as and when needed. Let's see how we need to do it.

1. Add a new save button for a new personal note.

 var r = $('<a id="pia_save_0" class="PIAicon" href="#" style="opacity: 1; display: inline;"> <i class="fa fa-floppy-o note-icon-save" aria-hidden="true" title="Save" onclick="SaveNote();"> </i> </a>');  
         $("#idPIAIconBottom_0").append(r);  //0 is the id for new note. only one note can be opened at first and upon saving it, another can be opened.

2. "onclick="SaveNote();" this will call the function below;

 // The id is 0 for the new personal note. This is called inside your new personal note popup  
 function SaveNote() {  
   try{  
     var p = jQuery.noConflict();   
     cstPersonalNoteUpd(0, '', p('#pia_editable_0').html(), true, p('#minicolors_bg_0').val(), p('#minicolors_text_0').val(), '', '');  
     p.PostItAll.destroy('#PIApostit_0');  //0 is the id
   }  
   catch (err) {  
     alert(err.message);  
   }  
 }  


3. "cstPersonalNoteUpd" is a JS function that uses JQuery AJAX to handle DB operations (Insert / Update)


 function cstPersonalNoteUpd(idPersonalNote, lbPersonalNote, flActive, lbBackGroundColor, lbFontColor, dtCallBack, idPNDescr) {  
   try {  
     if (lbPersonalNote.length == 0)   
       return;  
     var objPara = {  
       idPersonalNote: idPersonalNote,        
       lbPersonalNote: lbPersonalNote,  
       flActive: flActive,  
       lbBackGroundColor: lbBackGroundColor,  
       lbFontColor: lbFontColor,  
       dtCallBack: dtCallBack  
     };  
     $.ajax({  
       type: "POST",  
       url: 'yourservice.asmx/cstPersonalNoteUpd',  
       data: JSON.stringify(objPara),  
       contentType: "application/json; charset=utf-8",  
       dataType: "json",  
       success: function (msg) {  
         var ret = JSON.parse(msg.d);  
         $('#hdnIdPersonalNote').val(ret); // get the id to a hidden field          
       },  
       error: function (err) {  
         alert(err.responseText);  
       }  
     });  
   }  
   catch (err) {  
     alert(err.message);  
   }  
 }  

4. Write your own service with "cstPersonalNoteUpd" webmethod.

     [WebMethod(EnableSession = true)]  
     public string cstPersonalNoteUpd(int idPersonalNote, string lbTitlePersonalNote, string lbPersonalNote, bool flActive, string lbBackGroundColor, string lbFontColor, string dtCallBack)  
     {  
       if (HttpContext.Current.Session["LoggedInPerson"] == null) return new JavaScriptSerializer().Serialize(string.Empty);  
       JavaScriptSerializer js = new JavaScriptSerializer() { MaxJsonLength = Int32.MaxValue };  
       PersonEntity person = (PersonEntity)HttpContext.Current.Session["LoggedInPerson"];  
       PersonalNoteEntity pn = new PersonalNoteEntity();  
       pn.idPersonalNote = idPersonalNote;         
       pn.idPerson = person.idPerson;         
       pn.lbPersonalNote = string.IsNullOrEmpty(lbPersonalNote.Trim()) ? null : lbPersonalNote.Trim();   
       pn.flActive = flActive;  //boolean
       pn.lbBackGroundColor = string.IsNullOrEmpty(lbBackGroundColor.Trim()) ? null : lbBackGroundColor.Trim();   
       pn.lbFontColor = string.IsNullOrEmpty(lbFontColor.Trim()) ? null : lbFontColor.Trim();   
       if (!string.IsNullOrEmpty(dtCallBack))  //for hiding
       {  
         DateTime dtCB = DateTime.ParseExact(dtCallBack, "dd/MM/yyyy",CultureInfo.InvariantCulture);  
         pn.dtCallBack = dtCB;  
       }        
       new bllPersonalNote().cstPersonalNoteUpd(pn);  
       return js.Serialize(pn.idPersonalNote.ToString());  //return the id
     }  

5. The "cstPersonalNoteUpd" function should be called whenever a text is changed or a new personal note is created to save them in the database. If new, the id is returned and otherwise it updates. (You need to handle it in your procedure as below;)

 IF ISNULL(@idPersonalNote, 0) = 0 AND @idPerson > 0      
  BEGIN     
  -- Mode Insert    
  END  
 ELSE  
  BEGIN   
  -- update more  
  END  
 END  

6. Now you have done both the server side as well as client side function call. A new note will be saved. But to update an existing one, you can use "onChange" event of the postitall.

 onChange: function (id) {  
       cstPersonalNoteUpd(pid, '', p('#pia_editable_' + pid).html(), true, p('#minicolors_bg_' + pid).val(), p('#minicolors_text_' + pid).val(), '', idPNDescr);  
       return undefined;  
     },  

The pid is the id of the note where you can pass to update the existing record in the DB.

7. Same as above, you can have your own service call to return the list of notes available in the DB and show them in the web application.

Let's see how it is shown in the application.

Adding a new note.



To show the list in your dashboard. ( clicking on a single note can open it in the note editor.






HTML Special Characters in your web application

HTMLEncode and HTMLDecode are common methods which can be in HttpUtility or WebUtility to encode and decode html tags or templates in your web application. Usually, HTML templates are updated by the Admin which uses the same system with admin rights. 
But what if the application you develop is accessing different databases that are updated from various admin portals or platforms. Hence the templates may contain many tags or characters and the above HTMLDecode will not work for them. 
Following method represents the conversion of those special characters to the HTML Encoded values so that it can be understood by the browser to show the correct symbol.
To do so, you will need to first get the ASCII of each of the character and if the values falls in to a special character, replace it with a properly formatted string. Simply it looks like below;

 public static string GetSpecialCharactersEncoded(string text)  
     {  
       if (text == null)  
         return text;  
       string returnString = text;  
       Dictionary<int, string> dicList = new Dictionary<int, string>();  
       StringBuilder sb = new StringBuilder();  
       foreach (var c in text.ToCharArray())  
       {  
         sb.Append(GetEncodedValue(c.ToString()));  
       }  
       return sb.ToString();  
     }  

     private static string GetEncodedValue(string text)  
     {  
       string encTxt = text;  
       int iAscii = (int)Convert.ToChar(text);  
       if (iAscii == 8230)  
         encTxt = "&hellip;";  
       else if (iAscii == 8211)  
         encTxt = "&ndash;";  
       else if (iAscii == 8212)  
         encTxt = "&mdash;";  
       else if (iAscii == 8216)  
         encTxt = "&lsquo; ";  
       else if (iAscii == 8217)  
         encTxt = "&rsquo;";  
       else if (iAscii == 8218)  
         encTxt = "&lsquor;";  
       else if (iAscii == 8220)  
         encTxt = "&ldquo; ";  
       else if (iAscii == 8221)  
         encTxt = "&rdquo;";  
       else if (iAscii == 8222)  
         encTxt = "&ldquor;";  
       else if (iAscii == 8224)  
         encTxt = "&dagger;";  
       else if (iAscii == 8225)  
         encTxt = "&Dagger;";  
       else if (iAscii == 8226)  
         encTxt = "&bull;";  
       else if (iAscii == 8240)  
         encTxt = "&permil;";  
       else if (iAscii == 8364)  
         encTxt = "&euro;";  
       else if (iAscii == 8482)  
         encTxt = "&trade;";  
       else if (iAscii == 402)  
         encTxt = "&fnof;";  
       else if (iAscii == 376)  
         encTxt = "&yuml;";  
       else if (iAscii == 353)  
         encTxt = "&scaron;";  
       else if (iAscii == 352)  
         encTxt = "&Scaron;";  
       else if (iAscii == 339)  
         encTxt = "&oelig;";  
       else if (iAscii == 338)  
         encTxt = "&OElig;";  
       else if (iAscii == 255)  
         encTxt = "&yuml;";  
       else if (iAscii == 254)  
         encTxt = "&thorn;";  
       else if (iAscii == 253)  
         encTxt = "&yacute;";  
       else if (iAscii == 252)  
         encTxt = "&uuml;";  
       else if (iAscii == 251)  
         encTxt = "&ucirc;";  
       else if (iAscii == 250)  
         encTxt = "&uacute;";  
       else if (iAscii == 249)  
         encTxt = "&ugrave;";  
       else if (iAscii == 248)  
         encTxt = "&oslash;";  
       else if (iAscii == 247)  
         encTxt = "&divide;";  
       else if (iAscii == 246)  
         encTxt = "&ouml;";  
       else if (iAscii == 245)  
         encTxt = "&otilde;";  
       else if (iAscii == 244)  
         encTxt = "&ocirc;";  
       else if (iAscii == 243)  
         encTxt = "&oacute;";  
       else if (iAscii == 242)  
         encTxt = "&ograve;";  
       else if (iAscii == 241)  
         encTxt = "&ntilde;";  
       else if (iAscii == 240)  
         encTxt = "&eth;";  
       else if (iAscii == 239)  
         encTxt = "&iuml;";  
       else if (iAscii == 238)  
         encTxt = "&icirc;";  
       else if (iAscii == 237)  
         encTxt = "&iacute;";  
       else if (iAscii == 236)  
         encTxt = "&igrave;";  
       else if (iAscii == 235)  
         encTxt = "&euml;";  
       else if (iAscii == 234)  
         encTxt = "&ecirc;";  
       else if (iAscii == 233)  
         encTxt = "&eacute;";  
       else if (iAscii == 232)  
         encTxt = "&egrave;";  
       else if (iAscii == 231)  
         encTxt = "&ccedil;";  
       else if (iAscii == 230)  
         encTxt = "&aelig;";  
       else if (iAscii == 229)  
         encTxt = "&aring;";  
       else if (iAscii == 228)  
         encTxt = "&auml;";  
       else if (iAscii == 227)  
         encTxt = "&atilde;";  
       else if (iAscii == 226)  
         encTxt = "&acirc;";  
       else if (iAscii == 225)  
         encTxt = "&aacute;";  
       else if (iAscii == 224)  
         encTxt = "&agrave;";  
       else if (iAscii == 223)  
         encTxt = "&szlig;";  
       else if (iAscii == 222)  
         encTxt = "&THORN;";  
       else if (iAscii == 221)  
         encTxt = "&Yacute;";  
       else if (iAscii == 220)  
         encTxt = "&Uuml;";  
       else if (iAscii == 219)  
         encTxt = "&Ucirc;";  
       else if (iAscii == 218)  
         encTxt = "&Uacute;";  
       else if (iAscii == 217)  
         encTxt = "&Ugrave;";  
       else if (iAscii == 216)  
         encTxt = "&Oslash;";  
       else if (iAscii == 215)  
         encTxt = "&times;";  
       else if (iAscii == 214)  
         encTxt = "&Ouml;";  
       else if (iAscii == 213)  
         encTxt = "&Otilde;";  
       else if (iAscii == 212)  
         encTxt = "&Ocirc;";  
       else if (iAscii == 211)  
         encTxt = "&Oacute;";  
       else if (iAscii == 210)  
         encTxt = "&Ograve;";  
       else if (iAscii == 209)  
         encTxt = "&Ntilde;";  
       else if (iAscii == 208)  
         encTxt = "&ETH;";  
       else if (iAscii == 207)  
         encTxt = "&Iuml;";  
       else if (iAscii == 206)  
         encTxt = "&Icirc;";  
       else if (iAscii == 205)  
         encTxt = "&Iacute;";  
       else if (iAscii == 204)  
         encTxt = "&Igrave;";  
       else if (iAscii == 203)  
         encTxt = "&Euml;";  
       else if (iAscii == 202)  
         encTxt = "&Ecirc;";  
       else if (iAscii == 201)  
         encTxt = "&Eacute;";  
       else if (iAscii == 200)  
         encTxt = "&Egrave;";  
       else if (iAscii == 199)  
         encTxt = "&Ccedil;";  
       else if (iAscii == 198)  
         encTxt = "&AElig;";  
       else if (iAscii == 197)  
         encTxt = "&#197;";  
       else if (iAscii == 196)  
         encTxt = "&Auml;";  
       else if (iAscii == 195)  
         encTxt = "&Atilde;";  
       else if (iAscii == 194)  
         encTxt = "&Acirc;";  
       else if (iAscii == 193)  
         encTxt = "&Aacute;";  
       else if (iAscii == 192)  
         encTxt = "&Agrave;";  
       else if (iAscii == 191)  
         encTxt = "&iquest;";  
       else if (iAscii == 190)  
         encTxt = "&frac34;";  
       else if (iAscii == 189)  
         encTxt = "&frac12;";  
       else if (iAscii == 188)  
         encTxt = "&frac14;";  
       else if (iAscii == 187)  
         encTxt = "&raquo;";  
       else if (iAscii == 186)  
         encTxt = "&ordm;";  
       else if (iAscii == 185)  
         encTxt = "&sup1;";  
       else if (iAscii == 184)  
         encTxt = "&cedil;";  
       else if (iAscii == 183)  
         encTxt = "&middot;";  
       else if (iAscii == 182)  
         encTxt = "&para;";  
       else if (iAscii == 181)  
         encTxt = "&micro;";  
       else if (iAscii == 180)  
         encTxt = "&acute;";  
       else if (iAscii == 179)  
         encTxt = "&sup3;";  
       else if (iAscii == 178)  
         encTxt = "&sup2;";  
       else if (iAscii == 177)  
         encTxt = "&plusmn;";  
       else if (iAscii == 176)  
         encTxt = "&deg;";  
       else if (iAscii == 175)  
         encTxt = "&macr;";  
       else if (iAscii == 174)  
         encTxt = "&reg;";  
       else if (iAscii == 173)  
         encTxt = "&shy;";  
       else if (iAscii == 172)  
         encTxt = "&not;";  
       else if (iAscii == 171)  
         encTxt = "&laquo;";  
       else if (iAscii == 170)  
         encTxt = "&ordf;";  
       else if (iAscii == 169)  
         encTxt = "&copy;";  
       else if (iAscii == 168)  
         encTxt = "&uml;";  
       else if (iAscii == 167)  
         encTxt = "&sect;";  
       else if (iAscii == 166)  
         encTxt = "&brvbar;";  
       else if (iAscii == 165)  
         encTxt = "&yen;";  
       else if (iAscii == 164)  
         encTxt = "&curren;";  
       else if (iAscii == 163)  
         encTxt = "&pound;";  
       else if (iAscii == 162)  
         encTxt = "&cent;";  
       else if (iAscii == 161)  
         encTxt = "&iexcl;";  
       else if (iAscii == 160)  
         encTxt = "&nbsp;";  
       else if (iAscii == 34)  
         encTxt = "&quot;";  
       return encTxt;  
     }  

To call this method;

string res = GetSpecialCharactersEncoded(str);

For further reading, visit here (http://www.ascii.cl/htmlcodes.htm)

The character entity reference chart is here. (https://dev.w3.org/html5/html-author/charref)

Using this method will show the special characters without any issues.

Happy coding... :)


Monday, October 24, 2016

Sticky notes in your webpage...

Sticky notes are a good way to keep things noted such as "To Do Lists". There are many ways we can have sticky notes in a desktop or notebook. But if we have a portal or a website and needed to provide this facility, we have a good JQuery plug in.

http://postitall.txusko.com/plugin.html

You may find there is a Chrome plugin too but what you need is the JQuery one. Download it and add it to your project. After that, provide relevant references.
 <!-- CSS -->  
 <link rel="stylesheet" href="dist/jquery.postitall.css">  
 <!-- JS -->  
 <script src="libs/jquery/dist/jquery.min.js"></script>  
 <script src="dist/jquery.postitall.js"></script>  

You also may find some plug ins such as JQuery draggabletrumbowyg editor and minicolors for color picker.

You can find relevant examples and "How to" in the documentation here.

Now let's see how we can use this in our own website with more features.

 function addPersonalNote() {  
   var p = jQuery.noConflict();  // to avoid multiple JQuery references
   p.PostItAll.new({  //create new note
     content: 'Default content to show',  //
     id: 0,  // this has to be unique 
     posX: $(window).width() / 2,  // center of the page
     posY: '10',  //top of the page
     features: { addNew: false },  // adding new upon another is prohibited
     onCreated: function (id, options, obj) { return undefined; },  //fires at the creation time
     onChange: function (id) {  return undefined;  },  // fires when content is changed
     onSelect: function (id) { return undefined; },  // fires when selected
     onDelete: function (id) { return undefined; }  // fires when deleted
   });  
 }  

This is a simple way of adding a new note. The function "addPersonalNote()" creates a new sticky note as below.

 You can maximize it, minimize or even close it. or even can add new icons too. Also you can have the option to change the color through color picker too.



In our next part, we will talk about how to save this in the database. Until that, practice what we have done so far...

Happy Coding... :)