crv.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /* Copyright (c) Business Objects 2006. All rights reserved. */
  2. if (typeof(bobj) == 'undefined') {
  3. bobj = {};
  4. }
  5. if (typeof(bobj.crv) == 'undefined') {
  6. bobj.crv = {};
  7. // Constants used in the viewer
  8. bobj.crv.ActxPrintControl_CLSID = '88DD90B6-C770-4CFF-B7A4-3AFD16BB8824';
  9. bobj.crv.ActxPrintControl_Version = '12,2,218,2374';
  10. // Configuration for ALL viewers in a page
  11. // TODO The dhtmllib doesn't currently suport widgets from different locales
  12. // on the same page. We will need that support for the viewer.
  13. bobj.crv.config = {
  14. isDebug : false,
  15. scriptUri: null, // The uri for the viewer script dir (that holds crv.js)
  16. skin: "skin_standard",
  17. needFallback: true,
  18. lang: "en",
  19. useCompressedScripts: true,
  20. useAsync : true,
  21. indicatorOnly: false,
  22. logging: { enabled: false, id: 0}
  23. };
  24. bobj.crv.logger = {
  25. info: function(){ /* Do Nothing */}
  26. };
  27. // Udate configuration with properties from crv_config, if it exists
  28. if (typeof(crv_config) == 'object') {
  29. for (var i in crv_config) {
  30. bobj.crv.config[i] = crv_config[i];
  31. }
  32. }
  33. // If the uri for the bobj.crv script dir wasn't set, try to figure it out
  34. if (!bobj.crv.config.scriptUri) {
  35. var scripts = document.getElementsByTagName("script");
  36. var reCrvJs = /(.*)crv\.js$/i;
  37. for(var i = 0; i < scripts.length; i++) {
  38. var src = scripts[i].getAttribute("src");
  39. if(!src) { continue; }
  40. var matches = src.match(reCrvJs);
  41. if(matches && matches.length) {
  42. bobj.crv.config.scriptUri = matches[1];
  43. break;
  44. }
  45. }
  46. if (!bobj.crv.config.scriptUri ) {
  47. bobj.crv.config.scriptUri = '';
  48. if (bobj.crv.config.isDebug) {
  49. throw "bobj.crv.config.scriptUri is undefined";
  50. }
  51. }
  52. }
  53. /**
  54. * Parse a URI into its constitutents as defined in RFC2396
  55. *
  56. * @param uri [String] Uri to parse
  57. *
  58. * @return Returns object has string values for the uri's components
  59. */
  60. bobj.parseUri = function(uri) {
  61. var regExp = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$";
  62. var result = uri.match(new RegExp(regExp));
  63. return {
  64. scheme : result[2],
  65. authority : result[4],
  66. path : result[5],
  67. query : result[7],
  68. fragment : result[9]
  69. };
  70. };
  71. bobj.crvUri = function(uri) {
  72. return bobj.crv.config.scriptUri + uri;
  73. };
  74. bobj.skinUri = function(uri) {
  75. return bobj.crvUri("../dhtmllib/images/") + bobj.crv.config.skin + "/" + uri;
  76. };
  77. /**
  78. * Create a widget using json
  79. * eg.
  80. * createWidget({
  81. * cons: bobj.crv.myWidget,
  82. * args: {foo: 'bar'},
  83. * children: [ {cons: ...}, ...]
  84. * });
  85. *
  86. * @param json [Object] Object representing the widget
  87. * @return Returns the widget or null if creation failed
  88. */
  89. bobj.crv.createWidget = function(json) {
  90. if (!bobj.isObject(json)){
  91. return null;
  92. }
  93. var constructor = json.cons;
  94. if (bobj.isString(constructor)) {
  95. try {
  96. constructor = eval(constructor);
  97. }
  98. catch (err) {
  99. if (bobj.crv.config.isDebug) {
  100. throw "bobj.crv.createWidget: invalid constructor";
  101. }
  102. }
  103. }
  104. if (!bobj.isFunction(constructor)) {
  105. if (bobj.crv.config.isDebug) {
  106. throw "bobj.crv.createWidget: invalid constructor";
  107. }
  108. return null;
  109. }
  110. var widget = constructor(json.args);
  111. if (widget.addChild && bobj.isArray(json.children)) {
  112. for (var i = 0, len = json.children.length; i < len; ++i) {
  113. var child = bobj.crv.createWidget(json.children[i]);
  114. widget.addChild(child);
  115. }
  116. }
  117. return widget;
  118. };
  119. /**
  120. * Create a widget using bobj.crv.createWidget and write it to the document.
  121. * If optional element is given, the innerHTML is written instead and scripts
  122. * are executed.
  123. *
  124. * @param json [Object] Object representing the widget
  125. * @return Returns the widget or null if creation failed
  126. */
  127. bobj.crv.writeWidget = function(json, element) {
  128. var widget = bobj.crv.createWidget(json);
  129. if (widget) {
  130. if(element){
  131. var html = widget.getHTML();
  132. var ext = bobj.html.extractHtml(html);
  133. var links = ext.links;
  134. element.innerHTML = html;
  135. for(var iLinks = 0, linksLen = links.length; iLinks < linksLen; ++iLinks){
  136. bobj.includeLink(links[iLinks]);
  137. }
  138. var scripts = ext.scripts;
  139. for (var iScripts = 0, scriptsLen = scripts.length; iScripts < scriptsLen; ++iScripts) {
  140. var script = scripts[iScripts];
  141. if (!script) {continue;}
  142. if (script.text) {
  143. try {
  144. bobj.evalInWindow(script.text);
  145. }
  146. catch(err) {}
  147. }
  148. }
  149. var styleText = "";
  150. for (var i = 0, len = ext.styles.length; i < len ; i++) {
  151. styleText += ext.styles[i].text + '\n';
  152. }
  153. if(styleText.length > 0) {
  154. bobj.addStyleSheet(styleText);
  155. }
  156. }
  157. else {
  158. widget.write();
  159. }
  160. }
  161. return widget;
  162. };
  163. /**
  164. * Initialize a widget.
  165. *
  166. * @param widx [Int] Widget index
  167. * @param initElt [DOM Node, opt] The element that called the init event
  168. */
  169. bobj.crv._initWidget = function(widx, initElt) {
  170. try {
  171. var widget = _widgets[widx];
  172. widget.init();
  173. if (initElt) {
  174. MochiKit.DOM.removeElement(initElt);
  175. }
  176. }
  177. catch (err) {
  178. if (bobj.crv.config.isDebug) {
  179. var msg = "bobj.crv._initWidget: Couldn't initialize widget: ";
  180. msg += 'widx=' + widx;
  181. msg += ', type=';
  182. if (!_widgets[widx]) {
  183. msg += 'null';
  184. }
  185. else if (bobj.isString(_widgets[widx].widgetType)) {
  186. msg += _widgets[widx].widgetType;
  187. }
  188. else
  189. {
  190. msg += 'unknown';
  191. }
  192. msg += ' because: ' + err;
  193. throw msg;
  194. }
  195. }
  196. };
  197. /**
  198. * Creates an hidden image tag that initializes a widget using its onload
  199. * event. The tag should be appended to the html of the widget.
  200. *
  201. * Note: doesn't work in Opera 9, but we don't support that yet
  202. *
  203. * TODO nested widgets fail in Firefox because parent's init method has to
  204. * be called after child's init method and the timing of the img onload
  205. * events looks to be unpredictable. onerror might be more predictable if
  206. * no src is specified... is it supported in Safari?
  207. *
  208. * @param widx [Int] Widget index
  209. *
  210. * @return Returns html that initializes the widget with index == widx
  211. bobj.crv.getInitHTML = function(widx) {
  212. return bobj.html.IMG({
  213. style: {display:'none'},
  214. //onload: 'bobj.crv._initWidget(' + widx + ', this);',
  215. onload: "alert('loading " + widx + "')",
  216. src: bobj.crvUri("../dhtmllib/images/transp.gif")
  217. });
  218. }*/
  219. /**
  220. * Returns HTML that auto-intializes a widget.
  221. */
  222. bobj.crv.getInitHTML = function(widx) {
  223. var scriptId = 'bobj_crv_getInitHTML_' + widx;
  224. return '<script id="' + scriptId + '" language="javascript">' +
  225. 'bobj.crv._initWidget(' + widx + ',"' + scriptId + '");' +
  226. '</script>';
  227. };
  228. bobj.crv._preloadProcessingIndicatorImages = function() {
  229. var relativeImgPath = bobj.crvUri("../dhtmllib/images/" + bobj.crv.config.skin + "/");
  230. var images = [];
  231. var imgNames = [
  232. "wait01.gif",
  233. "dialogtitle.gif",
  234. "dialogelements.gif",
  235. "../transp.gif"];
  236. for(var i = 0, len = imgNames.length; i < len; i++){
  237. images[i] = new Image();
  238. images[i].src = relativeImgPath + imgNames[i];
  239. }
  240. };
  241. bobj.crv._loadJavaScript = function(scriptFile) {
  242. if (scriptFile) {
  243. document.write('<script language="javascript" src="'
  244. + bobj.crvUri(scriptFile)
  245. +'"></script>');
  246. }
  247. };
  248. /**
  249. * Include the viewer's scripts in the document.
  250. */
  251. bobj.crv._includeAll = function() {
  252. bobj.crv._includeLocaleStrings();
  253. if (bobj.crv.config.useCompressedScripts) {
  254. if (bobj.crv.config.indicatorOnly) {
  255. bobj.crv._loadJavaScript('../../processindicator.js');
  256. }
  257. else {
  258. bobj.crv._loadJavaScript('../../allInOne.js');
  259. if (bobj.crv.config.logging.Enabled) {
  260. bobj.crv._loadJavaScript('../log4javascript/log4javascript.js');
  261. }
  262. }
  263. return;
  264. }
  265. else {
  266. // list of script uris relative to crv.js
  267. var scripts = [
  268. '../MochiKit/Base.js',
  269. '../MochiKit/Async.js',
  270. '../MochiKit/DOM.js',
  271. '../MochiKit/Style.js',
  272. '../MochiKit/Signal.js',
  273. '../MochiKit/New.js',
  274. '../MochiKit/Color.js',
  275. '../MochiKit/Iter.js',
  276. '../MochiKit/Visual.js',
  277. '../log4javascript/log4javascript_uncompressed.js',
  278. '../external/date.js',
  279. '../dhtmllib/dom.js',
  280. 'initDhtmlLib.js',
  281. '../dhtmllib/palette.js',
  282. '../dhtmllib/menu.js',
  283. '../dhtmllib/psheet.js',
  284. '../dhtmllib/treeview.js',
  285. '../dhtmllib/dialog.js',
  286. '../dhtmllib/waitdialog.js',
  287. '../../prompting/js/promptengine_prompts2.js',
  288. '../../prompting/js/promptengine_calendar2.js',
  289. '../swfobject/swfobject.js',
  290. 'common.js',
  291. 'html.js',
  292. 'Toolbar.js',
  293. 'ToolPanel.js',
  294. 'ReportPage.js',
  295. 'ReportView.js',
  296. 'ReportAlbum.js',
  297. 'Separator.js',
  298. 'Viewer.js',
  299. 'ViewerListener.js',
  300. 'StateManager.js',
  301. 'IOAdapters.js',
  302. 'ArgumentNormalizer.js',
  303. 'event.js',
  304. 'PromptPage.js',
  305. 'Dialogs.js',
  306. 'StackedPanel.js',
  307. 'Tooltip.js',
  308. 'Parameter.js',
  309. 'ParameterController.js',
  310. 'ParameterUI.js',
  311. 'ParameterPanel.js',
  312. 'bobjcallback.js',
  313. 'Calendar.js',
  314. 'FlexParameterUI.js'
  315. ];
  316. for (var i = 0, len = scripts.length; i < len; i++) {
  317. document.write('<script language="javascript" src="'
  318. + bobj.crvUri(scripts[i])
  319. +'"></script>');
  320. }
  321. }
  322. };
  323. bobj.crv.getLangCode = function () {
  324. var splitChar = '_'; // default to java's locale split character
  325. if (bobj.crv.config.lang.indexOf ('-') > 0) {
  326. splitChar = '-'; // must be .Net's locale split
  327. }
  328. var lang = bobj.crv.config.lang.split(splitChar);
  329. // TODO (Julian): Java server side now handles the chinese variants due to language pack fix
  330. // We'll do the same for WebForm and then the following code can be removed.
  331. // For Chinese locales "zh" we only support "TW" & "CN"
  332. // zh_HK / zh_MO / zh_MY, are all mapped to zh_TW, everything else is zh_CN
  333. if(lang[0].toLowerCase() == "zh") {
  334. if(lang.length > 1 ) {
  335. var region = lang[1].toUpperCase();
  336. if(region == "TW" || region == "HK" || region == "MO" || region == "MY") {
  337. lang[1] = "TW";
  338. } else {
  339. lang[1] = "CN";
  340. }
  341. } else {
  342. lang[1] = "CN";
  343. }
  344. }
  345. // .NET webform viewers don't support variants from server side - until we fix this chop-off variant code for all other than "zh"
  346. if (lang.length > 1 && (!bobj.crv.config.needFallback || lang[0].toLowerCase() == "zh")) {
  347. lang = lang[0] + "_" + lang[1];
  348. }
  349. else {
  350. lang = lang[0];
  351. }
  352. return lang;
  353. };
  354. bobj.crv._includeLocaleStrings = function() {
  355. var lang = bobj.crv.getLangCode ();
  356. if (bobj.crv.config.needFallback) {
  357. bobj.crv._loadJavaScript('../../allStrings_en.js');
  358. if (lang == 'en') return; // DO NOT load 'en' strings below again!
  359. }
  360. bobj.crv._loadJavaScript('../../allStrings_' + lang + '.js');
  361. };
  362. if (typeof MochiKit == 'undefined') {
  363. MochiKit = {};
  364. }
  365. if (typeof MochiKit.__export__ == 'undefined') {
  366. MochiKit.__export__ = false; // don't export symbols into the global namespace
  367. }
  368. bobj.crv._preloadProcessingIndicatorImages();
  369. bobj.crv._includeAll();
  370. bobj.crv.initLog = function(logLevel, handlerUri) {
  371. bobj.crv.logger = log4javascript.getLogger();
  372. log4javascript.setEnabled(bobj.crv.config.logging.enabled);
  373. if (bobj.crv.config.logging.enabled) {
  374. bobj.crv.logger.setLevel(logLevel);
  375. var uri = handlerUri + '?ServletTask=Log';
  376. var ajaxAppender = new log4javascript.AjaxAppender(uri);
  377. bobj.crv.logger.addAppender(ajaxAppender);
  378. var oldLogFunc = bobj.crv.logger.log;
  379. bobj.crv.logger.log = function(level, message, exception) {
  380. oldLogFunc(level, bobj.crv.config.logging.id + ' ' + message, exception);
  381. };
  382. bobj.crv.logger.info('Logging Initialized');
  383. }
  384. };
  385. }
  386. if(typeof(Sys)!=='undefined') {
  387. Sys.Application.notifyScriptLoaded();
  388. }