Skip to content

Commit

Permalink
Merge pull request #25 from TheDMSGroup/ENG-192
Browse files Browse the repository at this point in the history
[ENG-192] data table loading for doc ready and ajaxComplete
  • Loading branch information
heathdutton authored Apr 5, 2018
2 parents 1358fca + d4ad1ce commit 484bdc1
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 139 deletions.
53 changes: 53 additions & 0 deletions Assets/js/campaign-revenue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Mautic.loadCampaignRevenueWidget = function () {
if (mQuery('#campaign-revenue-table').hasClass('table-initialized') == false) {
mQuery.getScriptCachedOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/js/datatables.min.js', function () {
mQuery.getCssOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/css/datatables.min.css', function () {
mQuery.getCssOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/css/dataTables.fontAwesome.css', function () {
mQuery('#campaign-revenue-table').DataTable(datatableRequest);
}); //getScriptsCachedOnce - fonteawesome css
});//getScriptsCachedOnce - datatables css
}); //getScriptsCachedOnce - datatables js
} // if table initialized
mQuery('#campaign-revenue-table').addClass('table-initialized');
}; //loadCampaignRevenueWidget

// getScriptCachedOnce for faster page loads in the backend.
mQuery.getScriptCachedOnce = function (url, callback) {
if (
typeof window.getScriptCachedOnce !== 'undefined'
&& window.getScriptCachedOnce.indexOf(url) !== -1
) {
callback();
return mQuery(this);
} else {
return mQuery.ajax({
url: url,
dataType: 'script',
cache: true
}).done(function () {
if (typeof window.getScriptCachedOnce === 'undefined') {
window.getScriptCachedOnce = [];
}
window.getScriptCachedOnce.push('url');
callback();
});
}
};

// getScriptCachedOnce for faster page loads in the backend.
mQuery.getCssOnce = function (url, callback) {
if (document.createStyleSheet){
document.createStyleSheet(url);
}
else {
mQuery("head").append(mQuery("<link rel='stylesheet' href='" + url + "' type='text/css' />"));
}
callback();
};

mQuery(document).ready(function () {
Mautic.loadCampaignRevenueWidget();
});
mQuery(document).ajaxComplete(function (event, xhr, settings) {
Mautic.loadCampaignRevenueWidget();
});
16 changes: 13 additions & 3 deletions Assets/js/global-revenue.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mQuery(document).ready(function () {
mQuery.getScriptCachedOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/js/datatables.min.js', function () {
Mautic.loadGlobalRevenueWidget = function () {
if (mQuery('#global-revenue').hasClass('table-initialized') == false) {
mQuery.getScriptCachedOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/js/datatables.min.js', function () {
mQuery.getCssOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/css/datatables.min.css', function () {
mQuery.getCssOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/css/dataTables.fontAwesome.css', function () {
// dependent files loaded, now get the data and render
Expand Down Expand Up @@ -116,7 +117,9 @@ mQuery(document).ready(function () {
}); //getScriptsCachedOnce - fonteawesome css
});//getScriptsCachedOnce - datatables css
}); //getScriptsCachedOnce - datatables js
}); //docready
} // if table initialized
mQuery('#global-revenue').addClass('table-initialized');
}; //loadGlobalRevenueWidget

function renderPublishToggle (id, active) {
if (active == 1) {
Expand Down Expand Up @@ -186,3 +189,10 @@ mQuery.getCssOnce = function (url, callback) {
callback();
};


mQuery(document).ready(function () {
Mautic.loadGlobalRevenueWidget();
});
mQuery(document).ajaxComplete(function (event, xhr, settings) {
Mautic.loadGlobalRevenueWidget();
});
237 changes: 123 additions & 114 deletions Assets/js/source-revenue.js
Original file line number Diff line number Diff line change
@@ -1,128 +1,131 @@
mQuery(document).ready(function () {
mQuery.getScriptCachedOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/js/datatables.min.js', function () {
mQuery.getCssOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/css/datatables.min.css', function () {
mQuery.getCssOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/css/dataTables.fontAwesome.css', function () {
// dependent files loaded, now get the data and render
mQuery.ajax({
url: mauticAjaxUrl,
type: 'POST',
data: {
action: 'plugin:mauticContactLedger:sourceRevenue',
},
cache: true,
dataType: 'json',
success: function (response) {
var rowCount = Math.floor((detailWidgetHeight - 220) / 40);
mQuery('#source-revenue').DataTable({
language:{
emptyTable: "No results found for this date range and filters."
},
data: response.rows,
autoFill: true,
columns: response.columns,
order: [[2, 'asc'], [4, 'asc']],
bLengthChange: false,
lengthMenu: [[rowCount]],
columnDefs: [
{
render: function (data, type, row) {
return renderPublishToggle(row[1], row[0]);
},
targets: 0
Mautic.loadSourceRevenueWidget = function () {
if (mQuery('#source-revenue').hasClass('table-initialized') == false) {
mQuery.getScriptCachedOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/js/datatables.min.js', function () {
mQuery.getCssOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/css/datatables.min.css', function () {
mQuery.getCssOnce(mauticBasePath + '/' + mauticAssetPrefix + 'plugins/MauticContactLedgerBundle/Assets/css/dataTables.fontAwesome.css', function () {
// dependent files loaded, now get the data and render
mQuery.ajax({
url: mauticAjaxUrl,
type: 'POST',
data: {
action: 'plugin:mauticContactLedger:sourceRevenue',
},
cache: true,
dataType: 'json',
success: function (response) {
var rowCount = Math.floor((detailWidgetHeight - 220) / 40);
mQuery('#source-revenue').DataTable({
language: {
emptyTable: "No results found for this date range and filters."
},
{
render: function (data, type, row) {
return renderCampaignName(row);
data: response.rows,
autoFill: true,
columns: response.columns,
order: [[2, 'asc'], [4, 'asc']],
bLengthChange: false,
lengthMenu: [[rowCount]],
columnDefs: [
{
render: function (data, type, row) {
return renderPublishToggle(row[1], row[0]);
},
targets: 0
},
targets: 2
},
{
render: function (data, type, row) {
return renderSourceName(row);
{
render: function (data, type, row) {
return renderCampaignName(row);
},
targets: 2
},
targets: 4
},
{
render: function (data, type, row) {
return '$' + data;
{
render: function (data, type, row) {
return renderSourceName(row);
},
targets: 4
},
targets: [9, 10, 11, 13]
},
{
render: function (data, type, row) {
return data + '%';
{
render: function (data, type, row) {
return '$' + data;
},
targets: [9, 10, 11, 13]
},
targets: 12
},
{visible: false, targets: [1, 3]},
{width: '5%', targets: [0]},
{width: '20%', targets: [2]}
],
{
render: function (data, type, row) {
return data + '%';
},
targets: 12
},
{visible: false, targets: [1, 3]},
{width: '5%', targets: [0]},
{width: '20%', targets: [2]}
],

footerCallback: function (row, data, start, end, display) {
// Add table footer if it doesnt exist
var container = mQuery('#source-revenue');
var columns = data[0].length;
if (mQuery('tr.detailPageTotal').length == 0) {
var footer = mQuery('<tfoot></tfoot>');
var tr = mQuery('<tr class=\'detailPTotal\' style=\'font-weight: 600; background: #fafafa;\'></tr>');
var tr2 = mQuery('<tr class=\'detailGrandTotal\' style=\'font-weight: 600; background: #fafafa;\'></tr>');
tr.append(mQuery('<td colspan=\'3\'>Page totals</td>'));
tr2.append(mQuery('<td colspan=\'3\'>Grand totals</td>'));
for (var i = 2; i < columns; i++) {
tr.append(mQuery('<td class=\'td-right\'></td>'));
tr2.append(mQuery('<td class=\'td-right\'></td>'));
footerCallback: function (row, data, start, end, display) {
// Add table footer if it doesnt exist
var container = mQuery('#source-revenue');
var columns = data[0].length;
if (mQuery('tr.detailPageTotal').length == 0) {
var footer = mQuery('<tfoot></tfoot>');
var tr = mQuery('<tr class=\'detailPTotal\' style=\'font-weight: 600; background: #fafafa;\'></tr>');
var tr2 = mQuery('<tr class=\'detailGrandTotal\' style=\'font-weight: 600; background: #fafafa;\'></tr>');
tr.append(mQuery('<td colspan=\'3\'>Page totals</td>'));
tr2.append(mQuery('<td colspan=\'3\'>Grand totals</td>'));
for (var i = 2; i < columns; i++) {
tr.append(mQuery('<td class=\'td-right\'></td>'));
tr2.append(mQuery('<td class=\'td-right\'></td>'));
}
footer.append(tr);
footer.append(tr2);
container.append(footer);
var tableBody = mQuery('#' + container[0].id + ' tbody');
}
footer.append(tr);
footer.append(tr2);
container.append(footer);
var tableBody = mQuery('#' + container[0].id + ' tbody');
}

if (data && data.length === 0) {
return;
}
try {
var api = this.api();
if (data && data.length === 0) {
return;
}
try {
var api = this.api();

// Remove the formatting to get integer data for
// summation
var intVal = function (i) {
return typeof i === 'string' ? i.replace(/[\$,]/g, '') * 1 : typeof i === 'number' ? i : 0;
};
// Remove the formatting to get integer data for
// summation
var intVal = function (i) {
return typeof i === 'string' ? i.replace(/[\$,]/g, '') * 1 : typeof i === 'number' ? i : 0;
};

var total = mQuery('#' + container[0].id + ' thead th').length;
var footer1 = mQuery(container).find('tfoot tr:nth-child(1)');
var footer2 = mQuery(container).find('tfoot tr:nth-child(2)');
for (var i = 2; i < total; i++) {
var pageSum = api
.column(i+3, {page: 'current'})
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
var sum = api
.column(i+3)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
var title = mQuery(container).find('thead th:nth-child(' + (i+2) + ')').text();
footer1.find('td:nth-child(' + (i) + ')').html(FormatFooter(title, pageSum, i));
footer2.find('td:nth-child(' + (i) + ')').html(FormatFooter(title, sum, i));
var total = mQuery('#' + container[0].id + ' thead th').length;
var footer1 = mQuery(container).find('tfoot tr:nth-child(1)');
var footer2 = mQuery(container).find('tfoot tr:nth-child(2)');
for (var i = 2; i < total; i++) {
var pageSum = api
.column(i + 3, {page: 'current'})
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
var sum = api
.column(i + 3)
.data()
.reduce(function (a, b) {
return intVal(a) + intVal(b);
}, 0);
var title = mQuery(container).find('thead th:nth-child(' + (i + 2) + ')').text();
footer1.find('td:nth-child(' + (i) + ')').html(FormatFooter(title, pageSum, i));
footer2.find('td:nth-child(' + (i) + ')').html(FormatFooter(title, sum, i));
}
}
}
catch (e) {
console.log(e);
}
} // FooterCallback
}); //.DataTables
} //success
}); //ajax
}); //getScriptsCachedOnce - fonteawesome css
});//getScriptsCachedOnce - datatables css
}); //getScriptsCachedOnce - datatables js
}); //docready
catch (e) {
console.log(e);
}
} // FooterCallback
}); //.DataTables
} //success
}); //ajax
}); //getScriptsCachedOnce - fonteawesome css
});//getScriptsCachedOnce - datatables css
}); //getScriptsCachedOnce - datatables js
} // if table initialized
mQuery('#source-revenue').addClass('table-initialized');
}; //loadSourceRevenueWidget

function renderPublishToggle (id, active) {
if (active == 1) {
Expand Down Expand Up @@ -196,3 +199,9 @@ mQuery.getCssOnce = function (url, callback) {
callback();
};

mQuery(document).ready(function () {
Mautic.loadSourceRevenueWidget();
});
mQuery(document).ajaxComplete(function (event, xhr, settings) {
Mautic.loadSourceRevenueWidget();
});
Loading

0 comments on commit 484bdc1

Please sign in to comment.