Switch over to more modern unix sharedmemory API (requested by fatbob)
[umurmur.git] / shm_utils / umurmurd-websocket / web / js / crel.js
1 //Copyright (C) 2012 Kory Nunn\r
2 \r
3 //Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\r
4 \r
5 //The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\r
6 \r
7 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
8 \r
9 /*\r
10 \r
11     This code is not formatted for readability, but rather run-speed and to assist compilers.\r
12     \r
13     However, the code's intention should be transparent.\r
14     \r
15     *** IE SUPPORT ***\r
16     \r
17     If you require this library to work in IE7, add the following after declaring crel.\r
18     \r
19     var testDiv = document.createElement('div'),\r
20         testLabel = document.createElement('label');\r
21 \r
22     testDiv.setAttribute('class', 'a');    \r
23     testDiv['className'] !== 'a' ? crel.attrMap['class'] = 'className':undefined;\r
24     testDiv.setAttribute('name','a');\r
25     testDiv['name'] !== 'a' ? crel.attrMap['name'] = function(element, value){\r
26         element.id = value;\r
27     }:undefined;\r
28     \r
29 \r
30     testLabel.setAttribute('for', 'a');\r
31     testLabel['htmlFor'] !== 'a' ? crel.attrMap['for'] = 'htmlFor':undefined;\r
32     \r
33     \r
34 \r
35 */\r
36 \r
37 (function (root, factory) {\r
38     if (typeof exports === 'object') {\r
39                 if (!root.window) {\r
40                         var jsdom = require('jsdom').jsdom;\r
41                         root.window = jsdom().parentWindow;\r
42                 }\r
43                 module.exports = factory(root.window);\r
44     } else if (typeof define === 'function' && define.amd) {\r
45         define(factory.bind(null, window));\r
46     } else {\r
47         root.crel = factory(root.window);\r
48     }\r
49 }(this, function (window) {\r
50     // based on http://stackoverflow.com/questions/384286/javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object\r
51     var isNode = typeof Node === 'object'\r
52         ? function (object) { return object instanceof Node }\r
53         : function (object) {\r
54             return object\r
55                 && typeof object === 'object'\r
56                 && typeof object.nodeType === 'number'\r
57                 && typeof object.nodeName === 'string';\r
58         };\r
59 \r
60     function crel(){\r
61         var document = window.document,\r
62             args = arguments, //Note: assigned to a variable to assist compilers. Saves about 40 bytes in closure compiler. Has negligable effect on performance.\r
63             element = document.createElement(args[0]),\r
64             child,\r
65             settings = args[1],\r
66             childIndex = 2,\r
67             argumentsLength = args.length,\r
68             attributeMap = crel.attrMap;\r
69 \r
70         // shortcut\r
71         if(argumentsLength === 1){\r
72             return element;\r
73         }\r
74 \r
75         if(typeof settings !== 'object' || isNode(settings)) {\r
76             --childIndex;\r
77             settings = null;\r
78         }\r
79 \r
80         // shortcut if there is only one child that is a string    \r
81         if((argumentsLength - childIndex) === 1 && typeof args[childIndex] === 'string' && element.textContent !== undefined){\r
82             element.textContent = args[childIndex];\r
83         }else{    \r
84             for(; childIndex < argumentsLength; ++childIndex){\r
85                 child = args[childIndex];\r
86                 \r
87                 if(child == null){\r
88                     continue;\r
89                 }\r
90                 \r
91                 if(!isNode(child)){\r
92                     child = document.createTextNode(child);\r
93                 }\r
94                 \r
95                 element.appendChild(child);\r
96             }\r
97         }\r
98         \r
99         for(var key in settings){\r
100             if(!attributeMap[key]){\r
101                 element.setAttribute(key, settings[key]);\r
102             }else{\r
103                 var attr = crel.attrMap[key];\r
104                 if(typeof attr === 'function'){     \r
105                     attr(element, settings[key]);               \r
106                 }else{            \r
107                     element.setAttribute(attr, settings[key]);\r
108                 }\r
109             }\r
110         }\r
111         \r
112         return element;\r
113     }\r
114     \r
115     // Used for mapping one kind of attribute to the supported version of that in bad browsers.\r
116     // String referenced so that compilers maintain the property name.\r
117     crel['attrMap'] = {};\r
118     \r
119     // String referenced so that compilers maintain the property name.\r
120     crel["isNode"] = isNode;\r
121     \r
122     return crel;\r
123 }));\r