Showing posts with label HTML5. Show all posts
Showing posts with label HTML5. Show all posts

Feb 17, 2016

Javascript world clock with automatic time zones

One would imagine that getting a world clock - real time with automatic time zones including daylight savings - wouldn't be that difficult to include on your web site. On a standalone web site it probably isn't since there are several free or low cost solutions available. But in SharePoint it wasn't quite as straightforward.

One of my customers, a global company, already has one solution implemented on their intranet site, but there are two problems with it: it only refreshes the time on page refresh and the daylight savings (i.e. offset for each time zone) need to be adjusted manually every half a year. So I began to look into the options for modernizing this world clock of theirs.

First thing I realized was that most of the ready made solutions rely on PHP. This is already a no-no in SharePoint. Then I came accross this very cool site http://www.clocklink.com/ which has built in time zones and a multitude of analaog clocks (using html5 or flash). These could be used in SharePoint quite easily per se, and I even found a pretty neat solution using a SharePoint list to create a multitude of easily maintainable clocks. Check it out at Path to SharePoint

However, this requires cross-domain queries and moreover, when working in e.g. SharePoint Online, queries from https to http wich pretty much is a showstopper here. 

At this point I was starting to be pretty frustrated. Being so close and still so far away from a working solution. Then along came moment.js. A life saver, so to speak. Plus a very elegantly written tutorial for creating an analog clock using javascript and html5 canvas.

Combining all this plus a little bit of jquery I finally came up with a fully SharePoint (Online) compatible world clock solution (though I actually ripped the canvas part away, as I only needed a digital time display with some additional info). So, let's do a walk through for the benefit of the next one needing to do something similar:

1) Download moment.js (or min) and the suitable version of moment-timezone (from the Moment and Moment Timezone pages). Optionally also download jquery-file to avoid any cross-domain calls.

2) Upload the js-files into a suitable library in SharePoint, eg. Site Assets on the site.

3) Create a test file for compiling all the needed code to add to the script editor web part on your page and first off, add references to the js-files in the SharePoint library.

<script type="text/javascript" src="../SiteAssets/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="../SiteAssets/moment.min.js"></script>
<script type="text/javascript" src="../SiteAssets/moment-timezone-with-data.min.js"></script>

4) Add a bit of CSS:

<style type="text/css">
.current-time {
display: block;
font-weight: bold;
text-align: center;
width: 200px;
padding: 10px;
}
.clockcontainer {
  background-color: lightblue;
  border: 1px solid blue;
  border-radius: 5px;
  float: left;
  margin: 5px;
}
</style>

5) Add the javascript logic:

<script type="text/javascript">
document.addEventListener('DOMContentLoaded', startTimer);
function startTimer() {
    setInterval(displayTime, 1000);
    displayTime();

function displayTime() {
jQuery(".clockcontainer").each(function(){
var timezone = jQuery(this).attr("title");
var zonetitle = jQuery(this.firstElementChild).attr("title");
var now = moment().tz(timezone).format("h:mm:ss A");
var day = moment().tz(timezone).format("dddd MMMM DD, YYYY");

jQuery(this.firstElementChild).html(zonetitle + "<br/>" + day + "<br/>" + now);
});
}
</script>

(Note the jQuery instead of $ - in SharePoint this generally works more reliably)

6) Copy and paste all of the above to a Script Editor web part on the page where you are adding the world clocks, preferably somewhere close to the bottom of the page.

7. Add a Content Editor web part on the page where you want to display the clocks.

8) Edit the source of the CEWP content and add the clock elements (as many as you wish, but note the highlighted parts):

<div class="clockcontainer" title="Europe/London">
<div class="current-time" title="London">
time</div>
</div>

9) Modify each element to put out the correct time zone (title of the clockcontainer element) and the label for the clock (title of the current-time element). Check proper time zone names at the momentjs site.

That should be it, folks. Now, your SharePoint page should display correct times for your chosen time zones, e.g.


Momentjs offers plenty of options for time and date formatting. 

Apr 26, 2012

SharePoint UI Version5

Some 6 months or so ago I downloaded this demo MasterPage v5 zip, i.e. a package containing a MasterPage done in HTML5 format, CSS3 used for styling and some JavaScript to make the UI responsive and adapt to different devices and size of screens. I have been so wrapped up in other stuff, that I tried it out first time only this week, to demo it in the seminar on Tuesday.

The example is quite cool, featuring most of the general branding scenarios, and triggered the onClick in me, starting the learning-by-doing process. In a sense, I have long known how to go about it, but never have really taken it to my core line of work, to implement the stuff in SharePoint. So off to work I was, and as for here and now, welcome yourself to some notes on how to make it happen.

As for the MasterPage, you can build it more or less the way you always have, for HTML5 understands all of the old fluently. But, if you really want to use HTML5, you might want to rethink the normal DIV repertoire and replace some divs with the new elements, e.g. HEADER, HGROUP, SECTION, FOOTER, NAV, to mention the most probably needed ones. One thing to keep in mind though, is that section != div. You still need the divs. Sections are more general semantic element.

My basic structure for the MasterPage looks like this (after the basic workspace and bodycontainer divs, and discarding all the rest of the SharePoint stuff like page editing status bar etc.):

<header>
  <hgroup> <!-- in my case this is a bit unnecessary though, since my top nav is outside the header -->
    <!-- several divs for sitelogo, search etc. -->
  </hgroup>
</header>

<nav id="topnavi">
  <!-- global navigation -->
</nav>

<section id="contentwrap">
  <nav id="lefnavi">
    <!-- left navigation -->
  </nav>
  <div class="s4-ca with-leftnav" id="MSO_ContentTable">
              <!-- mso_contentdiv required, helps SharePoint put the web part editing pane in the main body on the right -->
        <div id="MSO_ContentDiv" runat="server">
                  <!-- page content loads from the pages and pages layout -->
                  <asp:ContentPlaceHolder ID="PlaceHolderMain" runat="server" />
        </div>
    </div>
</section>

<footer>
  <!-- divs for footer content -->
</footer>

Oh, and note the HTML5 Doctype and HTML tag (for MasterPage), simply with no schema declarations:

<!DOCTYPE html>
<html lang="<%$Resources:wss,language_value%>" dir="<%$Resources:wss,multipages_direction_dir_value%>" runat="server" __expr-val-dir="ltr">

As for the CSS, many things are just the same as with XHTML. But then there's a lot more you can do. Plus things to do to maintain the backwards browser compatibility! Without any regard to this, this is what you might get
in IE9 and any other current browser:


and this is how it looks like in IE7 (IE8 isn't much different but at least it understands floats!):


So how to go about it? I will point out some of the things I did with this UI of mine, and how to make them work in the older browsers as well:
  • The general older browser fix is to set the SECTION, HEADER etc. to display:block by default in your CSS
  • In my styling the trickiest parts are the drop shadow of the header and the selected link, the rounded corners, the opacity used in almost all backgrounds and the gradient background of the whole workspace. They currently require several different targeted rules in order to fly in all browsers.
  • Let's start with the gradient background. "Simply" apply CSS rules for all different browsers, using the ms filter fix to make it happen also in IE7/8 (already implemented in the picture above):
    background: #fff;
    background: -moz-linear-gradient(top, #7e8598 0%, #fff 75%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#7e8598), color-stop(75%,#fff));
    background: -webkit-linear-gradient(top, #7e8598 0%,#fff 75%);
    background: -o-linear-gradient(top, #7e8598 0%,#fff 75%);
    background: -ms-linear-gradient(top, #7e8598 0%,#fff 75%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#7e8598', endColorstr='#ffffff',GradientType=0 );
    background: linear-gradient(top, #7e8598 0%,#fff 75%);
  • Then there are the rounded corners. As for most browsers, they can be done in a combo way of several rules, e.g. my header corners:
    border-radius: 10px;
    -webkit-border-radius: 10px
    -moz-border-radius: 10px;
    -o-border-radius: 10px;

    As for IE7/IE8, they won't be round unless you want to use e.g. the htc fix or some code, such as CurvyCorners - see this pretty good article on rounded corners
  • The header also has a drop shadow. For most browsers, you need a similar set of rules as the corners:
    -webkit-box-shadow: -1px -5px 5px rgba(0, 0, 0, 0.5);
    -moz-box-shadow: -1px -5px 5px rgba(0, 0, 0, 0.5);
    -o-box-shadow: -1px -5px 5px rgba(0, 0, 0, 0.5);
    box-shadow: -1px -5px 5px rgba(0, 0, 0, 0.5);

    and then the filters for IE7 and IE8 - they don't make as fancy a shadow though, so I simply left it out of the final UI:
    /* for IE8 */
    -ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=5, Direction=45, Color='#333333')";
    /* For IE 5.5 - 7 */
    filter: progid:DXImageTransform.Microsoft.Shadow(Strength=5, Direction=45, Color='#333333');
The IE8 version of a shadow:
  • Then there is the transparent background color. As for the CSS3 compatible browsers, this is quite simple:
    background: rgba(255, 255, 255, 0.95);
    the last decimal number being the opacity value.
    As for IE7 and 8, there's ways of doing this, some of them work, some of them didn't seem to. I decided to make things easy for me and go with solid colors for older browsers.
Add some multiple backgrounds and stuff, and there's more to do, this was just the basics for this site. I suppose we're getting there, but (ok, I know I'm repeating myself now) until we're done with IE8 and older + the other browsers start to all implement the standard CSS3... unh...

For more information and HTML5 + CSS3 tutorials, see e.g. w3Schools tutorials. And besides these, there are many other good ones for HTML5 and CSS3. I myself found e.g. the Impressive Webs quite cool, not a conventional tutorial really, but having many cool tips and info in edible bits.

Apr 24, 2012

Branding SharePoint - What, How, Why?

In our own SharePoint seminar Hyvät, pahat ja rumat (the good, the bad and the ugly) I gave a presentation on SharePoint Branding - what you can do and how you should do it. Many aspects of this topic I have covered in several posts in this blog from a quite technical perspective, jumping straight to how. Let's have a look at the what and why, and a bit more general how.

Why?

SharePoint out of the box is fully functional and ready to use from the UI perspective, too. But to be honest, it's not exactly pretty. Whereas the v4.master for team sites is maybe adoptable for a team site based intranet, the nightandday masterpage can really not be seen as anything else but an example of a publishing master. Neither one of them has the look and feel of the company implementing SharePoint, naturally. The best you can do without some sort of web development is use themes and change the logo, and save the site as template.

In most cases this is not enough. Even the intranet should look nice and reflect the company brand. Once I had made this branding solution to a company, the page all branded and looking nice, but when the customer representative first saw it, his first comment was: "That ribbon is the wrong color!" I changed the color of the ribbon row and he sighed: "Now it's feeling more homey." Despite what the IT guys and the developers might like to believe, branding is important, beyond the usability issues.

What?

SharePoint pages are HTML and CSS, just like any other web pages. It's got a whole lot of server side C# code and client side JavaScript in it, but as for the UI, it is (to be exact) XHTML (1.x) and CSS (2.1), and thus almost as customizable and flexible as web development in general. Almost. So why isn't any web developer capable of SharePoint branding? Because of the server environment, special functionalities and UI with the ribbon and webparts, and that server side code.

It is essential to understand the structure of SharePoint in order to understand what can be done, what not, and how to do it in a SharePoint way. You need to know about the SharePoint controls in order to know what to use and what to maybe ditch. You need to know that you can never ever delete the PlaceHolders from the masterpage (the what from where? - exactly! you need to know!). You need to know that there are limitations to the branding. E.g. webparts have a table-based HTML structure with a limited amount of unique IDs and as for the branding of web parts you are tied to this. You can go to an extent, but you may not be able realize all of the wild visions of the designer who knows nothing about the SharePoint limitations.

How?

Microsoft and several other instances promote SharePoint Designer a lot. But seriously: it is a good tool for e.g. external data connections, customizing list views on a specific page, creating a custom workflow etc. stuff for a single existing site, but it is not a branding tool when you want to have a managable branding solution that can easily be deployed in several site collections and not breaking with migration. For that, you really need a proper SharePoint branding solution made in a managable way in VisualStudio, deployed as a .wsp package, full with features.

A typical branding solution can contain:

Currently I still use XHTML and the CSS v2. There are a whole lot of IE7s still in use in the customer organizations, and even these web technologies create some problems with IE7 and require some specific CSS rules (instead of separate stylesheets, I prefer the * prefix to target IE7) in order to look like it should. HTML5 used in the way intended requires a whole lot of polyfills to work in older browsers, and CSS3 features are still partly browser tech dependent (i.e. webkit vs. moz) as is, so using those already means double work in many places, let alone targeting IE7, unless we simply decide the IE7 users be left with a less rich UI.

But, the future is in HTML5 and CSS3 plus JavaScript, so it's only a matter of time really. Mobile world is already there, more than the regular PC.

Some resources for browser compatibilities:

http://html5readiness.com/
http://mobilehtml5.org/
http://www.findmebyip.com/litmus/
http://www.quirksmode.org/m/table.html

Oct 24, 2011

Top Appeals of HTML5 & CSS3 - and Why It's Not Yet Enough

Taking a quite subjective viewpoint here to this much discussed topic, this article is about what I find to be the most appealing features of HTML5 and CSS3, and then again the reasons why I still don't use them (at least not much), in my line of work.

I create branding solutions according to graphic layouts created by graphic designers at advertisement agencies. Most of the branding solutions are for company intranets/extranets, quite often with a pre-defined set of browsers, quite often with a clausule "most of our employees still have Internet Explorer 7". Look at eg. the chart here to see how much use there is of HTML5 and CSS3 with the IE7! So yes, this is the uttermost reason for me to not to use HTML5 just yet. CSS3 I do use a bit, knowing that the IE7 users will be missing the fun.

As for the appeals, or should I say, the things that I most anticipate, waiting to be able to fully start to utilize these newer web technologies:
  1. rounded corners
  2. drop shadows
    The designers and the customers love this stuff! And yeah, they look nice. But did you know that in order to build a box with rounded corners (and/or drop shadows) takes either a fixed size (you can use a single background image), 3 div-elements (partial sccalability), or all of 6 divs (full scalability)? And when the borders and shadows and fills are different colors here and something else there, you need a new set of classes for each box. It's not a walk in the park with CSS3 yet either, what with the different browser supports, but it has potential.
  3. transparency
    You can do it without CSS3 too - but you need a different atribute for each browser.
  4. multiple background images
    Multiple background images? Not without multiple divs - but with CSS3 it's possible and this would be extremely useful in many cases, especially when the designers decide to make web parts look extra fancy.
  5. the new css selectors such as nth-child, last-of-type etc.
    How many times is the request to have "every other line (eg. of a SharePoint generated list) with light gray background or the first one looking like this and the last one like that and maybe we could even have the second last look a bit different"? With CSS3 you can.
So actually, the top 5 for one working with SharePoint branding is all about CSS3. Then again, SharePoint branding is more about CSS than (X)HTML. The site wire frame is such basic html that whether I use div class="footer" or simply footer-element, is mostly irrelevant. But if I needed to choose the two most appealing HTML5 features in general, they would be:
  1. video embedding
    Now, if we could forget the IE7 (and even IE8), this would be one of the best working features in these single-browser environments (since the whole video format support part of this is still a mess) like we have in quite many companies.
  2. easier forms
    This would be an extremely cool thing if only the browsers would support the features for real. I made a little experiment on my own (non-SharePoint) test site, out of curiosity. I made an HTML5 form, using all of the new features available, and NONE of the browsers (Opera, Safari, Chrome, IE7-9, Firefox - the newest versions available at the time, Aug. 2011) supported everything and in most cases most of the more useful features simply were lost. And as for me again, I don't really need to create forms, or if I do it's InfoPath.
Ok, among the other cool things, there is also the canvas, the geolocation, the local storage and session storage etc. etc. But they don't really play any role in my line of work, so I'll leave it to someone else to sing their praise. With SharePoint, especially with intranets, especially with the older IEs still in the picture, I'm still waiting for the right time to switch to HTML5, and CSS3 for good.

P.S. The mobile browsers generally have a pretty good support for HTML5 and CSS3
Most of the mobile browsers, eg. for the Safari for iOS, and most Android browsers, have a nice support for the most common HTML5 and CSS3 features. Take a look at this here chart. Looks nice, huh?