Thursday, June 19, 2014

Javascript - Dynamically Updating a Handler

One of the coolest, yet lesser promoted objects in the JavaScript world is the ability to dynamically update the handler of a given function. To break it down, a user essentially "re-programs" on the client side an interactive Html element (<a>, <div>, <span>, etc.) to change which function is called, when an option is selected (By whatever input method is used). This can save a ton of coding and can allow functions to change based on user entered parameters. It's feasible that the chosen value could be bound to a cookie, but otherwise the function will always load with the starting option selected by the developer. This may seem basic, but it's also incredibly powerful and useful in many scenarios. I used it to create mapping assistance, in which the destination (Which has to be concatenated with differing circumstances, based on user input) would be new per visitor and the destination may be different as well, based on where the visitor is travelling to. It creates an elegant, scale-able solution that can be easily updated with minimal code. 

THE SCRIPT (Within the Html header):

<script type="text/javascript">
function startHere(userChoice)
{
   if(userChoice=="iChooseThis")
   {
   document.getElementById(userChoice).onclick=nowDoThisFunction;
   }
   else
   {
   document.getElementById(userChoice).onclick=nowDoThisFunction2;
}

function nowDoThisFunction()
{
//Do this code;
}

function nowDoThisFunction2()
{
//Do this code instead;
}
</script>


HTML (Using the very crude example of acting based on the element clicked - you could change the function parameter to grab a text box value, drop-down menu, etc.):

<a id="iChooseThis" href="javascript:" onclick="javascript: startHere(this.id);" />

Sunday, June 8, 2014

Loading a YouTube Channel as a Feed

For a few projects now, it has become necessary to cross-integrate as many items as possible. One example, would be showing a YouTube channel in feed style within a given web site. The usefulness is that videos can be posted to the targeted channel via a smartphone and or other computer on the fly and be available on both the YouTube channel and the targeted website (The feed coming off Google generaly takes about fifteen minutes).Additionally, this can allow for easy uploading from multiple persons, while offloading the security aspects onto Google. 
  • The "html.push" can be tweaked to the desired layout, which will be injected into the "youtube" div tag, following the script. 
  • With the ajax section, the "url" attribute needs to be adjusted to match the desired YouTube channel.The only variable within the url is the YouTube user id, which can be verified using Google's instructions here.
Here's the full code to make it all happen! 

<script type="text/javascript">
function show_my_videos(data){
html = ['<ul id="youtube-videos" style="list-style-type:none">'];
$(data.feed.entry).each(function(entry){
url = this.link[0].href;
url_thumbnail = this.media$group.media$thumbnail[3].url;
var description;
try
{
description = this.media$group.media$description.$t;
}
catch(err)
{
description = '';
}
html.push('<li><table style="width:250px;"><tr><td colspan=2></td></tr><tr><td style="vertical-align:top"><a href="'+url+'" target="_blank">');
html.push('<img src="'+url_thumbnail+'" alt="'+description+'" style="border:1px white solid">');
html.push('</a></td><td style="font-size:8pt">'+description+'</td></tr><tr><td colspan=2 style="border-bottom:1px white solid">&nbsp;</td></tr></table></li>');
});
html.push('</ul>');
$("#youtube").html(html.join(''));
}
$.ajax({
type: "GET",
url: "http://gdata.youtube.com/feeds/api/users/[YouTube User ID]/uploads?alt=json-in-script&format=5",
cache: false,
dataType:'jsonp',
success: function(data){
show_my_videos(data);
  }
});

</script>

<div id="youtube" style=""></div>


Example, as rendered in page:

Tuesday, May 27, 2014

Using the SharePoint RichTextBox

Through the course of my own development projects, I had a need to for a rich textbox, for which SharePoint does support OOTB. However, here is the catch - if you wish to employ it, I discovered through rigorous research that manual calls to SharePoint javascript libraries are necessary to invoke needed features, such as full HTML support within the rich textbox. After piecing together all the different namespaces and javascript libraries \ functions from several different informative sites, I arrived at the following working solution. Two functions (With a parameter) are needed that are referenced in the ...\layouts\forms.js library; CreateRichEdit(id), and IsSafeHrefAlert().

CreateRichEdit along with the below script will render out the text box and toolbars, based on the RichTextMode speficied in the InputFormTextBox. IsSafeHrefAlert will ensure that any hyperlinks or pictures inserted via the toolbars will function properly, if used in the way I've documented below that forces the result to be TRUE.

Hope it helps!

______________________________

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ Page Language="C#" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>


<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
<meta name="WebPartPageExpansion" content="full" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
<SharePoint:CssLink runat="server" Version="4"/>
                <SharePoint:DelegateControl runat="server" ControlId="AdditionalPageHead" AllowMultipleControls="true"/>
<SharePoint:SPShortcutIcon runat="server" IconUrl="http://fcbc-sp3/images/Icon3.ico"/>
               
                <SharePoint:SPPageManager runat="server"/>
                <SharePoint:SPHelpPageComponent Visible="false" runat="server"/>


<script type="text/javascript">

function CreateRichEdit(id) {

if (browseris.ie5up && browseris.win32 && !IsAccessibilityFeatureEnabled()) {

g_aToolBarButtons = null;

g_fRTEFirstTimeGenerateCalled = true;

RTE_ConvertTextAreaToRichEdit(id, true, true, "", "1033", null, null, null, null, null, "Compatible", "\u002f", null, null, null, null);

RTE_TextAreaWindow_OnLoad(id);

}

else {

document.write("&nbsp;<br><SPAN class=ms-formdescription><a href='javascript:HelpWindowKey(\"nsrichtext\")'>Click for help about adding basic HTML formatting.</a></SPAN>&nbsp;<br>");

};

}
   var id = "<%= txtmsg.ClientID %>";
  

function IsSafeHrefAlert()
{
return true;
}
   
</script>
</head>

<body  >

<form id="form1" runat="server">
  <asp:ScriptManager id="ScriptManager" runat="server" EnablePageMethods="false" EnablePartialRendering="true" EnableScriptGlobalization="false" EnableScriptLocalization="true" />
<SharePoint:InputFormTextBox ID="txtmsg" runat="server" Rows="20" Columns="50" TextMode="MultiLine"
                RichText="true" RichTextMode="FullHtml" />
                </form>

</body>

</html>

Tuesday, May 20, 2014

Back to Basics - Understanding How JQuery Animate Works

It occurred to me that I should scale back just a bit to help break down the basics of my favorite JQuery feature - the animation. You'll see this come up a lot in my write-ups, as this is such a simple, yet incredibly powerful feature. It calculates and literally "plays out" the difference between a start point and end point, which is not just limited to page locations. To begin, the following items are needed

  • Reference to JQuery library in the header (Latest is currently v 1.11.1)
    • <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
  • Method to invoke the function (Can be bound to whatever handler, such as onLoad, onClick, etc., using JQuery or plain Javascript)
    • function animateThis()
    • {
    • $('#myDiv').animate({
    •    opacity:'0.0',
    •    },1024,function(){doMore())}
    • );
    • }
  • A container to be animated (Div works best)
    • <div id="myDiv">This container will be animated using my parameters</div>


Now, to break down the animate method.


  • $('#myDiv') tells JQuery which element or container ID to animate (Match to the "id" attribute). 
  • .animate tells JQuery to use the "animate" method and perform it
  • ({opacity:'0.0',} tells JQuery which options to use for the animation math. I'm using opacity as example in this case, which, currently set to "0.0", instructs JQuery to animate myDiv from it's current opacity to 0% opacity (invisible). This can be set to different levels, such as "0.5", which would be 50% opaque - put another way, half transparent. The effect for this example is that the contents of myDiv would fade into nothing and disappear. 
    • Many CSS attributes can be used to animate, using any combination there-of
      • Positions can be used (margin-left, left, top, bottom, etc.) - *Interestingly right and margin-right are not supported
      • Sizing can be used (height, width, etc.)
  • 1024, tells JQuery how long the animation should take to finish out, in milliseconds (1024 ms = 1 second)
  • function(){doMore())}tells JQuery to call a new function at the completion of the animation (this is totally optional and can be omitted, if not needed)
That's it! Give it a try!

Thursday, May 8, 2014

Fluid Navigation using JQuery Animate Function

I promised I would do some writing on JQuery and give some examples of how to easily leverage that technology. JQuery can be an incredibly powerful enhancement to the UX of a page and (in some cases) the aesthetics. For the moment, I'm going to avoid the many plug-ins and add-on libraries, because they are as numerous as the stars. I highly recommend taking an evening and Googling or Binging (If that's a word?) "JQuery plugins", just to get a look at what's out there. It's truly amazing!

For one project, I needed to create a navigation process to scroll through different views of interactive elements, without leaving the homepage. All within the core library for JQuery, it has extensive support for animation, using the function example:

$('#[Tag to be animated]').animate({[CSS property]:'[Value]',},[speed in milliseconds, where 1000 = ~1 second]);

Keeping in mind, that I had nine "windows" or collections of differing interactive elements (Images, links, etc.), this is the code that drives things. It answers to handlers from different menu options or elements within the "windows" themselves, giving me the ability to scroll through a large Div, by showing only a fixed portion (Equally spaced nested Div's) at a time.

The below function allowed me to pass in a parameter to tell the function were to navigate next, based on static logic (The content of the "window" elements are in most cases dynamic, but they will always appear on the same static "window" position). These elements are refreshed with each "if" case, as it can't be known from which "window" the visiting human will be on when selecting a new option. When a match is found in the "if" cases, it performs the animation at the bottom, were all other windows are animated to full transparency, while the selected "window" is animated to full opacity. Hence, not only are the "windows" moving in and out of view to guide navigation, but they are also fading in and out. Visit www.tmcacademy.com and try a few options to see it in action!

function MoveOver(option)
{
var scrollThis;
var boxHome = 'home';
var boxPrograms = 'programs';
var boxEnrollment = 'enrollment';
var boxAboutUs = 'aboutus';
var boxFaqs = 'faqs';
var boxStaff = 'staff';
var boxCalendar = 'calendar';
var boxDirections = 'directions';
var boxContactUs = 'contactus';
var boxCommunityLinks = 'communitylinks';
var show;
var wipe1;
var wipe2;
var wipe3;
var wipe4;
var wipe5;
var wipe6;
var wipe7;
var wipe8;
var wipe9;

if(option=='home')
{
scrollThis = 0;
wipe1=boxPrograms;
wipe2=boxEnrollment;
wipe3=boxAboutUs;
wipe4=boxFaqs;
wipe5=boxStaff;
wipe6=boxCalendar;
wipe7=boxDirections;
wipe8=boxContactUs;
wipe9=boxCommunityLinks;
show=boxHome;
}
if(option=='programs')
{
scrollThis = 1000;
wipe1=boxHome;
wipe2=boxEnrollment;
wipe3=boxAboutUs;
wipe4=boxFaqs;
wipe5=boxStaff;
wipe6=boxCalendar;
wipe7=boxDirections;
wipe8=boxContactUs;
wipe9=boxCommunityLinks;
show=boxPrograms;

}
if(option=='enrollment')
{
scrollThis = 2000;
wipe1=boxHome;
wipe2=boxPrograms;
wipe3=boxAboutUs;
wipe4=boxFaqs;
wipe5=boxStaff;
wipe6=boxCalendar;
wipe7=boxDirections;
wipe8=boxContactUs;
wipe9=boxCommunityLinks;
show=boxEnrollment;
}
if(option=='about')
{
scrollThis = 3000;
wipe1=boxHome;
wipe2=boxPrograms;
wipe3=boxEnrollment;
wipe4=boxFaqs;
wipe5=boxStaff;
wipe6=boxCalendar;
wipe7=boxDirections;
wipe8=boxContactUs;
wipe9=boxCommunityLinks;
show=boxAboutUs;
}
if(option=='faqs')
{
scrollThis = 4000;
wipe1=boxHome;
wipe2=boxPrograms;
wipe3=boxEnrollment;
wipe4=boxAboutUs;
wipe5=boxStaff;
wipe6=boxCalendar;
wipe7=boxDirections;
wipe8=boxContactUs;
wipe9=boxCommunityLinks;
show=boxFaqs;
}
if(option=='staff')
{
scrollThis = 5000;
wipe1=boxHome;
wipe2=boxPrograms;
wipe3=boxEnrollment;
wipe4=boxAboutUs;
wipe5=boxFaqs;
wipe6=boxCalendar;
wipe7=boxDirections;
wipe8=boxContactUs;
wipe9=boxCommunityLinks;
show=boxStaff;
}
if(option=='calendar')
{
scrollThis = 6000;
wipe1=boxHome;
wipe2=boxPrograms;
wipe3=boxEnrollment;
wipe4=boxAboutUs;
wipe5=boxFaqs;
wipe6=boxStaff;
wipe7=boxDirections;
wipe8=boxContactUs;
wipe9=boxCommunityLinks;
show=boxCalendar;
}
if(option=='directions')
{
scrollThis = 7000;
wipe1=boxHome;
wipe2=boxPrograms;
wipe3=boxEnrollment;
wipe4=boxAboutUs;
wipe5=boxFaqs;
wipe6=boxStaff;
wipe7=boxCalendar;
wipe8=boxContactUs;
wipe9=boxCommunityLinks;
show=boxDirections;
}
if(option=='contact')
{
scrollThis = 8000;
wipe1=boxHome;
wipe2=boxPrograms;
wipe3=boxEnrollment;
wipe4=boxAboutUs;
wipe5=boxFaqs;
wipe6=boxStaff;
wipe7=boxCalendar;
wipe8=boxDirections;
wipe9=boxCommunityLinks;
show=boxContactUs;
}
if(option=='community')
{
scrollThis = 9000;
wipe1=boxHome;
wipe2=boxPrograms;
wipe3=boxEnrollment;
wipe4=boxAboutUs;
wipe5=boxFaqs;
wipe6=boxStaff;
wipe7=boxCalendar;
wipe8=boxDirections;
wipe9=boxContactUs;
show=boxCommunityLinks;
}

 $("#insideFooter").animate({
    marginLeft: '-'+scrollThis+'px',
     },500);
    $('#'+show+'').animate({
    opacity:'1.0',
    },1000);  
    $('#'+wipe1+'').animate({
    opacity:'0.0',
    },1000);
    $('#'+wipe2+'').animate({
    opacity:'0.0',
    },1000);
    $('#'+wipe3+'').animate({
    opacity:'0.0',
    },1000);
    $('#'+wipe4+'').animate({
    opacity:'0.0',
    },1000);
    $('#'+wipe5+'').animate({
    opacity:'0.0',
    },1000);            
    $('#'+wipe6+'').animate({
    opacity:'0.0',
    },1000);
    $('#'+wipe7+'').animate({
    opacity:'0.0',
    },1000);
    $('#'+wipe8+'').animate({
    opacity:'0.0',
    },1000);
    $('#'+wipe9+'').animate({
    opacity:'0.0',
    },1000);    
}

Just in case, an example of a calling handler would be the following (Being included within an HTML element):

<a href="#" onclick="javascript: MoveOver('home');">HOME</a>



Saturday, April 5, 2014

Mobile Posting to SharePoint 2010 Lists

In working with one of my latest projects (www.tmcacademy.com), I needed to create a process by which announcements could be easily updated in the event of a school delay or closing. One could argue that posting is a relatively simple task by flipping on the computer, logging in via the web, and creating an announcement that is pre-programmed to post on the homepage by a variety of methods, but this is the age of mobile computing. There should be an easy method for posting from a tablet or smartphone that doesn't require third party software (There are many mobile apps that allow for this, but setting it up for clients is not a easy process and simply not a supportable scenario). There is, of course, the text-only "mobile" translation, built in to the SharePoint 2010 web delivery mechanism, but this is of virtually NO value. It is WAY too confusing to navigate and FAR too different from the "real" web experience to fulfill anything useful (Again, like updating the site in the event of a delay or closing). This is an unfortunate shortcoming of SharePoint thus far, considering what mobile devices are actually capable of (The same technology delivers full-featured game consoles that support HD textured 3D engines). I digress.

An easy approach for the project is to leverage a very useful feature built into SharePoint; email-enabled lists. In the case of having a SharePoint server in-house, the farm(s) would first need to be setup to accept inbound email (Web or cloud hosted arrangements often set this part up for you). An excellent tutorial for this process can be found here. Once this part is up and running, enabling lists and or document libraries is simple.

View the list or document library in a browser and make sure to be signed in with an administrator account.

  • Under List Tools, click List, then List Settings



  • Under Communications, click Incoming e-mail settings


  • Flip "Allow this list to receive e-mail?" to Yes, then enter the beginning of the email address. This can be just about anything, baring odd characters, such as !@#$%^&*()/ \
  • The domain of the email address is determined by the server \ farm setup, and cannot be changed from here
  • The other options can be configured as needed and appropriate to the scenario




  • Click OK to finish


From here, we have effectively built a way to simply email in updates to the site from any devices that supports email, regardless of technology. This eliminates the need for an additional interface and is immediately ready to accept information from clients. The rest is up to filtering, formatting, and web part controls, which is a matter of preference and fit to the business need.

Enjoy!

Wednesday, April 2, 2014

Projects with Details to Come

So I've kept quite busy working on new projects. I was contracted to re-imagine the web presence for The Mission Church Academy and Preschool (http://www.tmcacademy.com), and I'm excited to have a brand new web environment in full production and receiving new visitors. Please stop over and take a frolic through the new site! I'm planning a few posts on how to accomplish some of the nifty elements within the upcoming weeks, such as leveraging SharePoint email-enabled document libraries and jQuery animation. See you again soon!