grid.utils.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*global jQuery, define, URL */
  2. (function( factory ) {
  3. "use strict";
  4. if ( typeof define === "function" && define.amd ) {
  5. // AMD. Register as an anonymous module.
  6. define([
  7. "jquery"
  8. ], factory );
  9. } else {
  10. // Browser globals
  11. factory();
  12. }
  13. }(function() {
  14. "use strict";
  15. //module begin
  16. $.extend($.jgrid,{
  17. //window.jqGridUtils = {
  18. stringify : function(obj) {
  19. return JSON.stringify(obj,function(key, value){
  20. return (typeof value === 'function' ) ? value.toString() : value;
  21. });
  22. },
  23. parseFunc : function(str) {
  24. return JSON.parse(str,function(key, value){
  25. if(typeof value === "string" && value.indexOf("function") !== -1) {
  26. var sv = value.split(" ");
  27. sv[0] = $.trim( sv[0].toLowerCase() );
  28. if( (sv[0].indexOf('function') === 0) && value.trim().slice(-1) === "}") {
  29. return eval('('+value+')');
  30. } else {
  31. return value;
  32. }
  33. }
  34. return value;
  35. });
  36. },
  37. encode : function ( text ) { // repeated, but should not depend on grid
  38. return String(text).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
  39. },
  40. jsonToXML : function ( tree, options ) {
  41. var o = $.extend( {
  42. xmlDecl : '<?xml version="1.0" encoding="UTF-8" ?>\n',
  43. attr_prefix : '-',
  44. encode : true
  45. }, options || {}),
  46. that = this,
  47. scalarToxml = function ( name, text ) {
  48. if ( name === "#text" ) {
  49. return (o.encode ? that.encode(text) : text);
  50. } else if(typeof(text) ==='function') {
  51. return "<"+name+"><![CDATA["+ text +"]]></"+name+">\n";
  52. } if(text === "") {
  53. return "<"+name+">__EMPTY_STRING_</"+name+">\n";
  54. } else {
  55. return "<"+name+">"+(o.encode ? that.encode(text) : text )+"</"+name+">\n";
  56. }
  57. },
  58. arrayToxml = function ( name, array ) {
  59. var out = [];
  60. for( var i=0; i<array.length; i++ ) {
  61. var val = array[i];
  62. if ( typeof(val) === "undefined" || val == null ) {
  63. out[out.length] = "<"+name+" />";
  64. } else if ( typeof(val) === "object" && val.constructor == Array ) {
  65. out[out.length] = arrayToxml( name, val );
  66. } else if ( typeof(val) === "object" ) {
  67. out[out.length] = hashToxml( name, val );
  68. } else {
  69. out[out.length] = scalarToxml( name, val );
  70. }
  71. }
  72. if(!out.length) {
  73. out[0] = "<"+ name+">__EMPTY_ARRAY_</"+name+">\n";
  74. }
  75. return out.join("");
  76. },
  77. hashToxml = function ( name, tree ) {
  78. var elem = [];
  79. var attr = [];
  80. for( var key in tree ) {
  81. if ( ! tree.hasOwnProperty(key) ) continue;
  82. var val = tree[key];
  83. if ( key.charAt(0) !== o.attr_prefix ) {
  84. if ( val == null ) { // null or undefined
  85. elem[elem.length] = "<"+key+" />";
  86. } else if ( typeof(val) === "object" && val.constructor === Array ) {
  87. elem[elem.length] = arrayToxml( key, val );
  88. } else if ( typeof(val) === "object" ) {
  89. elem[elem.length] = hashToxml( key, val );
  90. } else {
  91. elem[elem.length] = scalarToxml( key, val );
  92. }
  93. } else {
  94. attr[attr.length] = " "+(key.substring(1))+'="'+(o.encode ? that.encode( val ) : val)+'"';
  95. }
  96. }
  97. var jattr = attr.join("");
  98. var jelem = elem.join("");
  99. if ( name == null ) { // null or undefined
  100. // no tag
  101. } else if ( elem.length > 0 ) {
  102. if ( jelem.match( /\n/ )) {
  103. jelem = "<"+name+jattr+">\n"+jelem+"</"+name+">\n";
  104. } else {
  105. jelem = "<"+name+jattr+">" +jelem+"</"+name+">\n";
  106. }
  107. } else {
  108. jelem = "<"+name+jattr+" />\n";
  109. }
  110. return jelem;
  111. };
  112. var xml = hashToxml( null, tree );
  113. return o.xmlDecl + xml;
  114. },
  115. xmlToJSON : function ( root, options ) {
  116. var o = $.extend ( {
  117. force_array : [], //[ "rdf:li", "item", "-xmlns" ];
  118. attr_prefix : '-'
  119. }, options || {} );
  120. if(!root) { return; }
  121. var __force_array = {};
  122. if ( o.force_array ) {
  123. for( var i=0; i< o.force_array.length; i++ ) {
  124. __force_array[o.force_array[i]] = 1;
  125. }
  126. }
  127. if(typeof root === 'string') {
  128. root = $.parseXML(root);
  129. }
  130. if(root.documentElement) {
  131. root = root.documentElement;
  132. }
  133. var addNode = function ( hash, key, cnts, val ) {
  134. if(typeof val === 'string') {
  135. if( val.indexOf('function') !== -1) {
  136. val = eval( '(' + val +')'); // we need this in our implement
  137. } else {
  138. switch(val) {
  139. case '__EMPTY_ARRAY_' :
  140. val = [];
  141. break;
  142. case '__EMPTY_STRING_':
  143. val = "";
  144. break;
  145. case "false" :
  146. val = false;
  147. break;
  148. case "true":
  149. val = true;
  150. break;
  151. }
  152. }
  153. }
  154. if ( __force_array[key] ) {
  155. if ( cnts === 1 ) {
  156. hash[key] = [];
  157. }
  158. hash[key][hash[key].length] = val; // push
  159. } else if ( cnts === 1 ) { // 1st sibling
  160. hash[key] = val;
  161. } else if ( cnts === 2 ) { // 2nd sibling
  162. hash[key] = [ hash[key], val ];
  163. } else { // 3rd sibling and more
  164. hash[key][hash[key].length] = val;
  165. }
  166. },
  167. parseElement = function ( elem ) {
  168. // COMMENT_NODE
  169. if ( elem.nodeType === 7 ) {
  170. return;
  171. }
  172. // TEXT_NODE CDATA_SECTION_NODE
  173. if ( elem.nodeType === 3 || elem.nodeType === 4 ) {
  174. var bool = elem.nodeValue.match( /[^\x00-\x20]/ );
  175. if ( bool == null ) return; // ignore white spaces
  176. return elem.nodeValue;
  177. }
  178. var retval, cnt = {}, i, key, val;
  179. // parse attributes
  180. if ( elem.attributes && elem.attributes.length ) {
  181. retval = {};
  182. for ( i=0; i<elem.attributes.length; i++ ) {
  183. key = elem.attributes[i].nodeName;
  184. if ( typeof(key) !== "string" ) {
  185. continue;
  186. }
  187. val = elem.attributes[i].nodeValue;
  188. if ( ! val ) {
  189. continue;
  190. }
  191. key = o.attr_prefix + key;
  192. if ( typeof(cnt[key]) === "undefined" ) {
  193. cnt[key] = 0;
  194. }
  195. cnt[key] ++;
  196. addNode( retval, key, cnt[key], val );
  197. }
  198. }
  199. // parse child nodes (recursive)
  200. if ( elem.childNodes && elem.childNodes.length ) {
  201. var textonly = true;
  202. if ( retval ) {
  203. textonly = false;
  204. } // some attributes exists
  205. for ( i=0; i<elem.childNodes.length && textonly; i++ ) {
  206. var ntype = elem.childNodes[i].nodeType;
  207. if ( ntype === 3 || ntype === 4 ) {
  208. continue;
  209. }
  210. textonly = false;
  211. }
  212. if ( textonly ) {
  213. if ( ! retval ) {
  214. retval = "";
  215. }
  216. for ( i=0; i<elem.childNodes.length; i++ ) {
  217. retval += elem.childNodes[i].nodeValue;
  218. }
  219. } else {
  220. if ( ! retval ) {
  221. retval = {};
  222. }
  223. for ( i=0; i<elem.childNodes.length; i++ ) {
  224. key = elem.childNodes[i].nodeName;
  225. if ( typeof(key) !== "string" ) {
  226. continue;
  227. }
  228. val = parseElement( elem.childNodes[i] );
  229. if ( !val ) {
  230. continue;
  231. }
  232. if ( typeof(cnt[key]) === "undefined" ) {
  233. cnt[key] = 0;
  234. }
  235. cnt[key] ++;
  236. addNode( retval, key, cnt[key], val );
  237. }
  238. }
  239. }
  240. return retval;
  241. };
  242. var json = parseElement( root ); // parse root node
  243. if ( __force_array[root.nodeName] ) {
  244. json = [ json ];
  245. }
  246. if ( root.nodeType !== 11 ) { // DOCUMENT_FRAGMENT_NODE
  247. var tmp = {};
  248. tmp[root.nodeName] = json; // root nodeName
  249. json = tmp;
  250. }
  251. return json;
  252. },
  253. saveAs : function (data, fname, opts) {
  254. opts = $.extend(true,{
  255. type : 'plain/text;charset=utf-8'
  256. }, opts || {});
  257. var file, url, tmp = [];
  258. fname = fname == null || fname === '' ? 'jqGridFile.txt' : fname;
  259. if(!$.isArray(data) ) {
  260. tmp[0]= data ;
  261. } else {
  262. tmp = data;
  263. }
  264. try {
  265. file = new File(tmp, fname, opts);
  266. } catch (e) {
  267. file = new Blob(tmp, opts);
  268. }
  269. if ( window.navigator && window.navigator.msSaveOrOpenBlob) {
  270. window.navigator.msSaveOrOpenBlob( file , fname );
  271. } else {
  272. url = URL.createObjectURL(file);
  273. var a = document.createElement("a");
  274. a.href = url;
  275. a.download = fname;
  276. document.body.appendChild(a);
  277. a.click();
  278. setTimeout(function() {
  279. document.body.removeChild(a);
  280. window.URL.revokeObjectURL(url);
  281. }, 0);
  282. }
  283. }
  284. });
  285. //module end
  286. //return window.jqGridUtils;
  287. }));