Communicating with IFrame

Creating IFrame in a web page is easy and it is also easy to have one way communication with it. WebPage can send a message to IFrame using PostMessage, but IFrame cannot send the message back to parent window. It is not allowed.

IframeSignalR

 

There are scenario’s where you may need to get the message back from IFrame. In this post, i will be talking about how you can have a two way communication with IFrame.

As we know, IFrame cannot send the message back to the root web, then we are left only with one option to go out to a service  and let that service send the message back to the root web. To have a good performance, we need to use web sockets for this communication. As you can see in the picture, the web page send the messages to IFrame using PostMessage, and when IFrame need to send any message back, it calls our SignalR server, which in turn calls the client method on parent web page.

You can find the full code here. Code is written using Typescript for web page pages, and C# for SignalR server. The code shows you how  JavaScript client can send and receive messages with SignalR server. In IFrame I load another web site, which is called “Extension B” in code.  First run SignalR server by pressing F5 in Visual studio. And then run the ExtensionB site and in last run the RootWeb site. Enjoy!

Requirements of RequireJS

I got couple of errors in my journey to use RequireJS. Looking at stackoverflow, I see many people stumble thru these problems. Many problems stems thru not reading the complete documentation of ‘require’ and lot of wrong and incomplete noise on internet. When you scroll thru RequireJS site, and see an example, you say oh, yea this is so simple, and with out reading much detail you jump into coding. In this article, I will go thru commons errors you may receive and their fix. In end and we will reach to a working code.

This code is referring to ECMA script5 using TypeScript 1.6. You choose ECMA version in project properties -> “TypeScript Build” pane in visual studio.

Here is my simple ‘Hello world’ application

<!DOCTYPE html>
<html lang="en">
<head>
         <script src="scripts/require.js></script>
</head>
<body>
       <div id="content"></div>
</body>
</html>

Here is my simple module

ModuleA {
      export class A {
        public static add(x: number, y: number): number {
           return x + y;
       }
    };
}

here is app.ts file, which is referring to ‘ModuleA’ which is sitting in scripts/js directory.

/// &lt;reference path="scripts/js/modulea.ts"&gt;
/// &lt;reference path="scripts/typings/requirejs/require.d.ts" &gt;

import moduleA = require('./scripts/js/ModuleA');
import _mA = moduleA.ModuleA;

window.onload = () => {
document.getElementById('content').innerHTML = "Answer:" +         _mA.A.add(2, 2).toString();
};

This is as simple as you can get. But when I tried to run this sample, my first build error was

TS1148 Cannot compile modules unless the ‘–module’ flag is provided.

This is simple, you have to go to Project properties by right clicking on project, choose “typescript build” and then choose “AMD” in module system.

After fixing this, I got the next error:

Error TS2306 File ‘E:/code/Require/Require/scripts/js/moduleA.ts’ is not a module. Require E:\code\Require\Require\app.ts 4

And you look at your code and it clearly says it is a module.  But soon you realize to put an ‘export’ in front of Module.

export ModuleA {
   export class A {
          public static add(x: number, y: number): number {
            return x + y;
          };
   };
};

Now, the code is compiling and running, but there is nothing just a blank page. Nothing on console, no clue what’s going wrong, it is just a blank page. There was no other option for me other than to go back and read RequireJS slowly and thoroughly and then I realized my mistake. I forgot “data-main” attribute in my html.

<script src="scripts/require.js" data-main="app"></script>

But, after fixing this, i run the application but there is again nothing on the page. But, now i can hit the break-point in my app.ts file. I hit the breakpoint and then F5, but nothing on the screen.

Back to RequireJS Site, now again reading their documentation slowly and thoroughly. And finally i hit to this :

It is possible when using RequireJS to load scripts quickly enough that they complete before the DOM is ready. Any work that tries to interact with the DOM should wait for the DOM to be ready. For modern browsers, this is done by waiting for the DOMContentLoaded event.

However, not all browsers in use support DOMContentLoaded. The domReady module implements a cross-browser method to determine when the DOM is ready. Download the module and use it in your project like so:

/// <reference path="scripts/js/modulea.ts" />
/// <reference path="scripts/typings/requirejs/require.d.ts" />

import moduleA = require('./scripts/js/ModuleA');
import _mA = moduleA.ModuleA;

require(['./scripts/domReady'], function (domReady) {
domReady(function () {
     document.getElementById('content').innerHTML =                      "Answer:" + _mA.A.add(2, 2).toString();
   });
});

Who knew that document.onload which works every where will not work with RequireJS.

However, using “domReady” solved the problem.

HTH

Parsing complex JSON

We were building a UI control which gets data from JSON. To keep things simple, we wanted our users to specify JSON in a simple “.” manner e.g. if JSON looks like this

var obj = {
     name: 'Rafat Sarosh',
     address: 'Redmond. Seattle'
}

then the HTML can be as simple as this:

<my-control column='name' data='name'>;

and then in our directives we can parse the JSON by simply writing row[data] and get the data. Here row is referring to the JSON obj and ‘data’ is referring to ‘name’. Effectively, row[data] will translate to obj[name] in my directive. So far so good. This all works fine, till you get a simple JSON, and you are more or less certain of the shape of JSON. The user of my directive is happy to write a simple dot notation in his HTML to access the data. But imagine, if JSON is little complex. e.g.

var obj = {

      f1: "Angular",
      f2: { 
             "name": "Rafat", 
             "address": [{ city: 'Seattle'}, {zip: '98059' }] 
           },
      f3: {
             "name": "Tim", 
             "address": [{ city: 'Redmond'}, {house: () => "I am function" }]
           },
      f4: [1, 2, 3]
}

then it becomes little difficult to write a simple parsing function. Imagine the following test cases if user want to access the data as follows:


'f3.address[1].house')()  //Should return "I am function"

'f3.address[0].city' //Should return 'Redmond'

'f2.name' //Should return 'Rafat'

'f4[2]' //Should return 3

'f1' //Should return 'Angular'

So here is the code to parse JSON of any complexity.

function getValue(object: any, path: string, replace: boolean = true): T {

  //convert, all the .,[,] to * so I can break the strings on *
  if (replace) //A switch so I don't do this every time in my recursive loop
      path = path.replace(/\]\.|\.|\[|\]/g, "*");
 
  var index = path.indexOf('*');
  if (index === -1)
      return object[path];

  //Get the first segment
  var firstSegment= path.slice(0, index);
  var remainingSegments= path.slice(index + 1);

  if (object[firstSegment] !== undefined && remainingSegments.length > 0) {
       return getValue(object[firstSegment], remainingSegments, false);
  } else
       return object[firstSegment];
}

Happy Coding!!!

Angular Directive – Table

I have to develop an application where most of the data is displayed in a table. Putting data in a table is pretty straight forward, but when you have multiple pages then you would like to have a table control which can be driven by data and added to pages with one line of HTML. Something like this:

<rmt-table data="data" columns="columns" on-row-clicked="RowClicked(Id)"></rmt-table/>

AngularDirectiveTable

To understand this post, get the full working code for the table directive from this plnkr. This directive takes two arrays of objects, name of columns, and the real data. This table directive has encapsulated paging and sorting functionality. Now dynamically you can supply different data and the page will show you different tables.

Code is straight forward, but I learn few things on the way. My first issue, was to get click on the table back to main controller with the row Id value. Unfortunately, this does not work as straight forward as you may think.

In main controller I have defined a function as follows:

  $scope.RowClicked = function(id) {
    alert(id);
  };

and as you can see in html, this function is defined on on-row-clicked.

<rmt-table data="data" columns="columns" on-row-clicked="RowClicked(Id)"></rmt-table/>

There are many angular courses and documentation where people are advised not to pass the parameter in the function, unfortunately this advice does not hold good anymore. Not only you have to pass the parameter in the function but the parameter name should be exact same as defined in your function definition.

Here is the function in directive:


angular.module('app').directive('rmtTable', function() {
  return {
    restrict: 'E',
    templateUrl: 'myTable.html',
    scope: {
      originalData: '=data',
      columns: '=columns',
      notifyParent: '&onRowClicked',
    },
 snip ....snip 

      $scope.rowClicked = function(row) {
        $scope.notifyParent({
          Id: row.Id
        });

Line 8, wire the external function to the directive. Line 13, calls the external function with the row id value. Here is the rub, you would think you could have called the function as follows:

      $scope.rowClicked = function(row) {
        $scope.notifyParent(row.Id);

But this will not work, this straight and simple way of calling function will not work, no matter what god you believe in :) You may think, that you can do some more javascript jugglery like

      $scope.rowClicked = function(row) {
        $scope.notifyParent()(row.Id);

but still this will not work. It has to be a object. And make sure your object, key must match the parameter you are passing in the function, in my case it is “Id”. Thanks to Dan Wahlin who taught me this trick.

My second issue was to debug the scripts in plnkr. By default, it shows the output in the same window, and if you press F12 you are presented by hundreds of minified JS. Fortunately, in the display IFrame you will find a blue color button which will ‘launch the preview window in a separate window’. In this separate window, it is just you and your code. Much easier to debug the issues.

Go ahead and play with this plnkr. Remember, this is a prototype, there are many things can be improved (for example, max row can also be supplied to directive), this is just to get you started on table directive.

Happy coding!

Tree View control with JavaScript and Knockout for large trees

[To understand this post, get the code from git]

Few days back, I needed  a Tree View, which should be able to show tree with thousands of nodes, a big tree, a really very big tree.

Most of the existing tree views used to crash my browser, they are making the naive mistake to load all the nodes in one shot. The simple solution was to load the nodes on demand, and when you open a node with thousands of children, and then you open another node which again has thousands of children, it is better to close the previous node and let go all their children than crash the browser.

Now, this tree automatic closing the previous node can have little more intelligent, and not necessarily close the node if the number of its children is small. So this tree is little intelligent in few aspect, it loads the children on demand, closes and flushes the nodes which creates the danger of crashing the browser. This tree trade performance over reliability.

To build a tree with JavaScript and  Knockout, you need to understand how knockout templeting works. The template binding populates the associated DOM element with the results of rendering a template. Templates are a simple and convenient way to build sophisticated UI structures. String-based templating is a way to connect Knockout to a third-party template engine. Knockout will pass your model values to the external template engine and inject the resulting markup string into your document.

If you don’t know about KO templating, then you should stop here and read more about it here.

To create a new string template engine, start with an existing nativeTemplateEngine.

CreateStringTemplateEngine method does that task.

SetEngine() {
         this.customTemplateEngine = TreeTempate.createStringTemplateEngine(
                new ko.nativeTemplateEngine(), this.templates);
        ko.setTemplateEngine(this.customTemplateEngine);
}

Inside createStringTemplateEngine, we use passed templateEngine and override it’s makeTemplateSource function to returns our stringTemplate, as follows:

     templateEngine.makeTemplateSource = function (templateName) {
           return new stringTemplate(templateName, templates);
    }

So again, take a nativeTemplateEngine of ko, and override it’s makeTemplateSource and return your own stringTemplate. This makeTemplateSource method is called from ko.renderTemplate function, which in turn calls your template “text” method.

Here is how template engine is implemented, you just have to implement one methods ‘text’:

class stringTemplate {

private _templateName: string;
private _templates: any;

constructor (templateName, templates) {
    this._templateName = templateName;
    this._templates = templates;
}
text (value) {
    if (arguments.length === 0) {
        return this._templates[this._templateName];
    }
    this[this._templateName] = value;
}
};

Templates is defined as follows:

knockoutTemplate

Pay close attention to how templates are recursively calling each other to draw the tree. First template (tree) calls nodes and nodes calls nodeCore to draw the real node. NodeCore has two calls one is to subNodes, and the other one is to nodeContent. Writing this recursive template is key to drawing your tree on the browser.

KO will call stringTemplate, text method again and again with the template names it keep encountering and we will keep returning their value e.g, for ‘tree’ text will return ”

The html of the page is as simple as this:

<html>
    <body>
       <div data-bind="template: { name: TreeTemplate }"  />
    </body>
</html>

<script>
 $(document).ready(function () {
        var d = new data.Data();
        var vm = new Tree.viewModel(d);
        ko.applyBindings(vm);
 });
</script>

This is how the tree will look.
TreeChild

When the Ko hit the following html  data-bind=”template: { name: TreeTemplate }”  It start processing the ‘tree’ template in templates. Which says for each item, please apply the node template. And then node template says for data apply the nodeCore template, and this templating continues on till we reach to nodeContent.

Rest of the code deals with clicking on nodes and loading children on demand, checking the status of tree, and automatically closing nodes which are too big and pose a threat to crash the browser. These cut-off number are configurable in the code, and you should be able to change and play with it. Interesting part of the code is many of the recursive functions. These functions need to traverse down thru the child nodes before they close a parent node.

Hopefully, this will get started you on your journey of creating a TreeView with KO and JavaScript.

Knockout click event knocking you out

Here is my simple viewmodel:

knockoutBindingViewModel

here is a simple html

knockoutBindinghtml

When I open this html in browser, I see removePerson method is called again and again, in fact all three times. Looks like whenever I bind a value of ‘people’ to html,  knockout calls removePerson. I don’t want this repeated call to ‘removePerson’, I want it to be called only on click, not on every bind.

Digging little deeper into knockout, you will find that ko registers the event and because of $data binding context, ko calls the removePerson again and again for every binding. So what is the solution?

Turns out that solution is simple, just remove $data from removePerson method and ko will stop calling this method for every binding.knockoutBindinghtml2

However, you may wonder once you have removed $data context from removePerson, then how your method will know about the person to be removed?

Here comes the ko magic, when ko calls the click method it automatically passes two arguments, first one is the current model value, which will point to the exact person and second one is event object. Read here more.

Once this repeated function call problem is solved, you get into another problem and you realize that your ‘anotherFunction’ is not called and in browser debugger window you find an error.

knockoutBindingError

The error says: Uncaught TypeError: undefined is not a function.

Before you go any further, Read this section again and see if you can find the source of  error.

What happens here is ko calls removePerson in eventHandler context which changes the meaning of “this”. You can see the value of ‘this’ in debugger. The documentation suggest you use ‘self’ or other variables and refer it in the function in place of ‘this’, and don’t be dependent on the ‘this’ value. But, if you are using typescript then you are stuck and isn’t typescript suppose to save you from ‘this’ and ‘self’ funny javascript behavior.

It turns out that there is another easy solution, to preserve the value of ‘this’ in typescript you should use lambda syntax. Just change the function as follows:

knockoutBindingViewModel2

This syntax of typeScript preserve the value of this. Read here how typeScript saves you from ‘this’ and ‘that’ of JavaScript.

Interestingly all this solution is simple once you know it, but if you don’t know,  you may break your head for hours, like I did :) hopefully it will save you some headache of ko and typeScript mood swings.

KISS

Few days back, I came across this clever piece of code, an extremely simplified version. Please give it 5 seconds to see what is happening here. OK, give it 10 second and you will see what this code is doing:


namespace LaLaLambda
{
    class Program
    {
        static void Main(string[] args)
        {
            Process();
        }

        static void  Process (){
            WriteResponse(
                (writer) =>
                {
                    writer.WriteIt("write this line");
                });
        }

        static void WriteResponse ( Action  writeAction)
        {
            using (TextWriter writer = File.CreateText("C:\\temp\\test.txt"))
            {
                writeAction( new MessageWriter (writer));
            }
        }
    }

    class MessageWriter {

        TextWriter tw;

        public MessageWriter (TextWriter t){
            tw = t;
        }
        public void WriteIt (string s)
        {
            tw.WriteLine (s);
        }
    }

I am showing you a very simple code which has all the details removed, and objects are simplified. Imagine, it is a code base of hundreds of files, Process and WriteResponse is in different files and part of different complex object. So what is the problem with the above code. Here are couple of problems I see:

While looking at Process, I am scratching my head, thinking what the hell is writer? Is it a text writer, memory writer or a sand writer (a writer which writes on sand). Remember, this is simplified version, it can be named foo in real code, and you have no clue what is foo? and there is not one object but bunch more object are passed to this anonymous function in real code, and you wonder about all other parameters too.

Second, when you reach to WriteResponse function, somewhere deep in the guts you find writeAction, and then you wonder hah! what writeAction do? You cannot search it as it has come to this function as a delegate. After realizing that, you go back to Process, and you see that it is calling WriteIt on the writer. So, what exactly is writer and then you go back to WriteResponse method again. After couple of jumping back and forth you understand what the hell is happening here.

I will say this is abusing lambda and delegate. Please don’t do it. Write a clever code, appreciate it, show it to your friend, but don’t check it in.

Here is a simplified version of above code:


namespace LaLaLambda
{
    class Program
    {
        static void Main(string[] args)
        {
            Process();
        }

       static void Process()
        {
            WriteResponse("write this line");
        }

        static void WriteResponse(string val)
        {
            using (TextWriter writer = File.CreateText("C:\\temp\\test.txt"))
            {
                MessageWriter mw = new MessageWriter(writer);
                mw.WriteIt (val);
            }
        }
    }

    class MessageWriter {

        TextWriter tw;

        public MessageWriter (TextWriter t){
            tw = t;
        }
        public void WriteIt (string s)
        {
            tw.WriteLine (s);
        }
     }

Above code has all the context together. It is simple, anyone can understand it. You look at it, and immediately you understand it, your eyes will not even wonder up and down.

I know, many of you will be starting your flame thrower to throw many examples at me where we need this kind of code where anonymous calling anonymous and taking a delegate as parameter and they all are tangled together like snakes. Yes, there are legitimate use cases, and use it when you cannot simplify it. For example


  static void  Process (){
            WriteResponse(
                (writer) =>
                {
                   //lot of complicated logic is happening here, for example you are checking writer and depending on it's type you are changing the further logic
                }
             );
        }

You can have different flavor of Process with almost similar construct. Different anonymous can keep the structure of code same but adding a new behavior only in certain cases. A good example of Open/close pattern. The existing code written is not changed, it is closed for modification but using lambda you can extend certain behavior of it.

In last, Write code as Hemingway wrote his stories. keep it simple!

Encroachment on Hard disk

Yesterday, I noticed my 100+ GB SSD has no space left. After investigation, I found my Skydrive is taking 20 GB. It was an easy fix, right click on Skydrive, click on property menu, choose Location tab and set a new location where your file should be synced. I moved all my skydrive files to E: drive and thought this will solve the problem.

Next day, when I came in office, I found again there is no free space left on my c drive. Empty the deleted folder, It helped with few hundred MB but still not much free space left. I looked around, try to find any huge files, music, pictures, video etc and deleted many files from C drive.  All this deleting did not free up any significant amount of space. Then I went ahead and tried the followings (you can reach to all these options by right clicking on drive and choos property):

  • Disk cleanup.
  • Compress this drive to save disk space.
  • Uncheck “allow files on this drive to have contents indexed in addition to file properties”
  • On tools tab you will find, ‘Error Checking’
  • Optimize and defragment drive

I ran all the above options, still no luck! Hard disk is still showing no space left.

Searched the hidden files (In explorer, click on view tab and check on  show all the hidden files to see hidden files) but found nothing.

When all else failed, I got a TreeSize utility (not recommending any particular product, as I did not use anything from web. There are many products, let me know which one you like) and looked at my hard disk. TreeSize showed me two hidden files hiberfile.sys and pagefile.sys both of 20 GB each. These files are not visible to naked eye, you have to use one of the TreeSize utilities.  Now how to delete this file:

Start Command Prompt as “Run as administrator.”
type powercfg -h off

This gets rid of your hiberfile.sys. But now, there is no more option to set your computer to hibernation. Hibernation feature creates this file. You can turn it ‘on’ by typing powercfg -h on.

for pagefile.sys, go to ControlPanel -> system->click on Advance -> then setup your page file size to a reasonable size.

Just fixing this freed up 40 GB for me.

Hope this will save you some time. I wasted almost two hours to solve this problem.