appEditor.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: cpscrn
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2013
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. function CCAppEditor(id, context){
  9. this.id = id;
  10. this.context = context;
  11. window[id] = this;
  12. this.idGenCounter = 0;
  13. this.objectSelectMap = {};
  14. }
  15. CCAppEditor.prototype = {
  16. _generateId: function(){
  17. return this.id + this.idGenCounter++;
  18. },
  19. getAppId: function (){
  20. return this.id;
  21. },
  22. getUniqueId: function (name){
  23. return this.getAppId() + name;
  24. },
  25. render: function(){
  26. // Fetch the list of applications
  27. var _self = this;
  28. var xhr = this.context._getXHR({
  29. onSuccess: function(obj){
  30. try {
  31. var response = JSON.parse(obj.responseText);
  32. _self.applications = response.applications;
  33. var appId = _self.context.getSetting('app.id');
  34. if (appId){
  35. _self.renderApplicationSettings(appId);
  36. }else {
  37. _self.renderApplicationList();
  38. }
  39. } catch (e) {
  40. console.log(e);
  41. }
  42. }
  43. });
  44. xhr.open('GET', this.context.getProxiedURI(this.context.getGatewayURI() + '?b_action=xts.run&m=cps4/portlets/sdk2/service/getapps.xts'));
  45. xhr.send();
  46. // Check the current App setting.
  47. },
  48. _getApp:function(id){
  49. var app = null;
  50. if (this.applications && this.applications.applicationList){
  51. for (var i = 0 ; i< this.applications.applicationList.length; i++){
  52. if (this.applications.applicationList[i].id == id ){
  53. app = this.applications.applicationList[i];
  54. break;
  55. }
  56. }
  57. }
  58. return app;
  59. },
  60. renderApplicationSettings:function(appId){
  61. var app = this._getApp(appId);
  62. this.currentApp = app;
  63. if (app){
  64. var html = this._getEditPageHeader(app);
  65. html += this._getEditPageContent(app);
  66. }
  67. var mainDiv = document.getElementById(this.getUniqueId('EditorMain'));
  68. mainDiv.innerHTML = html;
  69. },
  70. _getEditPageHeader:function(app){
  71. var html = '<div class="cogSelectedAppHeader">';
  72. html+= this.htmlEncode(this._getMessageFromObject(app.title));
  73. html+='<div class="cogSelectedAppChange" onclick="window[\''+this.getAppId()+'\'].renderApplicationList()">';
  74. html+='Change selected application';
  75. html+='</div>';
  76. html+='</div>';
  77. return html;
  78. },
  79. _getEditPageContent:function(app){
  80. var html = '<div class="cogAppEditContent">';
  81. html +='<form name="'+this.getUniqueId('form')+'">';
  82. html += '<input type="hidden" name="app.url" value="'+this.htmlEncode(app.url)+'">';
  83. html += '<input type="hidden" name="app.id" value="'+this.htmlEncode(app.id)+'">';
  84. if (app.messagefile != undefined) {
  85. html += '<input type="hidden" name="app.messagefile" value="'+this.htmlEncode(app.messagefile)+'">';
  86. }
  87. if (app.path != undefined) {
  88. html += '<input type="hidden" name="app.path" value="'+this.htmlEncode(app.path)+'">';
  89. }
  90. if (app.features && app.features.featureList){
  91. html += '<input type="hidden" name="app.features" value="'+this.htmlEncode(app.features.featureList.toString())+'">';
  92. }
  93. if (app.properties && app.properties.propertyGroupList){
  94. for (var i = 0 ; i < app.properties.propertyGroupList.length; i++){
  95. html+= this._getEditPageGroup(app.properties.propertyGroupList[i]);
  96. }
  97. }
  98. html += '<div class="cogAppFormFooter">';
  99. html += '<input class="portlet-form-button" type="button" onclick="window[\''+this.getAppId()+'\'].context.saveSettings(document.forms[\''+this.getUniqueId('form') + '\'])" name="OK" value="OK">';
  100. html += '&nbsp;&nbsp;';
  101. html += '<input class="portlet-form-button" type="button" name="Cancel" value="Cancel" onclick="window[\''+this.getAppId()+'\'].context.cancelSettings()">';
  102. html += '&nbsp;&nbsp;';
  103. html += '<input class="portlet-form-button" type="button" name="Reset" value="Reset" onclick="window[\''+this.getAppId()+'\'].context.resetSettings()">';
  104. html += '</div>';
  105. html+='</form>';
  106. html+='</div>';
  107. return html;
  108. },
  109. _getEditPageGroup: function(group){
  110. var html = '<div class="cogAppEditGroup">';
  111. html += '<div class="cogAppEditGroupTitle">';
  112. html += this.htmlEncode(this._getMessageFromObject(group.title));
  113. html+='</div>';
  114. if (group.propertyList){
  115. for (var i = 0 ; i <group.propertyList.length; i++){
  116. var prop = group.propertyList[i];
  117. if (prop.control && prop.control.dependsOnChoice){
  118. // Dependent properties are rendered with the properties they depend on
  119. continue;
  120. }
  121. html+= this._getEditPageProperty(prop, group);
  122. }
  123. }
  124. html+='</div>';
  125. return html;
  126. },
  127. _getEditPageProperty: function(property, group){
  128. var html = '<div class="cogAppEditProperty">';
  129. if (property.control && this._getMessageFromObject(property.control.label)){
  130. html +='<div class="cogAppEditPropertyDesc">';
  131. html += this.htmlEncode(this._getMessageFromObject(property.control.label));
  132. html+='</div>'
  133. }
  134. html +='<div class="cogAppEditPropertyControl">';
  135. html += this._getEditPagePropertyControl(property, group);
  136. html+='</div>';
  137. html+='</div>';
  138. return html;
  139. },
  140. _getEditPagePropertyControl: function(property, group){
  141. var type;
  142. if (property.control){
  143. type = property.control.type;
  144. }
  145. if (!type){
  146. type = 'text'; // Default control type
  147. }
  148. var name = property.name;
  149. var valueList = this._getPropertyValueList(property);
  150. var size = this._getControlAttribute(property, 'size', 25);
  151. var isDisabled = false;
  152. if (property.control && property.control.dependsOnChoice){
  153. var p = this._getPropertyByName(property.control.dependsOnChoice.name, group);
  154. if (p){
  155. var v = this._getPropertyValueList(p);
  156. isDisabled = (this.indexOf(v, property.control.dependsOnChoice.value) == -1)
  157. }
  158. }
  159. var id = this._generateId();
  160. property.controlId = id;
  161. property.dependents = this._getDependentProperties(name, group);
  162. var html = '';
  163. if (type == 'text'){
  164. var value = valueList.length > 0 ? valueList[0]: '';
  165. // Text control
  166. html += '<input size="'+this.htmlEncode(size)+'" name="'+this.htmlEncode(name)+'" type="text" value="'+this.htmlEncode(value) +'"';
  167. if (isDisabled){
  168. html+=' disabled="true"';
  169. }
  170. html+='/>';
  171. }else if (type == 'radio' || type=='checkbox'){
  172. // Radio control
  173. for (var i = 0; i < property.control.choiceList.length; i++){
  174. var isChecked = (( valueList.length == 0) && i == 0 && type == 'radio') ||
  175. (this.indexOf(valueList, property.control.choiceList[i].value) != -1);
  176. html += this._getChoice(property, property.control.choiceList[i], type, name, group, isChecked, isDisabled, id);
  177. }
  178. } else if (type == 'cmobject'){
  179. var propertyName = this.htmlEncode(name);
  180. var parentID = this.getUniqueId('objectSelect');
  181. var selectorID = this.getUniqueId(propertyName);
  182. var validTypes;
  183. if (property.control.value == undefined) {
  184. validTypes = property.control.valueList;
  185. } else {
  186. validTypes = new Array(property.control.value);
  187. }
  188. var _self = this;
  189. function objectSelectOkCallback(cmObjs) {
  190. var cmObj = cmObjs[0];
  191. document.getElementById(selectorID + "_tooltip").innerHTML = cmObj.tooltip + ' ';
  192. document.getElementById(selectorID + "_input").value = cmObj.storeID;
  193. document.getElementById(selectorID + '_input').value = cmObj.storeID;
  194. document.getElementById(selectorID + '_inputImg').value = cmObj.iconLink;
  195. document.getElementById(selectorID + '_inputPath').value = cmObj.pathString;
  196. document.getElementById(selectorID + '_inputName').value = cmObj.tooltip;
  197. document.getElementById(selectorID + "_path").innerHTML = cmObj.pathString;
  198. document.getElementById(selectorID + "_icon").src = cmObj.iconLink;
  199. document.getElementById(selectorID + "_tooltip").innerHTML = cmObj.tooltip + ' ';
  200. document.getElementById(selectorID + "_clear").style.display = 'block';
  201. objectSelectCancelCallback();
  202. }
  203. function objectSelectCancelCallback() {
  204. document.getElementById(selectorID).style.display = 'none';
  205. document.getElementById(_self.getUniqueId('EditorMain')).style.display = 'block';
  206. }
  207. //Create CMObjectSelect instance
  208. if (!this.objectSelectMap[selectorID]) {
  209. this.objectSelectMap[selectorID] = new CMObjectSelect(selectorID, parentID, objectSelectCancelCallback, objectSelectOkCallback, validTypes, this.context);
  210. this.objectSelectMap[selectorID].render();
  211. }
  212. var input = '';
  213. var inputImg = '';
  214. var inputPath = '';
  215. var inputName = '';
  216. var path = '';
  217. var icon = '';
  218. var tooltip = '';
  219. var clearVisibility = 'none';
  220. if (this.context.settings[this.htmlEncode(name)] != undefined && this.context.settings[this.htmlEncode(name)] != '') {
  221. input = this.context.settings[this.htmlEncode(name)];
  222. inputImg = this.context.settings[this.htmlEncode(name) + '.img'];
  223. inputPath = this.context.settings[this.htmlEncode(name) + '.path'];
  224. inputName = this.context.settings[this.htmlEncode(name) + '.name'];
  225. path = this.context.settings[this.htmlEncode(name) + '.path'];
  226. icon = this.context.settings[this.htmlEncode(name) + '.img'];
  227. tooltip = this.context.settings[this.htmlEncode(name) + '.name'] + ' ';
  228. clearVisibility = 'block';
  229. }
  230. html += '<span id="'+ selectorID +'_path' +'">' + path + '</span>';
  231. html += '<img id="' + selectorID + '_icon' +'"height="16" width="16" src="' + icon + '" title="' + tooltip +'" style="vertical-align:middle;margin-right: 4px;" />';
  232. html += '<span id="'+ selectorID +'_tooltip' +'">' + tooltip + '</span>';
  233. html += '<a id="'+ selectorID + '_link'+'" href="javascript:window[\''+this.getAppId()+'\']._objectSelect(\''+selectorID+'\');">Select an object... </a>';
  234. html += '<a id="'+ selectorID + '_clear'+'" style="display:' + clearVisibility + '" href="javascript:window[\''+this.getAppId()+'\']._objectSelectClear(\''+selectorID+'\');">Clear</a>';
  235. html += '<input type="hidden" id="'+ selectorID + '_input' + '" name="'+this.htmlEncode(name)+'" value="' + input + '"></input>';
  236. html += '<input type="hidden" id="'+ selectorID + '_inputImg' + '" name="'+this.htmlEncode(name)+ '.img' + '" value="' + inputImg + '"></input>';
  237. html += '<input type="hidden" id="'+ selectorID + '_inputPath' + '" name="'+this.htmlEncode(name)+ '.path' + '" value="' + inputPath + '"></input>';
  238. html += '<input type="hidden" id="'+ selectorID + '_inputName' + '" name="'+this.htmlEncode(name)+ '.name' + '" value="' + inputName + '"></input>';
  239. }
  240. return html;
  241. },
  242. _getChoice:function(property, choice, type, name, group, checked, isDisabled, id){
  243. var choiceDependents = property.dependents[choice.value];
  244. var hasDependents = choiceDependents && choiceDependents.length > 0;
  245. var html = '<input onchange="window[\''+this.getAppId()+'\']._choiceChanged(this, \''+id+'\')" name="'+this.htmlEncode(name)+'" type="'+ this.htmlEncode(type)+ '" value="'+this.htmlEncode(choice.value)+ '"';
  246. if (checked){
  247. html+= ' checked=""';
  248. }
  249. if (isDisabled){
  250. html+=' disabled="true"';
  251. }
  252. html += '/>';
  253. html += this.htmlEncode(this._getMessageFromObject(choice.label)) + '<br>';
  254. // Render dependent properties
  255. if (hasDependents){
  256. html +='<div class="cogAppSubProps">';
  257. for (var i = 0; i < choiceDependents.length; i++){
  258. html += this._getEditPageProperty(choiceDependents[i], group);
  259. }
  260. html+='</div>';
  261. }
  262. return html;
  263. },
  264. _getPropertyValueList: function(property) {
  265. var value = this.context.getSettingValues(property.name);
  266. if(value === undefined && property.control){
  267. value = property.control.defaultValueList;
  268. }
  269. if(value === undefined){
  270. value = [];
  271. }
  272. return value;
  273. },
  274. _getControlAttribute: function(property, attribute, defaultValue ){
  275. var value;
  276. if(property.control){
  277. value = property.control[attribute];
  278. }
  279. if (!value){
  280. value = defaultValue;
  281. }
  282. return value;
  283. },
  284. _getPropertyById:function(id){
  285. if (this.currentApp.properties && this.currentApp.properties.propertyGroupList ){
  286. for (var i = 0 ; i< this.currentApp.properties.propertyGroupList.length; i++){
  287. var group = this.currentApp.properties.propertyGroupList[i];
  288. if (group.propertyList){
  289. for (var i = 0 ; i <group.propertyList.length; i++){
  290. var prop = group.propertyList[i];
  291. if (prop.controlId == id){
  292. return prop;
  293. }
  294. }
  295. }
  296. }
  297. }
  298. return null;
  299. },
  300. _getPropertyByName:function(name, group){
  301. var prop;
  302. if (group.propertyList){
  303. for (var i = 0 ; i< group.propertyList.length; i++){
  304. var p = group.propertyList[i];
  305. if ( p.name = name){
  306. prop = p;
  307. break;
  308. }
  309. }
  310. }
  311. return prop;
  312. },
  313. _getDependentProperties: function(name, group){
  314. var props = {};
  315. if (group.propertyList){
  316. for (var i = 0 ; i< group.propertyList.length; i++){
  317. var p = group.propertyList[i];
  318. if ( p.control && p.control.dependsOnChoice && p.control.dependsOnChoice.name == name){
  319. if (!props[p.control.dependsOnChoice.value]){
  320. props[p.control.dependsOnChoice.value] = [];
  321. }
  322. props[p.control.dependsOnChoice.value].push(p);
  323. }
  324. }
  325. }
  326. return props;
  327. },
  328. _choiceChanged: function(choice, id){
  329. var prop = this._getPropertyById(id);
  330. if (prop && prop.dependents){
  331. var choiceDependents= prop.dependents[choice.value];
  332. if (choiceDependents){
  333. for (var i = 0; i < choiceDependents.length; i++){
  334. var name = choiceDependents[i].name;
  335. var formControl = document.forms[this.getUniqueId('form')][name];
  336. if (formControl){
  337. formControl.disabled = !choice.checked;
  338. }
  339. }
  340. }
  341. if (choice.type == 'radio'){
  342. // Disable properties dependent on other choices.
  343. for (var name in prop.dependents){
  344. if (name != choice.value){
  345. var choiceDependents= prop.dependents[name];
  346. if (choiceDependents){
  347. for (var i = 0; i < choiceDependents.length; i++){
  348. var name = choiceDependents[i].name;
  349. var formControl = document.forms[this.getUniqueId('form')][name];
  350. if (formControl){
  351. formControl.disabled = choice.checked;
  352. }
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. console.log(this._getPropertyById(id));
  360. },
  361. _getMessageFromObject: function(object) {
  362. if (object === undefined) {
  363. return;
  364. }
  365. var resourceID = object.resourceID;
  366. if (resourceID != undefined) {
  367. return this.context.getMessage(resourceID);
  368. }
  369. else {
  370. return object;
  371. }
  372. },
  373. renderApplicationList: function(){
  374. var html = '<div class="cogAppEntryList">';
  375. if (this.applications && this.applications.applicationList){
  376. for (var i = 0 ; i< this.applications.applicationList.length; i++){
  377. html += '<div class="cogAppEntry" id="'+this.htmlEncode(this.applications.applicationList[i].id)+'">';
  378. var icon = this.applications.applicationList[i].icon;
  379. html += '<table cellpadding="0" cellspacing="0"><tr>'
  380. if (icon){
  381. html += '<td class="cogAppEntryIcon">';
  382. html += '<img src="'+ this.htmlEncode(icon.replace('$WEB$', this.context.getWebContentURI())) +'">';
  383. html += '</td>';
  384. }else{
  385. html += '<td class="cogAppEntryIcon">';
  386. html += '<img class="cogAppEntryEmptyIcon" src="'+ this.htmlEncode(this.context.getWebContentURI() + '/cps4/portlets/sdk/images/space.gif') +'">';
  387. html += '</td>';
  388. }
  389. html += '<td style="vertical-align:top;">';
  390. html += '<div class="cogAppEntryTitle">';
  391. html += this.htmlEncode(this._getMessageFromObject(this.applications.applicationList[i].title));
  392. html += '</div>';
  393. html += '<div class="cogAppEntryDesc">';
  394. html += this.htmlEncode(this._getMessageFromObject(this.applications.applicationList[i].description));
  395. html += '</div>';
  396. html += '</tr></table>';
  397. html += '</td>';
  398. html +='</div>';
  399. }
  400. }
  401. html+='</div>';
  402. var mainDiv = document.getElementById(this.getUniqueId('EditorMain'));
  403. mainDiv.innerHTML = html;
  404. var _self = this;
  405. var entries = this.getElementsByClassName(mainDiv, 'cogAppEntry');
  406. for (var i = 0 ; i < entries.length; i++){
  407. entries[i].onclick = function(event){
  408. _self.renderApplicationSettings(this.id);
  409. };
  410. }
  411. },
  412. _objectSelectClear: function(selectorID) {
  413. document.getElementById(selectorID + "_path").innerHTML ='';
  414. document.getElementById(selectorID + "_icon").src = '';
  415. document.getElementById(selectorID + "_tooltip").innerHTML = '';
  416. document.getElementById(selectorID + "_clear").style.display = 'none';
  417. document.getElementById(selectorID + "_input").value = '';
  418. document.getElementById(selectorID + "_inputPath").value ='';
  419. document.getElementById(selectorID + "_inputImg").value = '';
  420. document.getElementById(selectorID + "_inputName").value = '';
  421. },
  422. _objectSelect:function(selectorID) {
  423. document.getElementById(this.getUniqueId('EditorMain')).style.display = 'none';
  424. document.getElementById(selectorID).style.display = 'block';
  425. this.objectSelectMap[selectorID].resizePager();
  426. },
  427. /******* Utility functions ******/
  428. scope: function(scope, func){
  429. return function(){
  430. func.apply(scope, arguments);
  431. }
  432. },
  433. getElementsByClassName: function(node, className){
  434. return node.getElementsByClassName(className);
  435. },
  436. indexOf: function(array, value){
  437. return array.indexOf(value +'');
  438. },
  439. /*Not used so far */
  440. replaceTokens:function(template, tokenValues){
  441. for (var name in tokenValues){
  442. template.replace('$'+name+'$', tokenValues[name]);
  443. }
  444. return template;
  445. },
  446. /*Not used so far */
  447. cancelEventBuble: function(e){
  448. var evt = e ? e:window.event;
  449. if (evt.stopPropagation){
  450. evt.stopPropagation();
  451. }
  452. if (evt.cancelBubble!=null){
  453. evt.cancelBubble = true;
  454. }
  455. },
  456. htmlEncode: function(text)
  457. {
  458. return text.toString()
  459. .replace(/\&/g, "&amp;")
  460. .replace(/</g, "&lt;")
  461. .replace(/>/g, "&gt;")
  462. .replace(/"/g, "&quot;")
  463. .replace(/'/g, "&apos;");
  464. },
  465. /*Not used so far */
  466. getCssMapping: function(originalClass) {
  467. var newClass = this.context.getCssMapping(originalClass);
  468. if (!newClass) {
  469. return originalClass;
  470. }
  471. return newClass;
  472. }
  473. }