solidus_admin_insights 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +54 -0
- data/Gemfile +2 -0
- data/LICENSE +26 -0
- data/README.md +71 -0
- data/Rakefile +21 -0
- data/app/assets/javascripts/spree/backend/jquery.tablesorter.min.js +4 -0
- data/app/assets/javascripts/spree/backend/solidus_admin_insights.js +2 -0
- data/app/assets/javascripts/spree/backend/solidus_admin_insights/paginator.js +128 -0
- data/app/assets/javascripts/spree/backend/solidus_admin_insights/report_loader.js +265 -0
- data/app/assets/javascripts/spree/backend/solidus_admin_insights/searcher.js +72 -0
- data/app/assets/javascripts/spree/backend/solidus_admin_insights/table_sorter.js +47 -0
- data/app/assets/javascripts/spree/backend/tmpl.js +87 -0
- data/app/assets/javascripts/spree/frontend/solidus_admin_insights.js +2 -0
- data/app/assets/stylesheets/spree/backend/override_pdf.css +71 -0
- data/app/assets/stylesheets/spree/backend/solidus_admin_insights.css +132 -0
- data/app/assets/stylesheets/spree/frontend/solidus_admin_insights.css +4 -0
- data/app/controllers/spree/admin/insights_controller.rb +103 -0
- data/app/helpers/spree/admin/base_helper_decorator.rb +17 -0
- data/app/models/spree/app_configuration_decorator.rb +3 -0
- data/app/models/spree/product_decorator.rb +3 -0
- data/app/models/spree/promotion_action_decorator.rb +3 -0
- data/app/models/spree/return_authorization_decorator.rb +4 -0
- data/app/permissions/spree/permission_sets/insight_display.rb +9 -0
- data/app/reports/spree/best_selling_products_report.rb +42 -0
- data/app/reports/spree/cart_additions_report.rb +36 -0
- data/app/reports/spree/cart_removals_report.rb +36 -0
- data/app/reports/spree/cart_updations_report.rb +40 -0
- data/app/reports/spree/payment_method_transactions_conversion_rate_report.rb +74 -0
- data/app/reports/spree/payment_method_transactions_conversion_rate_report/payment_method_state_distribution_chart.rb +39 -0
- data/app/reports/spree/payment_method_transactions_report.rb +60 -0
- data/app/reports/spree/payment_method_transactions_report/payment_method_revenue_distribution_chart.rb +36 -0
- data/app/reports/spree/product_views_report.rb +46 -0
- data/app/reports/spree/product_views_to_cart_additions_report.rb +53 -0
- data/app/reports/spree/product_views_to_purchases_report.rb +54 -0
- data/app/reports/spree/promotional_cost_report.rb +84 -0
- data/app/reports/spree/promotional_cost_report/promotional_cost_chart.rb +37 -0
- data/app/reports/spree/promotional_cost_report/usage_count_chart.rb +41 -0
- data/app/reports/spree/report.rb +131 -0
- data/app/reports/spree/report/chart.rb +11 -0
- data/app/reports/spree/report/configuration.rb +40 -0
- data/app/reports/spree/report/date_slicer.rb +61 -0
- data/app/reports/spree/report/observation.rb +49 -0
- data/app/reports/spree/report/query_fragments.rb +45 -0
- data/app/reports/spree/report/query_time_scale.rb +19 -0
- data/app/reports/spree/report/result.rb +100 -0
- data/app/reports/spree/report/timed_observation.rb +47 -0
- data/app/reports/spree/report/timed_result.rb +48 -0
- data/app/reports/spree/returned_products_report.rb +37 -0
- data/app/reports/spree/sales_performance_report.rb +107 -0
- data/app/reports/spree/sales_performance_report/profit_loss_chart.rb +37 -0
- data/app/reports/spree/sales_performance_report/profit_loss_percent_chart.rb +36 -0
- data/app/reports/spree/sales_performance_report/sale_cost_price_chart.rb +48 -0
- data/app/reports/spree/sales_tax_report.rb +64 -0
- data/app/reports/spree/sales_tax_report/monthly_sales_tax_comparison_chart.rb +39 -0
- data/app/reports/spree/shipping_cost_report.rb +89 -0
- data/app/reports/spree/shipping_cost_report/shipping_cost_distribution_chart.rb +38 -0
- data/app/reports/spree/trending_search_report.rb +50 -0
- data/app/reports/spree/trending_search_report/frequency_distribution_pie_chart.rb +41 -0
- data/app/reports/spree/unique_purchases_report.rb +39 -0
- data/app/reports/spree/user_pool_report.rb +66 -0
- data/app/reports/spree/user_pool_report/distribution_column_chart.rb +65 -0
- data/app/reports/spree/users_not_converted_report.rb +48 -0
- data/app/reports/spree/users_who_recently_purchased_report.rb +69 -0
- data/app/services/spree/report_generation_service.rb +27 -0
- data/app/views/spree/admin/insights/_chart.html.erb +4 -0
- data/app/views/spree/admin/insights/download.pdf.erb +27 -0
- data/app/views/spree/admin/insights/index.html.erb +82 -0
- data/app/views/spree/admin/insights/search/_product_views_search.html.erb +13 -0
- data/app/views/spree/admin/insights/search/_search_form.html.erb +39 -0
- data/app/views/spree/admin/insights/search/_trending_searches_search.html.erb +13 -0
- data/app/views/spree/admin/insights/search/_users_not_converted_search.html.erb +13 -0
- data/app/views/spree/admin/insights/search/_users_who_have_not_purchased_recently_search.html.erb +13 -0
- data/app/views/spree/admin/insights/search/_users_who_recently_purchased_search.html.erb +13 -0
- data/app/views/spree/admin/shared/_insights_side_menu.html.erb +5 -0
- data/app/views/spree/admin/shared/sub_menu/_insight.html.erb +7 -0
- data/app/views/spree/admin/templates/insights/_paginator.template +11 -0
- data/app/views/spree/admin/templates/insights/_search.template +76 -0
- data/app/views/spree/admin/templates/insights/_show.template +49 -0
- data/app/views/spree/layouts/pdf.html.erb +9 -0
- data/bin/rails +7 -0
- data/config/initializers/add_to_sidebar.rb +14 -0
- data/config/initializers/assets.rb +1 -0
- data/config/initializers/mime_types.rb +2 -0
- data/config/initializers/wicked_pdf.rb +21 -0
- data/config/locales/en.yml +167 -0
- data/config/routes.rb +6 -0
- data/lib/generators/solidus_admin_insights/install/install_generator.rb +36 -0
- data/lib/generators/solidus_admin_insights/install/solidus_admin_insights.rb +22 -0
- data/lib/solidus_admin_insights.rb +14 -0
- data/lib/solidus_admin_insights/engine.rb +27 -0
- data/lib/solidus_admin_insights/factories.rb +6 -0
- data/solidus_admin_insights.gemspec +42 -0
- data/spec/spec_helper.rb +93 -0
- metadata +419 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 070e7fdbda893fd37283e8fcc9cbdb5b44ede55a
|
4
|
+
data.tar.gz: 332064c9672b762386c9a56d8d5d33f9426491d2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 03062260769865709aacfe5ad0e6fbbaf5020243256e10805092e87ef9a0ed1446d5d1af46d4e5e5fdecc04571359249213fe43c2886b293feca11a66f6805e9
|
7
|
+
data.tar.gz: 6d7c5904dc50c71a7d38f5a8d022c5523f3bc1ed8093f1ab28fd37a12fd789296d2ef112229b5a5c1b4d6fa314d1c0d354f71d33622cf95206eb92d8182e359d
|
data/.gitignore
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
\#*
|
2
|
+
*~
|
3
|
+
.#*
|
4
|
+
.DS_Store
|
5
|
+
.idea
|
6
|
+
.project
|
7
|
+
.sass-cache
|
8
|
+
coverage
|
9
|
+
Gemfile.lock
|
10
|
+
nbproject
|
11
|
+
pkg
|
12
|
+
*.swp
|
13
|
+
spec/dummy
|
14
|
+
|
15
|
+
*.gem
|
16
|
+
*.rbc
|
17
|
+
/.config
|
18
|
+
/InstalledFiles
|
19
|
+
/pkg/
|
20
|
+
/spec/reports/
|
21
|
+
/spec/examples.txt
|
22
|
+
/test/tmp/
|
23
|
+
/test/version_tmp/
|
24
|
+
/tmp/*
|
25
|
+
|
26
|
+
## Specific to RubyMotion:
|
27
|
+
.dat*
|
28
|
+
.repl_history
|
29
|
+
build/
|
30
|
+
|
31
|
+
## Documentation cache and generated files:
|
32
|
+
/.yardoc/
|
33
|
+
/_yardoc/
|
34
|
+
/doc/
|
35
|
+
/rdoc/
|
36
|
+
|
37
|
+
## Environment normalization:
|
38
|
+
/.bundle/
|
39
|
+
/vendor/bundle
|
40
|
+
/lib/bundler/man/
|
41
|
+
|
42
|
+
.rvmrc
|
43
|
+
.ruby-version
|
44
|
+
/public/system
|
45
|
+
.rspec
|
46
|
+
*.sassc
|
47
|
+
*.DS_Store
|
48
|
+
/test
|
49
|
+
/config/*.yml
|
50
|
+
*.log
|
51
|
+
/public/test_files
|
52
|
+
/public/system/*
|
53
|
+
/public/*.json
|
54
|
+
.byebug_history
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Copyright (c) 2017 [Vinsol]
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification,
|
5
|
+
are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
* Neither the name Spree nor the names of its contributors may be used to
|
13
|
+
endorse or promote products derived from this software without specific
|
14
|
+
prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
17
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
18
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
19
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
20
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
21
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
22
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
23
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
SolidusAdminInsights
|
2
|
+
==============
|
3
|
+
|
4
|
+
Dashboard for querying and viewing you solidus store's metrics. Use SolidusEventTracker to capture the metrics
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
Add solidus_admin_insights to your Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'solidus_admin_insights'
|
13
|
+
```
|
14
|
+
|
15
|
+
Bundle your dependencies and run the installation generator:
|
16
|
+
|
17
|
+
```shell
|
18
|
+
bundle
|
19
|
+
bundle exec rails g solidus_admin_insights:install
|
20
|
+
```
|
21
|
+
|
22
|
+
Adding new reports
|
23
|
+
-------------------
|
24
|
+
|
25
|
+
Create a class that inherits from `Spree::Report` and define a `report_query` method. If the report is to be paginated. it should define also define a method called `paginated` and set it to return `true` alongwith defining `paginated_report_query` and `record_count_query`. the `_query` methods should return objects that respond to `to_sql` which returns sql string for reporting query.
|
26
|
+
|
27
|
+
All reports need to define the following constants:
|
28
|
+
1. `SORTABLE_ATTRIBUTES`: Other attributes based on which reports can be sorted.
|
29
|
+
2. `DEFAULT_SORTABLE_ATTRIBUTE`: The attribute which is used by default to sort the report results.
|
30
|
+
3. `HEADERS`: The static header fields for report. Note time based fields are automatically added. Any field not declared here but available in observation will be ignored while displaying the report.
|
31
|
+
4. `SEARCH_ATTRIBUTES`: A hash containing the attributes and their name on frontend based on which report result can be filtered.
|
32
|
+
|
33
|
+
Additionally they need to define two nested classes. `Result` and `Result::Observation`.
|
34
|
+
|
35
|
+
`Result` class can inherit from `Spree::Report::Result` if it is a basic report or from `Spree::Report::TimedResult` if the result can be time scaled(i.e. changing reporting period changes the scale of report).
|
36
|
+
|
37
|
+
Similarly Observation class needs to inherit either from `Spree::Report::Observation` or `Spree::Report::TimedObservation`. It defines a macro call `observation_fields` which can be passed an array or hash with default values of fields which form one report item. Create a method of same name in Observation class for virtual fields which are not returned by queries. ie. average or for formatting db results.
|
38
|
+
|
39
|
+
`TimedResult` has 2 lifecycle methods which can be overriden for customizing the report output.
|
40
|
+
1. `build_empty_observations`: Generates empty observations which are later filled with datapoints returned by report query.
|
41
|
+
2. `populate_observations`: Fill the empty observations with data returned via query.
|
42
|
+
|
43
|
+
`TimedObservation` defines a describes? method which can be overriden to change where the query results gets copied to.
|
44
|
+
|
45
|
+
You can add charts to reports by calling `charts` with a list of classes representing the chart. Each chart implementing class gets the results in it's initializer and need to implement to_h method returning the json representation of chart.
|
46
|
+
|
47
|
+
Finally, register the report in initializer `solidus_admin_insights.rb` in its appropriate category or make a new category to make it available in admin dashboard.
|
48
|
+
|
49
|
+
Testing
|
50
|
+
-------
|
51
|
+
|
52
|
+
First bundle your dependencies, then run `rake`. `rake` will default to building the dummy app if it does not exist, then it will run specs. The dummy app can be regenerated by using `rake test_app`.
|
53
|
+
|
54
|
+
```shell
|
55
|
+
bundle
|
56
|
+
bundle exec rake
|
57
|
+
```
|
58
|
+
|
59
|
+
When testing your applications integration with this extension you may use it's factories.
|
60
|
+
Simply add this require statement to your spec_helper:
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
require 'solidus_admin_insights/factories'
|
64
|
+
```
|
65
|
+
|
66
|
+
Credits
|
67
|
+
-------
|
68
|
+
|
69
|
+
[![vinsol.com: Ruby on Rails, iOS and Android developers](http://vinsol.com/vin_logo.png "Ruby on Rails, iOS and Android developers")](http://vinsol.com)
|
70
|
+
|
71
|
+
Copyright (c) 2017 [vinsol.com](http://vinsol.com "Ruby on Rails, iOS and Android developers"), released under the New MIT License
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'spree/testing_support/extension_rake'
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new
|
8
|
+
|
9
|
+
task :default do
|
10
|
+
if Dir["spec/dummy"].empty?
|
11
|
+
Rake::Task[:test_app].invoke
|
12
|
+
Dir.chdir("../../")
|
13
|
+
end
|
14
|
+
Rake::Task[:spec].invoke
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Generates a dummy app for testing'
|
18
|
+
task :test_app do
|
19
|
+
ENV['LIB_NAME'] = 'solidus_admin_insights'
|
20
|
+
Rake::Task['extension:test_app'].invoke
|
21
|
+
end
|
@@ -0,0 +1,4 @@
|
|
1
|
+
|
2
|
+
(function($){$.extend({tablesorter:new
|
3
|
+
function(){var parsers=[],widgets=[];this.defaults={cssHeader:"header",cssAsc:"headerSortUp",cssDesc:"headerSortDown",cssChildRow:"expand-child",sortInitialOrder:"asc",sortMultiSortKey:"shiftKey",sortForce:null,sortAppend:null,sortLocaleCompare:true,textExtraction:"simple",parsers:{},widgets:[],widgetZebra:{css:["even","odd"]},headers:{},widthFixed:false,cancelSelection:true,sortList:[],headerList:[],dateFormat:"us",decimal:'/\.|\,/g',onRenderHeader:null,selectorHeaders:'thead th',debug:false};function benchmark(s,d){log(s+","+(new Date().getTime()-d.getTime())+"ms");}this.benchmark=benchmark;function log(s){if(typeof console!="undefined"&&typeof console.debug!="undefined"){console.log(s);}else{alert(s);}}function buildParserCache(table,$headers){if(table.config.debug){var parsersDebug="";}if(table.tBodies.length==0)return;var rows=table.tBodies[0].rows;if(rows[0]){var list=[],cells=rows[0].cells,l=cells.length;for(var i=0;i<l;i++){var p=false;if($.metadata&&($($headers[i]).metadata()&&$($headers[i]).metadata().sorter)){p=getParserById($($headers[i]).metadata().sorter);}else if((table.config.headers[i]&&table.config.headers[i].sorter)){p=getParserById(table.config.headers[i].sorter);}if(!p){p=detectParserForColumn(table,rows,-1,i);}if(table.config.debug){parsersDebug+="column:"+i+" parser:"+p.id+"\n";}list.push(p);}}if(table.config.debug){log(parsersDebug);}return list;};function detectParserForColumn(table,rows,rowIndex,cellIndex){var l=parsers.length,node=false,nodeValue=false,keepLooking=true;while(nodeValue==''&&keepLooking){rowIndex++;if(rows[rowIndex]){node=getNodeFromRowAndCellIndex(rows,rowIndex,cellIndex);nodeValue=trimAndGetNodeText(table.config,node);if(table.config.debug){log('Checking if value was empty on row:'+rowIndex);}}else{keepLooking=false;}}for(var i=1;i<l;i++){if(parsers[i].is(nodeValue,table,node)){return parsers[i];}}return parsers[0];}function getNodeFromRowAndCellIndex(rows,rowIndex,cellIndex){return rows[rowIndex].cells[cellIndex];}function trimAndGetNodeText(config,node){return $.trim(getElementText(config,node));}function getParserById(name){var l=parsers.length;for(var i=0;i<l;i++){if(parsers[i].id.toLowerCase()==name.toLowerCase()){return parsers[i];}}return false;}function buildCache(table){if(table.config.debug){var cacheTime=new Date();}var totalRows=(table.tBodies[0]&&table.tBodies[0].rows.length)||0,totalCells=(table.tBodies[0].rows[0]&&table.tBodies[0].rows[0].cells.length)||0,parsers=table.config.parsers,cache={row:[],normalized:[]};for(var i=0;i<totalRows;++i){var c=$(table.tBodies[0].rows[i]),cols=[];if(c.hasClass(table.config.cssChildRow)){cache.row[cache.row.length-1]=cache.row[cache.row.length-1].add(c);continue;}cache.row.push(c);for(var j=0;j<totalCells;++j){cols.push(parsers[j].format(getElementText(table.config,c[0].cells[j]),table,c[0].cells[j]));}cols.push(cache.normalized.length);cache.normalized.push(cols);cols=null;};if(table.config.debug){benchmark("Building cache for "+totalRows+" rows:",cacheTime);}return cache;};function getElementText(config,node){var text="";if(!node)return"";if(!config.supportsTextContent)config.supportsTextContent=node.textContent||false;if(config.textExtraction=="simple"){if(config.supportsTextContent){text=node.textContent;}else{if(node.childNodes[0]&&node.childNodes[0].hasChildNodes()){text=node.childNodes[0].innerHTML;}else{text=node.innerHTML;}}}else{if(typeof(config.textExtraction)=="function"){text=config.textExtraction(node);}else{text=$(node).text();}}return text;}function appendToTable(table,cache){if(table.config.debug){var appendTime=new Date()}var c=cache,r=c.row,n=c.normalized,totalRows=n.length,checkCell=(n[0].length-1),tableBody=$(table.tBodies[0]),rows=[];for(var i=0;i<totalRows;i++){var pos=n[i][checkCell];rows.push(r[pos]);if(!table.config.appender){var l=r[pos].length;for(var j=0;j<l;j++){tableBody[0].appendChild(r[pos][j]);}}}if(table.config.appender){table.config.appender(table,rows);}rows=null;if(table.config.debug){benchmark("Rebuilt table:",appendTime);}applyWidget(table);setTimeout(function(){$(table).trigger("sortEnd");},0);};function buildHeaders(table){if(table.config.debug){var time=new Date();}var meta=($.metadata)?true:false;var header_index=computeTableHeaderCellIndexes(table);$tableHeaders=$(table.config.selectorHeaders,table).each(function(index){this.column=header_index[this.parentNode.rowIndex+"-"+this.cellIndex];this.order=formatSortingOrder(table.config.sortInitialOrder);this.count=this.order;if(checkHeaderMetadata(this)||checkHeaderOptions(table,index))this.sortDisabled=true;if(checkHeaderOptionsSortingLocked(table,index))this.order=this.lockedOrder=checkHeaderOptionsSortingLocked(table,index);if(!this.sortDisabled){var $th=$(this).addClass(table.config.cssHeader);if(table.config.onRenderHeader)table.config.onRenderHeader.apply($th);}table.config.headerList[index]=this;});if(table.config.debug){benchmark("Built headers:",time);log($tableHeaders);}return $tableHeaders;};function computeTableHeaderCellIndexes(t){var matrix=[];var lookup={};var thead=t.getElementsByTagName('THEAD')[0];var trs=thead.getElementsByTagName('TR');for(var i=0;i<trs.length;i++){var cells=trs[i].cells;for(var j=0;j<cells.length;j++){var c=cells[j];var rowIndex=c.parentNode.rowIndex;var cellId=rowIndex+"-"+c.cellIndex;var rowSpan=c.rowSpan||1;var colSpan=c.colSpan||1
|
4
|
+
var firstAvailCol;if(typeof(matrix[rowIndex])=="undefined"){matrix[rowIndex]=[];}for(var k=0;k<matrix[rowIndex].length+1;k++){if(typeof(matrix[rowIndex][k])=="undefined"){firstAvailCol=k;break;}}lookup[cellId]=firstAvailCol;for(var k=rowIndex;k<rowIndex+rowSpan;k++){if(typeof(matrix[k])=="undefined"){matrix[k]=[];}var matrixrow=matrix[k];for(var l=firstAvailCol;l<firstAvailCol+colSpan;l++){matrixrow[l]="x";}}}}return lookup;}function checkCellColSpan(table,rows,row){var arr=[],r=table.tHead.rows,c=r[row].cells;for(var i=0;i<c.length;i++){var cell=c[i];if(cell.colSpan>1){arr=arr.concat(checkCellColSpan(table,headerArr,row++));}else{if(table.tHead.length==1||(cell.rowSpan>1||!r[row+1])){arr.push(cell);}}}return arr;};function checkHeaderMetadata(cell){if(($.metadata)&&($(cell).metadata().sorter===false)){return true;};return false;}function checkHeaderOptions(table,i){if((table.config.headers[i])&&(table.config.headers[i].sorter===false)){return true;};return false;}function checkHeaderOptionsSortingLocked(table,i){if((table.config.headers[i])&&(table.config.headers[i].lockedOrder))return table.config.headers[i].lockedOrder;return false;}function applyWidget(table){var c=table.config.widgets;var l=c.length;for(var i=0;i<l;i++){getWidgetById(c[i]).format(table);}}function getWidgetById(name){var l=widgets.length;for(var i=0;i<l;i++){if(widgets[i].id.toLowerCase()==name.toLowerCase()){return widgets[i];}}};function formatSortingOrder(v){if(typeof(v)!="Number"){return(v.toLowerCase()=="desc")?1:0;}else{return(v==1)?1:0;}}function isValueInArray(v,a){var l=a.length;for(var i=0;i<l;i++){if(a[i][0]==v){return true;}}return false;}function setHeadersCss(table,$headers,list,css){$headers.removeClass(css[0]).removeClass(css[1]);var h=[];$headers.each(function(offset){if(!this.sortDisabled){h[this.column]=$(this);}});var l=list.length;for(var i=0;i<l;i++){h[list[i][0]].addClass(css[list[i][1]]);}}function fixColumnWidth(table,$headers){var c=table.config;if(c.widthFixed){var colgroup=$('<colgroup>');$("tr:first td",table.tBodies[0]).each(function(){colgroup.append($('<col>').css('width',$(this).width()));});$(table).prepend(colgroup);};}function updateHeaderSortCount(table,sortList){var c=table.config,l=sortList.length;for(var i=0;i<l;i++){var s=sortList[i],o=c.headerList[s[0]];o.count=s[1];o.count++;}}function multisort(table,sortList,cache){if(table.config.debug){var sortTime=new Date();}var dynamicExp="var sortWrapper = function(a,b) {",l=sortList.length;for(var i=0;i<l;i++){var c=sortList[i][0];var order=sortList[i][1];var s=(table.config.parsers[c].type=="text")?((order==0)?makeSortFunction("text","asc",c):makeSortFunction("text","desc",c)):((order==0)?makeSortFunction("numeric","asc",c):makeSortFunction("numeric","desc",c));var e="e"+i;dynamicExp+="var "+e+" = "+s;dynamicExp+="if("+e+") { return "+e+"; } ";dynamicExp+="else { ";}var orgOrderCol=cache.normalized[0].length-1;dynamicExp+="return a["+orgOrderCol+"]-b["+orgOrderCol+"];";for(var i=0;i<l;i++){dynamicExp+="}; ";}dynamicExp+="return 0; ";dynamicExp+="}; ";if(table.config.debug){benchmark("Evaling expression:"+dynamicExp,new Date());}eval(dynamicExp);cache.normalized.sort(sortWrapper);if(table.config.debug){benchmark("Sorting on "+sortList.toString()+" and dir "+order+" time:",sortTime);}return cache;};function makeSortFunction(type,direction,index){var a="a["+index+"]",b="b["+index+"]";if(type=='text'&&direction=='asc'){return"("+a+" == "+b+" ? 0 : ("+a+" === null ? Number.POSITIVE_INFINITY : ("+b+" === null ? Number.NEGATIVE_INFINITY : ("+a+" < "+b+") ? -1 : 1 )));";}else if(type=='text'&&direction=='desc'){return"("+a+" == "+b+" ? 0 : ("+a+" === null ? Number.POSITIVE_INFINITY : ("+b+" === null ? Number.NEGATIVE_INFINITY : ("+b+" < "+a+") ? -1 : 1 )));";}else if(type=='numeric'&&direction=='asc'){return"("+a+" === null && "+b+" === null) ? 0 :("+a+" === null ? Number.POSITIVE_INFINITY : ("+b+" === null ? Number.NEGATIVE_INFINITY : "+a+" - "+b+"));";}else if(type=='numeric'&&direction=='desc'){return"("+a+" === null && "+b+" === null) ? 0 :("+a+" === null ? Number.POSITIVE_INFINITY : ("+b+" === null ? Number.NEGATIVE_INFINITY : "+b+" - "+a+"));";}};function makeSortText(i){return"((a["+i+"] < b["+i+"]) ? -1 : ((a["+i+"] > b["+i+"]) ? 1 : 0));";};function makeSortTextDesc(i){return"((b["+i+"] < a["+i+"]) ? -1 : ((b["+i+"] > a["+i+"]) ? 1 : 0));";};function makeSortNumeric(i){return"a["+i+"]-b["+i+"];";};function makeSortNumericDesc(i){return"b["+i+"]-a["+i+"];";};function sortText(a,b){if(table.config.sortLocaleCompare)return a.localeCompare(b);return((a<b)?-1:((a>b)?1:0));};function sortTextDesc(a,b){if(table.config.sortLocaleCompare)return b.localeCompare(a);return((b<a)?-1:((b>a)?1:0));};function sortNumeric(a,b){return a-b;};function sortNumericDesc(a,b){return b-a;};function getCachedSortType(parsers,i){return parsers[i].type;};this.construct=function(settings){return this.each(function(){if(!this.tHead||!this.tBodies)return;var $this,$document,$headers,cache,config,shiftDown=0,sortOrder;this.config={};config=$.extend(this.config,$.tablesorter.defaults,settings);$this=$(this);$.data(this,"tablesorter",config);$headers=buildHeaders(this);this.config.parsers=buildParserCache(this,$headers);cache=buildCache(this);var sortCSS=[config.cssDesc,config.cssAsc];fixColumnWidth(this);$headers.click(function(e){var totalRows=($this[0].tBodies[0]&&$this[0].tBodies[0].rows.length)||0;if(!this.sortDisabled&&totalRows>0){$this.trigger("sortStart");var $cell=$(this);var i=this.column;this.order=this.count++%2;if(this.lockedOrder)this.order=this.lockedOrder;if(!e[config.sortMultiSortKey]){config.sortList=[];if(config.sortForce!=null){var a=config.sortForce;for(var j=0;j<a.length;j++){if(a[j][0]!=i){config.sortList.push(a[j]);}}}config.sortList.push([i,this.order]);}else{if(isValueInArray(i,config.sortList)){for(var j=0;j<config.sortList.length;j++){var s=config.sortList[j],o=config.headerList[s[0]];if(s[0]==i){o.count=s[1];o.count++;s[1]=o.count%2;}}}else{config.sortList.push([i,this.order]);}};setTimeout(function(){setHeadersCss($this[0],$headers,config.sortList,sortCSS);appendToTable($this[0],multisort($this[0],config.sortList,cache));},1);return false;}}).mousedown(function(){if(config.cancelSelection){this.onselectstart=function(){return false};return false;}});$this.bind("update",function(){var me=this;setTimeout(function(){me.config.parsers=buildParserCache(me,$headers);cache=buildCache(me);},1);}).bind("updateCell",function(e,cell){var config=this.config;var pos=[(cell.parentNode.rowIndex-1),cell.cellIndex];cache.normalized[pos[0]][pos[1]]=config.parsers[pos[1]].format(getElementText(config,cell),cell);}).bind("sorton",function(e,list){$(this).trigger("sortStart");config.sortList=list;var sortList=config.sortList;updateHeaderSortCount(this,sortList);setHeadersCss(this,$headers,sortList,sortCSS);appendToTable(this,multisort(this,sortList,cache));}).bind("appendCache",function(){appendToTable(this,cache);}).bind("applyWidgetId",function(e,id){getWidgetById(id).format(this);}).bind("applyWidgets",function(){applyWidget(this);});if($.metadata&&($(this).metadata()&&$(this).metadata().sortlist)){config.sortList=$(this).metadata().sortlist;}if(config.sortList.length>0){$this.trigger("sorton",[config.sortList]);}applyWidget(this);});};this.addParser=function(parser){var l=parsers.length,a=true;for(var i=0;i<l;i++){if(parsers[i].id.toLowerCase()==parser.id.toLowerCase()){a=false;}}if(a){parsers.push(parser);};};this.addWidget=function(widget){widgets.push(widget);};this.formatFloat=function(s){var i=parseFloat(s);return(isNaN(i))?0:i;};this.formatInt=function(s){var i=parseInt(s);return(isNaN(i))?0:i;};this.isDigit=function(s,config){return/^[-+]?\d*$/.test($.trim(s.replace(/[,.']/g,'')));};this.clearTableBody=function(table){if($.browser.msie){function empty(){while(this.firstChild)this.removeChild(this.firstChild);}empty.apply(table.tBodies[0]);}else{table.tBodies[0].innerHTML="";}};}});$.fn.extend({tablesorter:$.tablesorter.construct});var ts=$.tablesorter;ts.addParser({id:"text",is:function(s){return true;},format:function(s){return $.trim(s.toLocaleLowerCase());},type:"text"});ts.addParser({id:"digit",is:function(s,table){var c=table.config;return $.tablesorter.isDigit(s,c);},format:function(s){return $.tablesorter.formatFloat(s);},type:"numeric"});ts.addParser({id:"currency",is:function(s){return/^[£$€?.]/.test(s);},format:function(s){return $.tablesorter.formatFloat(s.replace(new RegExp(/[£$€]/g),""));},type:"numeric"});ts.addParser({id:"ipAddress",is:function(s){return/^\d{2,3}[\.]\d{2,3}[\.]\d{2,3}[\.]\d{2,3}$/.test(s);},format:function(s){var a=s.split("."),r="",l=a.length;for(var i=0;i<l;i++){var item=a[i];if(item.length==2){r+="0"+item;}else{r+=item;}}return $.tablesorter.formatFloat(r);},type:"numeric"});ts.addParser({id:"url",is:function(s){return/^(https?|ftp|file):\/\/$/.test(s);},format:function(s){return jQuery.trim(s.replace(new RegExp(/(https?|ftp|file):\/\//),''));},type:"text"});ts.addParser({id:"isoDate",is:function(s){return/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(s);},format:function(s){return $.tablesorter.formatFloat((s!="")?new Date(s.replace(new RegExp(/-/g),"/")).getTime():"0");},type:"numeric"});ts.addParser({id:"percent",is:function(s){return/\%$/.test($.trim(s));},format:function(s){return $.tablesorter.formatFloat(s.replace(new RegExp(/%/g),""));},type:"numeric"});ts.addParser({id:"usLongDate",is:function(s){return s.match(new RegExp(/^[A-Za-z]{3,10}\.? [0-9]{1,2}, ([0-9]{4}|'?[0-9]{2}) (([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(AM|PM)))$/));},format:function(s){return $.tablesorter.formatFloat(new Date(s).getTime());},type:"numeric"});ts.addParser({id:"shortDate",is:function(s){return/\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4}/.test(s);},format:function(s,table){var c=table.config;s=s.replace(/\-/g,"/");if(c.dateFormat=="us"){s=s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/,"$3/$1/$2");}else if(c.dateFormat=="uk"){s=s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/,"$3/$2/$1");}else if(c.dateFormat=="dd/mm/yy"||c.dateFormat=="dd-mm-yy"){s=s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/,"$1/$2/$3");}return $.tablesorter.formatFloat(new Date(s).getTime());},type:"numeric"});ts.addParser({id:"time",is:function(s){return/^(([0-2]?[0-9]:[0-5][0-9])|([0-1]?[0-9]:[0-5][0-9]\s(am|pm)))$/.test(s);},format:function(s){return $.tablesorter.formatFloat(new Date("2000/01/01 "+s).getTime());},type:"numeric"});ts.addParser({id:"metadata",is:function(s){return false;},format:function(s,table,cell){var c=table.config,p=(!c.parserMetadataName)?'sortValue':c.parserMetadataName;return $(cell).metadata()[p];},type:"numeric"});ts.addWidget({id:"zebra",format:function(table){if(table.config.debug){var time=new Date();}var $tr,row=-1,odd;$("tr:visible",table.tBodies[0]).each(function(i){$tr=$(this);if(!$tr.hasClass(table.config.cssChildRow))row++;odd=(row%2==0);$tr.removeClass(table.config.widgetZebra.css[odd?0:1]).addClass(table.config.widgetZebra.css[odd?1:0])});if(table.config.debug){$.tablesorter.benchmark("Applying Zebra widget",time);}}});})(jQuery);
|
@@ -0,0 +1,128 @@
|
|
1
|
+
function Paginator(inputs, reportLoader) {
|
2
|
+
this.$insightsTableList = inputs.insightsDiv;
|
3
|
+
this.paginatorDiv = inputs.paginatorDiv;
|
4
|
+
this.tableSorter = inputs.tableSorterObject;
|
5
|
+
this.removePaginationButton = inputs.removePaginationButton;
|
6
|
+
this.reportLoader = reportLoader;
|
7
|
+
this.applyPaginationButton = inputs.applyPaginationButton;
|
8
|
+
}
|
9
|
+
|
10
|
+
Paginator.prototype.bindEvents = function () {
|
11
|
+
var _this = this;
|
12
|
+
this.paginatorDiv.on('click', '.pagination-link', function (event) {
|
13
|
+
event.preventDefault();
|
14
|
+
_this.loadPaginationData(event);
|
15
|
+
});
|
16
|
+
|
17
|
+
this.reportLoader.perPageSelector.on('change', function(event) {
|
18
|
+
_this.togglePaginatorButtons(_this.removePaginationButton, _this.applyPaginationButton);
|
19
|
+
_this.loadReportData(event);
|
20
|
+
});
|
21
|
+
|
22
|
+
this.removePaginationButton.on('click', function(event) {
|
23
|
+
_this.togglePaginatorButtons(_this.applyPaginationButton, _this.removePaginationButton);
|
24
|
+
event.preventDefault();
|
25
|
+
_this.removePagination(this);
|
26
|
+
});
|
27
|
+
|
28
|
+
this.applyPaginationButton.on('click', function(event) {
|
29
|
+
_this.togglePaginatorButtons(_this.removePaginationButton, _this.applyPaginationButton);
|
30
|
+
event.preventDefault();
|
31
|
+
_this.applyPagination(event);
|
32
|
+
});
|
33
|
+
};
|
34
|
+
|
35
|
+
Paginator.prototype.togglePaginatorButtons = function(firstButton, secondButton) {
|
36
|
+
firstButton.closest('span').removeClass('hide');
|
37
|
+
secondButton.closest('span').addClass('hide');
|
38
|
+
};
|
39
|
+
|
40
|
+
Paginator.prototype.refreshPaginator = function(data) {
|
41
|
+
this.reportLoader.perPageSelector.val(data['per_page']);
|
42
|
+
this.populatePaginationData(data);
|
43
|
+
};
|
44
|
+
|
45
|
+
Paginator.prototype.loadPaginationData = function (event) {
|
46
|
+
var $element = $(event.target),
|
47
|
+
sorted_attributes = this.tableSorter.fetchSortedAttribute(),
|
48
|
+
attribute = sorted_attributes[0],
|
49
|
+
sortOrder = sorted_attributes[1],
|
50
|
+
requestPath = $element.attr('href') + '&sort%5Battribute%5D=' + attribute + '&sort%5Btype%5D=' + sortOrder,
|
51
|
+
_this = this;
|
52
|
+
_this.reportLoader.requestUrl = requestPath;
|
53
|
+
|
54
|
+
if (!($element.parents('li').hasClass('active'))) {
|
55
|
+
$.ajax({
|
56
|
+
type: 'GET',
|
57
|
+
url: requestPath,
|
58
|
+
dataType: 'json',
|
59
|
+
success: function(data) {
|
60
|
+
_this.populateInsightsData(data);
|
61
|
+
_this.paginatorDiv.find('.active').removeClass('active');
|
62
|
+
$element.parents('li').addClass('active');
|
63
|
+
}
|
64
|
+
});
|
65
|
+
}
|
66
|
+
};
|
67
|
+
|
68
|
+
Paginator.prototype.populateInsightsData = function(data) {
|
69
|
+
this.reportLoader.populateInsightsData(data);
|
70
|
+
};
|
71
|
+
|
72
|
+
Paginator.prototype.populatePaginationData = function(data) {
|
73
|
+
var $templateData = $(tmpl('paginator-tmpl', data));
|
74
|
+
this.paginatorDiv.empty().append($templateData);
|
75
|
+
this.pageLinks = this.paginatorDiv.find('.pagination-link');
|
76
|
+
};
|
77
|
+
|
78
|
+
Paginator.prototype.loadReportData = function(event) {
|
79
|
+
var $element = $(event.target),
|
80
|
+
sorted_attributes = this.tableSorter.fetchSortedAttribute(),
|
81
|
+
attribute = sorted_attributes[0],
|
82
|
+
sortOrder = sorted_attributes[1],
|
83
|
+
requestUrl = $element.data('url') + '&sort%5Battribute%5D=' + attribute + '&sort%5Btype%5D=' + sortOrder + '&' + $('#filter-search').serialize() + '&per_page=' + $element.val();
|
84
|
+
$element.data('url', requestUrl);
|
85
|
+
this.reportLoader.loadChart($element);
|
86
|
+
};
|
87
|
+
|
88
|
+
Paginator.prototype.removePagination = function(currentElement) {
|
89
|
+
var $element = this.reportLoader.perPageSelector,
|
90
|
+
_this = this,
|
91
|
+
sorted_attributes = this.tableSorter.fetchSortedAttribute(),
|
92
|
+
attribute = sorted_attributes[0],
|
93
|
+
sortOrder = sorted_attributes[1],
|
94
|
+
requestUrl = $element.data('url') + '&sort%5Battribute%5D=' + attribute + '&sort%5Btype%5D=' + sortOrder + '&' + $('#filter-search').serialize() + '&paginate=false';
|
95
|
+
$(currentElement).attr('href', requestUrl);
|
96
|
+
_this.reportLoader.requestUrl = requestUrl;
|
97
|
+
$element.val('');
|
98
|
+
$.ajax({
|
99
|
+
type: 'GET',
|
100
|
+
url: requestUrl,
|
101
|
+
dataType: 'json',
|
102
|
+
success: function(data) {
|
103
|
+
_this.populateInsightsData(data);
|
104
|
+
_this.paginatorDiv.empty();
|
105
|
+
}
|
106
|
+
});
|
107
|
+
};
|
108
|
+
|
109
|
+
Paginator.prototype.applyPagination = function(currentElement) {
|
110
|
+
var $element = this.reportLoader.perPageSelector,
|
111
|
+
_this = this,
|
112
|
+
sorted_attributes = this.tableSorter.fetchSortedAttribute(),
|
113
|
+
attribute = sorted_attributes[0],
|
114
|
+
sortOrder = sorted_attributes[1],
|
115
|
+
requestUrl = $element.data('url') + '&sort%5Battribute%5D=' + attribute + '&sort%5Btype%5D=' + sortOrder + '&' + $('#filter-search').serialize();
|
116
|
+
$(currentElement).attr('href', requestUrl);
|
117
|
+
_this.reportLoader.requestUrl = requestUrl;
|
118
|
+
$element.val('5');
|
119
|
+
$.ajax({
|
120
|
+
type: 'GET',
|
121
|
+
url: requestUrl,
|
122
|
+
dataType: 'json',
|
123
|
+
success: function(data) {
|
124
|
+
_this.populateInsightsData(data);
|
125
|
+
_this.populatePaginationData(data);
|
126
|
+
}
|
127
|
+
});
|
128
|
+
};
|
@@ -0,0 +1,265 @@
|
|
1
|
+
//= require spree/backend/solidus_admin_insights/paginator
|
2
|
+
//= require spree/backend/solidus_admin_insights/searcher
|
3
|
+
//= require spree/backend/solidus_admin_insights/table_sorter
|
4
|
+
|
5
|
+
function ReportLoader(inputs) {
|
6
|
+
this.$selectList = inputs.reportsSelectBox;
|
7
|
+
this.$insightsTableList = inputs.insightsDiv;
|
8
|
+
this.$narrowDownData = inputs.narrowDownData;
|
9
|
+
this.tableHelpers = inputs.tableHelpers;
|
10
|
+
this.pageSelector = inputs.tableHelpers.find('#page-selector');
|
11
|
+
this.perPageSelector = this.tableHelpers.find('#per_page');
|
12
|
+
this.pageHelpers = this.tableHelpers.find('#page-helpers');
|
13
|
+
this.resetButton = inputs.resetButton;
|
14
|
+
this.downloadButton = inputs.downloadButton;
|
15
|
+
this.refreshButton = inputs.refreshButton;
|
16
|
+
this.filterDiv = inputs.filterDiv;
|
17
|
+
this.paginatorDiv = inputs.paginatorDiv;
|
18
|
+
this.removePaginationButton = inputs.removePaginationButton;
|
19
|
+
this.applyPaginationButton = inputs.applyPaginationButton;
|
20
|
+
this.chartContainer = inputs.chartContainer;
|
21
|
+
this.downloadLinks = inputs.downloadLinks;
|
22
|
+
this.requestUrl = '';
|
23
|
+
this.isStatePushable = true;
|
24
|
+
this.tableSorterObject = null;
|
25
|
+
this.searcherObject = null;
|
26
|
+
this.paginatorObject = null;
|
27
|
+
}
|
28
|
+
|
29
|
+
ReportLoader.prototype.init = function() {
|
30
|
+
var tableSorterInputs = {
|
31
|
+
$insightsTable: this.$insightsTableList,
|
32
|
+
reportLoader: this,
|
33
|
+
paginatorDiv: this.paginatorDiv
|
34
|
+
};
|
35
|
+
this.tableSorterObject = new TableSorter(tableSorterInputs);
|
36
|
+
this.tableSorterObject.bindEvents();
|
37
|
+
|
38
|
+
var searcherInputs = {
|
39
|
+
filterDiv: this.filterDiv,
|
40
|
+
insightsDiv: this.$insightsTableList,
|
41
|
+
tableSorterObject: this.tableSorterObject
|
42
|
+
};
|
43
|
+
this.searcherObject = new Searcher(searcherInputs, this);
|
44
|
+
this.searcherObject.bindEvents();
|
45
|
+
|
46
|
+
var paginatorInputs = {
|
47
|
+
paginatorDiv: this.paginatorDiv,
|
48
|
+
insightsDiv: this.$insightsTableList,
|
49
|
+
tableSorterObject: this.tableSorterObject,
|
50
|
+
removePaginationButton: this.removePaginationButton,
|
51
|
+
applyPaginationButton: this.applyPaginationButton
|
52
|
+
};
|
53
|
+
this.paginatorObject = new Paginator(paginatorInputs, this);
|
54
|
+
this.paginatorObject.bindEvents();
|
55
|
+
this.setDefaultReport();
|
56
|
+
};
|
57
|
+
|
58
|
+
ReportLoader.prototype.setDefaultReport = function() {
|
59
|
+
if(location.pathname == '/admin/insights') {
|
60
|
+
this.$selectList.val($(this.$selectList.find('option')[1]).val()).change();
|
61
|
+
window.history.pushState({}, '', this.$selectList.find(':selected').data('url'));
|
62
|
+
}
|
63
|
+
};
|
64
|
+
|
65
|
+
ReportLoader.prototype.bindEvents = function() {
|
66
|
+
var _this = this;
|
67
|
+
_this.$selectList.on('change', function() {
|
68
|
+
$(this).find('option').first().attr('disabled', true);
|
69
|
+
_this.paginatorObject.togglePaginatorButtons(_this.paginatorObject.removePaginationButton, _this.paginatorObject.applyPaginationButton);
|
70
|
+
_this.loadChart($(this).find(':selected'));
|
71
|
+
});
|
72
|
+
|
73
|
+
this.resetButton.on('click', function() {
|
74
|
+
_this.resetFilters(event);
|
75
|
+
});
|
76
|
+
|
77
|
+
this.refreshButton.on('click', function() {
|
78
|
+
_this.refreshPage(event);
|
79
|
+
});
|
80
|
+
|
81
|
+
this.downloadButton.on('click', function() {
|
82
|
+
_this.toggleDownloadLinks(event, this);
|
83
|
+
});
|
84
|
+
|
85
|
+
this.$narrowDownData.on('keyup', function() {
|
86
|
+
var value = $(this).val();
|
87
|
+
var pattern = new RegExp(value, "i");
|
88
|
+
|
89
|
+
_this.$insightsTableList.find('tbody > tr').each(function() {
|
90
|
+
if (!($(this).find('td').text().search(pattern) >= 0)) {
|
91
|
+
$(this).hide();
|
92
|
+
}
|
93
|
+
if (($(this).find('td').text().search(pattern) >= 0)) {
|
94
|
+
$(this).show();
|
95
|
+
}
|
96
|
+
});
|
97
|
+
|
98
|
+
});
|
99
|
+
|
100
|
+
$('body').on('click', function(event) {
|
101
|
+
_this.downloadButton.removeClass('open');
|
102
|
+
event.stopPropagation();
|
103
|
+
});
|
104
|
+
|
105
|
+
_this.bindPopStateEventCallback();
|
106
|
+
};
|
107
|
+
|
108
|
+
ReportLoader.prototype.toggleDownloadLinks = function(event, element) {
|
109
|
+
$(element).toggleClass('open');
|
110
|
+
event.preventDefault();
|
111
|
+
event.stopPropagation();
|
112
|
+
};
|
113
|
+
|
114
|
+
ReportLoader.prototype.resetFilters = function(event) {
|
115
|
+
event.preventDefault();
|
116
|
+
var $element = $(event.target),
|
117
|
+
paginate = this.removePaginationButton.closest('span').hasClass('hide');
|
118
|
+
$element.attr('href', this.perPageSelector.data('url') + '&paginate=' + paginate);
|
119
|
+
$element.data('url', this.perPageSelector.data('url') + '&paginate=' + paginate);
|
120
|
+
this.loadChart($element);
|
121
|
+
};
|
122
|
+
|
123
|
+
ReportLoader.prototype.refreshPage = function(event) {
|
124
|
+
event.preventDefault();
|
125
|
+
var $element = $(event.target);
|
126
|
+
$element.attr('href', location.href);
|
127
|
+
$element.data('url', location.href);
|
128
|
+
this.loadChart($element);
|
129
|
+
};
|
130
|
+
|
131
|
+
ReportLoader.prototype.bindPopStateEventCallback = function() {
|
132
|
+
var _this = this;
|
133
|
+
window.onpopstate = function(event) {
|
134
|
+
event.state ? (report_name = event.state['report_name'] || '') : (report_name = '');
|
135
|
+
_this.$selectList.val(report_name);
|
136
|
+
_this.$selectList.select2('val', report_name);
|
137
|
+
var $selectedOption = _this.$selectList.find(':selected');
|
138
|
+
_this.fetchChartDataWithoutState(location.href, $selectedOption);
|
139
|
+
};
|
140
|
+
};
|
141
|
+
|
142
|
+
ReportLoader.prototype.loadChart = function($selectedOption) {
|
143
|
+
var requestPath = $selectedOption.data('url');
|
144
|
+
this.fetchChartData(requestPath, $selectedOption);
|
145
|
+
};
|
146
|
+
|
147
|
+
ReportLoader.prototype.fetchChartData = function(url, $selectedOption) {
|
148
|
+
var _this = this;
|
149
|
+
_this.requestUrl = url;
|
150
|
+
$.ajax({
|
151
|
+
type: 'GET',
|
152
|
+
url: url,
|
153
|
+
dataType: 'json',
|
154
|
+
cache: false,
|
155
|
+
success: function(data) {
|
156
|
+
(_this.isStatePushable ? _this.populateInsightsData(data) : _this.populateInsightsDataWithoutState(data))
|
157
|
+
if(data.headers != undefined) {
|
158
|
+
_this.tableHelpers.removeClass('hide');
|
159
|
+
|
160
|
+
if(!data.pagination_required) {
|
161
|
+
_this.pageSelector.addClass('hide');
|
162
|
+
$.each(_this.pageHelpers.find('.col-md-2'), function(index, object) {
|
163
|
+
$(object).removeClass('col-md-2').addClass('col-md-3');
|
164
|
+
});
|
165
|
+
} else {
|
166
|
+
_this.pageSelector.removeClass('hide');
|
167
|
+
$.each(_this.pageHelpers.find('.col-md-3'), function(index, object) {
|
168
|
+
$(object).removeClass('col-md-3').addClass('col-md-2');
|
169
|
+
});
|
170
|
+
}
|
171
|
+
_this.perPageSelector.data('url', data['request_path'] + '?report_category=' + data['report_category']);
|
172
|
+
_this.setDownloadLinksPath();
|
173
|
+
_this.searcherObject.refreshSearcher($selectedOption, data);
|
174
|
+
_this.paginatorObject.refreshPaginator(data);
|
175
|
+
if(data.searched_fields != undefined)
|
176
|
+
_this.searcherObject.fillFormFields(data.searched_fields);
|
177
|
+
}
|
178
|
+
}
|
179
|
+
});
|
180
|
+
};
|
181
|
+
|
182
|
+
ReportLoader.prototype.buildChart = function(data) {
|
183
|
+
var chart_container = $('#chart-container');
|
184
|
+
if ((data['chart_json'] != undefined) && (data['chart_json']['chart'])) {
|
185
|
+
chart_container.empty().removeClass('hidden');
|
186
|
+
$.each(data['chart_json']['charts'], function(index, chart) {
|
187
|
+
var chart_div = $('<div>', { id: chart['id'] });
|
188
|
+
chart_container.append(chart_div);
|
189
|
+
chart_div.highcharts(chart['json']);
|
190
|
+
});
|
191
|
+
} else {
|
192
|
+
chart_container.addClass('hidden');
|
193
|
+
}
|
194
|
+
};
|
195
|
+
|
196
|
+
ReportLoader.prototype.fetchChartDataWithoutState = function(url, $selectedOption) {
|
197
|
+
this.isStatePushable = false;
|
198
|
+
this.fetchChartData(url, $selectedOption);
|
199
|
+
};
|
200
|
+
|
201
|
+
ReportLoader.prototype.populateInsightsData = function(data) {
|
202
|
+
if(data.headers != undefined) {
|
203
|
+
var $templateData = $(tmpl('tmpl', data));
|
204
|
+
this.$insightsTableList.empty().append($templateData);
|
205
|
+
this.buildChart(data);
|
206
|
+
} else {
|
207
|
+
this.$insightsTableList.empty();
|
208
|
+
this.paginatorDiv.empty();
|
209
|
+
this.filterDiv.addClass('hide');
|
210
|
+
this.chartContainer.addClass('hidden');
|
211
|
+
}
|
212
|
+
if(this.isStatePushable) {
|
213
|
+
this.pushUrlToHistory();
|
214
|
+
} else {
|
215
|
+
this.isStatePushable = true;
|
216
|
+
}
|
217
|
+
};
|
218
|
+
|
219
|
+
ReportLoader.prototype.setDownloadLinksPath = function($selectedOption) {
|
220
|
+
var _this = this;
|
221
|
+
$.each(this.downloadLinks, function() {
|
222
|
+
$(this).attr('href', $(this).data('url') + '?id=' + _this.$selectList.val() + '&paginate=false');
|
223
|
+
});
|
224
|
+
};
|
225
|
+
|
226
|
+
ReportLoader.prototype.populateInsightsDataWithoutState = function(data) {
|
227
|
+
this.isStatePushable = false;
|
228
|
+
this.populateInsightsData(data);
|
229
|
+
};
|
230
|
+
|
231
|
+
ReportLoader.prototype.pushUrlToHistory = function() {
|
232
|
+
var report_name = this.$selectList.find(':selected').val();
|
233
|
+
window.history.pushState({ report_name: report_name }, '', this.requestUrl);
|
234
|
+
this.requestUrl = '';
|
235
|
+
};
|
236
|
+
|
237
|
+
ReportLoader.prototype.populateInitialData = function() {
|
238
|
+
var $selectedOption = this.$selectList.find(':selected');
|
239
|
+
this.fetchChartDataWithoutState(location.href, $selectedOption);
|
240
|
+
};
|
241
|
+
|
242
|
+
$(function() {
|
243
|
+
var is_admin_insights_page = $("[data-hook='admin-insights']").length;
|
244
|
+
if (is_admin_insights_page) {
|
245
|
+
var inputs = {
|
246
|
+
insightsDiv: $('#report-div'),
|
247
|
+
reportsSelectBox: $('#reports'),
|
248
|
+
resetButton: $('#reset'),
|
249
|
+
refreshButton: $('#refresh'),
|
250
|
+
removePaginationButton: $('#remove-pagination'),
|
251
|
+
applyPaginationButton: $('#apply-pagination'),
|
252
|
+
tableHelpers: $('#table-helpers'),
|
253
|
+
filterDiv: $('#search-div'),
|
254
|
+
paginatorDiv: $('#paginator-div'),
|
255
|
+
narrowDownData: $('#narrow-down-report-data'),
|
256
|
+
chartContainer: $('#chart-container'),
|
257
|
+
downloadLinks: $('.download-link'),
|
258
|
+
downloadButton: $('.toggle-btn')
|
259
|
+
},
|
260
|
+
reportLoader = new ReportLoader(inputs);
|
261
|
+
reportLoader.init();
|
262
|
+
reportLoader.bindEvents();
|
263
|
+
reportLoader.populateInitialData();
|
264
|
+
}
|
265
|
+
});
|