Xataface HTML Reports Module 0.2
HTML Reports Module for Xataface
js/ckeditor/plugins/insertmacro/insertmacro.js
Go to the documentation of this file.
00001 //require <xataface/modules/htmlreports/schemabrowser.js>
00002 //require <xataface/modules/htmlreports/RelationshipSelector.js>
00003 //require <ckeditor.js>
00004 //require-css <ckeditor/plugins/insertmacro/insertmacro.css>
00005 
00006 
00007 
00008 
00009 (function(){
00010         var SchemaBrowser = xataface.modules.htmlreports.SchemaBrowser;
00011         var RelationshipSelector = xataface.modules.htmlreports.RelationshipSelector;
00012         
00013         var $ = jQuery;
00014         //alert('here');
00015         //CKEDITOR.config.toolbar_XBasic = [['button-pre', 'Bold', 'Italic', 'Underline', 'InsertMacro']];
00016         CKEDITOR.config.toolbar_XBasic = $.merge([], CKEDITOR.config.toolbar_Full);
00017         CKEDITOR.config.toolbar_XBasic.push(['InsertMacro', 'setrelationship', 'validatetemplate','addsectionheader','addsectionfooter', 'previewreport', 'previewreporttable']);
00018         //[['button-pre', 'Bold', 'Italic', 'Underline', 'InsertMacro']];
00019         CKEDITOR.plugins.add('insertmacro', {
00020                 init: function(editor){
00021                         
00022                         
00023                         
00024                         
00025                         var pluginName = 'insertmacro';
00026                         //CKEDITOR.dialog.add(pluginName, this.path + 'dialogs/foo.js');
00027                         
00028                         
00029                         editor.addCss('div.xf-htmlreports-section-header{ border: 1px dotted #8cacbb; background-image: url('+XATAFACE_MODULES_HTMLREPORTS_URL+'/images/section_header.png); background-repeat: no-repeat;padding: 10px 0px;}');
00030                         editor.addCss('div.xf-htmlreports-section-footer{ border: 1px dotted #8cacbb; background-image: url('+XATAFACE_MODULES_HTMLREPORTS_URL+'/images/section_footer.png); background-repeat: no-repeat;padding: 10px 0px;}');
00031                         
00032                         
00033                         
00037                         editor.addCommand(pluginName, new CKEDITOR.command(editor, {
00038                                 
00039                                 exec: function(){
00040                                         //alert(editor.element);
00041                                         var tableName = editor.element.getAttribute('data-xf-htmlreports-tablename');
00042                         
00043                                         XataJax.ready(function(){
00044                                         
00045                                                 
00046                                         
00047                                                 var div = document.createElement('div');
00048                                                 
00049                                                 var sb = new SchemaBrowser({
00050                                                         query: {'-table': tableName}
00051                                                 });
00052                                                 sb.bind('fieldClicked', function(event){
00053                                                         try {
00054                                                                 editor.insertText(event.macro);
00055                                                         } catch (e){
00056                                                                 alert('Please select the position in the template where you would like this field to be inserted.');
00057                                                         }
00058                                                 });
00059                                                 sb.update();
00060                                                 
00061                                                 $(div).append(sb.getElement());
00062                                                 //$(div).append(sb.prevButton.getElement());
00063                                                 //$(div).append(btn);
00064                                                 $('body').append(div);
00065                                                 $(div).dialog({
00066                                                         title: 'Insert Field',
00067                                                         width: 300,
00068                                                         height: $(window).height(),
00069                                                         position: ['right','top'],
00070                                                         zIndex: 9999
00071                                                         
00072                                                         
00073                                                 });
00074                                                 //$('body').append(sb.getElement());
00075                                         });
00076                                 }
00077                         
00078                         }));
00079                         //alert('here');
00080                         editor.ui.addButton('InsertMacro', {
00081                                 label: 'Insert Field',
00082                                 command: pluginName
00083                         });
00084                         
00085                         
00086                         
00090                         editor.addCommand('setrelationship', new CKEDITOR.command(editor, {
00091                         
00092                         
00093                                 exec: function(){
00094                                         //alert(editor.element);
00095                                         var tableName = editor.element.getAttribute('data-xf-htmlreports-tablename');
00096                         
00097                                         XataJax.ready(function(){
00098                                         
00099                                                 
00100                                         
00101                                                 var div = document.createElement('div');
00102                                                 
00103                                                 var sb = new RelationshipSelector({
00104                                                         table: tableName
00105                                                 });
00106                                                 sb.bind('relationshipSelected', function(event){
00107                                                         var element = editor.getSelection().getStartElement();
00108                                                         if ( !element ){
00109                                                                 //alert('no element selected..');
00110                                                                 return;
00111                                                         }
00112                                                         var nativeEl = element.$;
00113                                                         
00114                                                         // See if there is already a relationship attribute
00115                                                         var existing = $(nativeEl).parents('[relationship]');
00116                                                         if ( existing.size() >= 1 ){
00117                                                                 //alert(existing.attr('relationship'));
00118                                                                 var rel = sb.getSelectedRelationship();
00119                                                                 if ( rel ){
00120                                                                         existing.attr('relationship', rel);
00121                                                                 } else {
00122                                                                         existing.removeAttr('relationship');
00123                                                                 }
00124                                                         } else {
00125                                                                 var container = $(nativeEl).parents('ul,ol,table').first();
00126                                                                 if ( container.size() == 0){
00127                                                                         alert('Please click inside a table or list and try again.');
00128                                                                         return;
00129                                                                 }
00130                                                                 var rel = sb.getSelectedRelationship();
00131                                                                 if ( rel ){
00132                                                                         container.attr('relationship', rel);
00133                                                                 } else {
00134                                                                         container.removeAttr('relationship');
00135                                                                 }
00136                                                                 
00137                                                         }
00138                                                         
00139                                                         
00140                                                         
00141                                                 });
00142                                                 sb.update();
00143                                                 
00144                                                 $(div).append(sb.getElement());
00145                                                 //$(div).append(sb.prevButton.getElement());
00146                                                 //$(div).append(btn);
00147                                                 $('body').append(div);
00148                                                 $(div).dialog({
00149                                                         title: 'Select Relationship for Portal',
00150                                                         width: 300,
00151                                                         height: 150,
00152                                                         zIndex: 9999,
00153                                                         modal: true
00154                                                         
00155                                                         
00156                                                 });
00157                                                 
00158                                                 
00159                                                 sb.bind('loaded', function(event){
00160                                                         var element = editor.getSelection().getStartElement();
00161                                                         if ( !element ){
00162                                                                 alert('no element selected..');
00163                                                                 $(div).dialog('close');
00164                                                                 return;
00165                                                         }
00166                                                         var nativeEl = element.$;
00167                                                         
00168                                                         // See if there is already a relationship attribute
00169                                                         var existing = $(nativeEl).parents('[relationship]');
00170                                                         if ( existing.size() >= 1 ){
00171                                                                 //alert(existing.attr('relationship'));
00172                                                                 sb.setSelectedRelationship(existing.attr('relationship'));
00173                                                         }
00174                                                 });
00175                                                 //alert(existing.size());
00176                                                 //$('body').append(sb.getElement());
00177                                         });
00178                                         
00179                                 }
00180                         }));
00181                         
00182                         editor.ui.addButton('setrelationship', {
00183                                 label: 'Set Relationship',
00184                                 command: 'setrelationship'
00185                         });
00186                         
00187                         
00191                         editor.addCommand('validatetemplate', new CKEDITOR.command(editor, {
00192                         
00193                         
00194                                 exec: function(){
00195                                         //alert(editor.element);
00196                                         var tableName = editor.element.getAttribute('data-xf-htmlreports-tablename');
00197                                         
00198                                         var data = editor.getData();
00199                                         
00200                                         var q = {
00201                                                 '-table': tableName,
00202                                                 '--template': data,
00203 
00204                                                 '-action': 'htmlreports_validate_template'
00205                                         };
00206                                         $.post(DATAFACE_SITE_HREF, q, function(response){
00207                                         
00208                                                 try {
00209                                                         if ( response.message){
00210                                                                 alert(response.message);
00211                                                         } else {
00212                                                                 alert('Unspecified server error.  See error log for details.');
00213                                                         }
00214                                                 
00215                                                 } catch (e){
00216                                                         alert(e);
00217                                                 }
00218                                         });
00219                                         
00220                                         
00221                                 }
00222                         }));
00223                         
00224                         editor.ui.addButton('validatetemplate', {
00225                                 label: 'Validate Template',
00226                                 command: 'validatetemplate'
00227                         }); 
00228                         
00229                         
00233                         editor.addCommand('addsectionheader', new CKEDITOR.command(editor, {
00234                         
00235                         
00236                                 exec: function(){
00237                                         //alert(editor.element);
00238                                         var tableName = editor.element.getAttribute('data-xf-htmlreports-tablename');
00239                                         
00240                                         var rootEl = editor.document.$;
00241                                         
00242                                         // First let's see if there is an existing header
00243                                         
00244 
00245                                         var existing = $('div.xf-htmlreports-section-header', rootEl);
00246                                         var htmlToAdd = '<div class="xf-htmlreports-section-header"><h2>Header Text</h2></div>';
00247                                         if ( existing.size() > 0 ){
00248                                                 var last = existing.get(existing.size()-1);
00249                                                 var el = new CKEDITOR.dom.element(last);
00250                                                 var range = new CKEDITOR.dom.range(editor.document);
00251                                                 range.setStartAfter(el);
00252                                                 range.setEndAfter(el);
00253                                                 editor.getSelection().selectRanges([range]);
00254                                                 editor.insertHtml(htmlToAdd);
00255                                                 
00256                                         } else {
00257                                         
00258                                                 editor.document.getBody().append(new CKEDITOR.dom.element($(htmlToAdd).get(0)), true);
00259                                                 /*                                      //alert('here');
00260                                                 var range = new CKEDITOR.dom.range(editor.document);
00261                                                 range.setStart(editor.document, 0);
00262                                                 range.setEnd(editor.document, 0);
00263                                                                                         //alert('here');
00264                                                 editor.getSelection().selectRanges([range]);
00265                                                 
00266                                                 editor.insertHtml(htmlToAdd);
00267                                                 */      
00268                                         }
00269                                         
00270                                         editor.updateElement();
00271                                 }
00272                         }));
00273                         
00274                         
00275                         
00276                         editor.ui.addButton('addsectionheader', {
00277                                 label: 'Add Section Header',
00278                                 command: 'addsectionheader'
00279                         });
00280                         
00281                         
00285                         editor.addCommand('addsectionfooter', new CKEDITOR.command(editor, {
00286                         
00287                         
00288                                 exec: function(){
00289                                         //alert(editor.element);
00290                                         var tableName = editor.element.getAttribute('data-xf-htmlreports-tablename');
00291                                         
00292                                         var rootEl = editor.document.$;
00293                                         
00294                                         // First let's see if there is an existing header
00295                                         
00296                                         
00297                                         var existing = $('div.xf-htmlreports-section-footer', rootEl);
00298                                         var htmlToAdd = '<div class="xf-htmlreports-section-footer"><h3>Footer Text</h3></div>';
00299                                         if ( existing.size() > 0 ){
00300                                                 var last = existing.get(0);
00301                                                 //var prev = $(last).prev();
00302                                                 //if ( prev.size() > 0 ) last = prev.get(0);
00303                                                 
00304                                                 var el = new CKEDITOR.dom.element(last);
00305                                                 var range = new CKEDITOR.dom.range(editor.document);
00306                                                 el.insertBeforeMe(new CKEDITOR.dom.element($(htmlToAdd).get(0)));
00307                                                 /*
00308                                                 range.setStartAfter(el);
00309                                                 range.setEndAfter(el);
00310                                                 editor.getSelection().selectRanges([range]);
00311                                                 editor.insertHtml(htmlToAdd);
00312                                                 */
00313                                                 
00314                                         } else {
00315                                                 //alert($(editor.document.getBody().$).html());
00316                                                 editor.document.getBody().appendHtml(htmlToAdd);
00317                                                 
00318                                                 //editor.document.getDocumentElement().insertAfterMe(new CKEDITOR.dom.element($(htmlToAdd).get(0)));
00319                                                 /*
00320                                                 var range = new CKEDITOR.dom.range(editor.document);
00321                                                 
00322                                                 range.setStartAfter(editor.document.getDocumentElement());
00323                                                 range.setEndAfter(editor.document.getDocumentElement());
00324                                                 editor.getSelection().selectRanges([range]);
00325                                                 editor.insertHtml(htmlToAdd);*/
00326                                         }
00327                                         editor.updateElement();
00328                                         
00329                                         
00330                                 }
00331                         }));
00332                         
00333                         editor.ui.addButton('addsectionfooter', {
00334                                 label: 'Add Section Footer',
00335                                 command: 'addsectionfooter'
00336                         });
00337                         
00338                         
00339                         
00343                         editor.addCommand('previewreport', new CKEDITOR.command(editor, {
00344                         
00345                         
00346                                 exec: function(){
00347                                         //alert(editor.element);
00348                                         var tableName = editor.element.getAttribute('data-xf-htmlreports-tablename');
00349                                         
00350                                         var form = $('<form>')
00351                                                 .attr('method','post')
00352                                                 .attr('target','_blank')
00353                                                 .attr('action', DATAFACE_SITE_HREF)
00354                                                 .append(
00355                                                         $('<input/>')
00356                                                                 .attr('type', 'hidden')
00357                                                                 .attr('name', '-action')
00358                                                                 .attr('value', 'htmlreports_preview_report')
00359                                                 )
00360                                                 .append(
00361                                                         $('<input/>')
00362                                                                 .attr('type', 'hidden')
00363                                                                 .attr('name', '-table')
00364                                                                 .attr('value', tableName)
00365                                                 )
00366                                                 .append(
00367                                                         $('<input/>')
00368                                                                 .attr('type', 'hidden')
00369                                                                 .attr('name', '--template')
00370                                                                 .attr('value', editor.getData())
00371                                                                 
00372                                                 .append(
00373                                                         $('<input/>')
00374                                                                 .attr('type', 'hidden')
00375                                                                 .attr('name', '--css')
00376                                                                 .attr('value', editor.element.getAttribute('data-xf-htmlreports-css'))
00377                                                 )
00378                                         );
00379                                         $('body').append(form);
00380                                         form.submit();
00381                                         /*
00382                                         var q = {
00383                                                 '-action': 'htmlreports_preview_report',
00384                                                 '-table': tableName,
00385                                                 '--template': editor.getData()
00386                                         };
00387                                         
00388                                         $.post(DATAFACE_SITE_HREF, q, function(response){
00389                                                 
00390                                                 var div = document.createElement('div');
00391                                                 $(div).html(response);
00392                                                 $('body').append(div);
00393                                                 $(div).dialog({
00394                                                         title: 'Report Preview',
00395                                                         width: $(window).width()-40,
00396                                                         height: $(window).height()-40,
00397                                                         zIndex: 9999
00398                                                 });
00399                                                 
00400                                                 
00401                                                 
00402                                                                 
00403                                                 
00404                                         });
00405                                         */
00406                                         
00407                                 }
00408                         }));
00409                         
00410                         editor.ui.addButton('previewreport', {
00411                                 label: 'Preview Report',
00412                                 command: 'previewreport'
00413                         });
00414                         
00415                         
00419                         editor.addCommand('previewreporttable', new CKEDITOR.command(editor, {
00420                         
00421                         
00422                                 exec: function(){
00423                                         //alert(editor.element);
00424                                 
00425                                         var tableName = editor.element.getAttribute('data-xf-htmlreports-tablename');
00426                                         
00427                                         
00428                                         var form = $('<form>')
00429                                                 .attr('method','post')
00430                                                 .attr('target','_blank')
00431                                                 .attr('action', DATAFACE_SITE_HREF)
00432                                                 .append(
00433                                                         $('<input/>')
00434                                                                 .attr('type', 'hidden')
00435                                                                 .attr('name', '-action')
00436                                                                 .attr('value', 'htmlreports_preview_report')
00437                                                 )
00438                                                 .append(
00439                                                         $('<input/>')
00440                                                                 .attr('type', 'hidden')
00441                                                                 .attr('name', '-table')
00442                                                                 .attr('value', tableName)
00443                                                 )
00444                                                 .append(
00445                                                         $('<input/>')
00446                                                                 .attr('type', 'hidden')
00447                                                                 .attr('name', '--template')
00448                                                                 .attr('value', editor.getData())
00449                                                                 
00450                                                 )
00451                                                 .append(
00452                                                         $('<input/>')
00453                                                                 .attr('type', 'hidden')
00454                                                                 .attr('name', '--css')
00455                                                                 .attr('value', editor.element.getAttribute('data-xf-htmlreports-css'))
00456                                                 )
00457                                                 .append(
00458                                                         $('<input/>')
00459                                                                 .attr('type', 'hidden')
00460                                                                 .attr('name', '--view')
00461                                                                 .attr('value', 'table')
00462                                                 )
00463                                                 ;
00464                                         $('body').append(form);
00465                                         form.submit();
00466                                         /*
00467                                         var q = {
00468                                                 '-action': 'htmlreports_preview_report',
00469                                                 '-table': tableName,
00470                                                 '--template': editor.getData()
00471                                         };
00472                                         
00473                                         $.post(DATAFACE_SITE_HREF, q, function(response){
00474                                                 
00475                                                 var div = document.createElement('div');
00476                                                 $(div).html(response);
00477                                                 $('body').append(div);
00478                                                 $(div).dialog({
00479                                                         title: 'Report Preview',
00480                                                         width: $(window).width()-40,
00481                                                         height: $(window).height()-40,
00482                                                         zIndex: 9999
00483                                                 });
00484                                                 
00485                                                 
00486                                                 
00487                                                                 
00488                                                 
00489                                         });
00490                                         */
00491                                         
00492                                 }
00493                         }));
00494                         
00495                         editor.ui.addButton('previewreporttable', {
00496                                 label: 'Preview Report as Table',
00497                                 command: 'previewreporttable'
00498                         });
00499                         
00500                 }
00501         
00502         });
00503         
00504         
00505         
00506 })();
 All Data Structures Files Functions Variables Enumerations