spree_admin_insights 3.2.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 +6 -0
- data/LICENSE +26 -0
- data/README.md +96 -0
- data/Rakefile +21 -0
- data/app/assets/javascripts/spree/backend/jquery.tablesorter.min.js +4 -0
- data/app/assets/javascripts/spree/backend/spree_admin_insights.js +2 -0
- data/app/assets/javascripts/spree/backend/spree_admin_insights/paginator.js +128 -0
- data/app/assets/javascripts/spree/backend/spree_admin_insights/report_loader.js +228 -0
- data/app/assets/javascripts/spree/backend/spree_admin_insights/searcher.js +103 -0
- data/app/assets/javascripts/spree/backend/spree_admin_insights/table_sorter.js +47 -0
- data/app/assets/javascripts/spree/backend/tmpl.js +87 -0
- data/app/assets/javascripts/spree/frontend/spree_admin_insights.js +2 -0
- data/app/assets/stylesheets/spree/backend/override_pdf.css +71 -0
- data/app/assets/stylesheets/spree/backend/spree_admin_insights.css +40 -0
- data/app/assets/stylesheets/spree/frontend/spree_admin_insights.css +4 -0
- data/app/controllers/spree/admin/insights_controller.rb +112 -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/models/spree/user_decorator.rb +3 -0
- data/app/overrides/add_insights_to_admin_side_menu.html.rb +5 -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 +91 -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 +16 -0
- data/app/views/spree/admin/templates/insights/_paginator.template +9 -0
- data/app/views/spree/admin/templates/insights/_search.template +75 -0
- data/app/views/spree/admin/templates/insights/_show.template +48 -0
- data/app/views/spree/layouts/pdf.html.erb +9 -0
- data/bin/rails +7 -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 +169 -0
- data/config/locales/pl.yml +164 -0
- data/config/routes.rb +6 -0
- data/lib/generators/spree_admin_insights/install/install_generator.rb +35 -0
- data/lib/generators/spree_admin_insights/install/spree_admin_insights.rb +22 -0
- data/lib/spree_admin_insights.rb +14 -0
- data/lib/spree_admin_insights/engine.rb +26 -0
- data/lib/spree_admin_insights/factories.rb +6 -0
- data/spec/spec_helper.rb +93 -0
- data/spree_admin_insights.gemspec +41 -0
- metadata +420 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 448690ee08fa5218a6d4a61ef6fb1c8ab3c52a7a
|
4
|
+
data.tar.gz: 441e1c52b66073bbd2394a699e6713f3e1f273fa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 78234f82a75305b4db2a4c0591256ebedaf85bfa98b42e79c93b10bc5119e9a00177badc2af295e6441aa901d6ab00f4466066d697c7489f8ab194e78789a6f9
|
7
|
+
data.tar.gz: d83d608a57b958cb8397db77b0854fd7ca8bbe195b68931563682e3787aacd9967e80b06a59ab773e7df2a4c5587ea53a96cad4abe2dba6f403b1e137ec71e8c
|
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) 2016 [name of plugin creator]
|
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,96 @@
|
|
1
|
+
[SpreeAdminInsights](http://vinsol.com/spreecommerce-admin-insights)
|
2
|
+
==============
|
3
|
+
|
4
|
+
When it comes to driving an Ecommerce business, knowing the right metrics and access to relevant data is half the battle won! This allows you to take immediate and more importantly, the right action.
|
5
|
+
|
6
|
+
This extension provides extensive and targeted reports for the Admin. Which products were viewed the most yesterday, which brand is most popular in a particular geography, which user is a consistent buyer and much more, all the reports a website owner could probably need are a click away!
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
Features
|
11
|
+
--------
|
12
|
+
Elaborate reporting from the following categories are available:
|
13
|
+
* **Financial Analysis -** Involves reports around sales, payment methods and shipping etc
|
14
|
+
* **Product Analysis -** Insights of product purchase, abandoned cart etc
|
15
|
+
* **Promotional analysis -** Reports of promotional costs etc are available.
|
16
|
+
* **Search Analysis -** Search details reports.
|
17
|
+
* **User Analysis -** Includes elaborate user analysis.
|
18
|
+
|
19
|
+
**Other features include :**
|
20
|
+
* Search and Filter
|
21
|
+
* Save reports in various formats.
|
22
|
+
* Refresh reports
|
23
|
+
* Reset report
|
24
|
+
* Remove pagination or change pagination count.
|
25
|
+
|
26
|
+
Installation
|
27
|
+
------------
|
28
|
+
|
29
|
+
1. Add spree_admin_insights to your Gemfile:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
gem 'spree_events_tracker'
|
33
|
+
gem 'spree_admin_insights'
|
34
|
+
```
|
35
|
+
|
36
|
+
2. Bundle your dependencies and run the installation generator:
|
37
|
+
|
38
|
+
```shell
|
39
|
+
bundle
|
40
|
+
bundle exec rails g spree_events_tracker:install # Ensure event tracker files are installed
|
41
|
+
bundle exec rails g spree_admin_insights:install
|
42
|
+
```
|
43
|
+
|
44
|
+
3. Restart your server
|
45
|
+
|
46
|
+
Usage
|
47
|
+
-------
|
48
|
+
Once installed it will automatically starts all data loging and statistical analysis and provides you a user friendly graphical representation of reports. This extension also allows you to download the reports in multiple formats. For more detailed usage please see [this](http://vinsol.com/spreecommerce-admin-insights) blog.
|
49
|
+
|
50
|
+
To access these reports goto admin section and click on 'Insights' section in the vertical menu bar.
|
51
|
+
|
52
|
+
|
53
|
+
Adding new reports
|
54
|
+
-------------------
|
55
|
+
|
56
|
+
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.
|
57
|
+
|
58
|
+
All reports need to define the following constants:
|
59
|
+
1. `SORTABLE_ATTRIBUTES`: Other attributes based on which reports can be sorted.
|
60
|
+
2. `DEFAULT_SORTABLE_ATTRIBUTE`: The attribute which is used by default to sort the report results.
|
61
|
+
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.
|
62
|
+
4. `SEARCH_ATTRIBUTES`: A hash containing the attributes and their name on frontend based on which report result can be filtered.
|
63
|
+
|
64
|
+
Additionally they need to define two nested classes. `Result` and `Result::Observation`.
|
65
|
+
|
66
|
+
`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).
|
67
|
+
|
68
|
+
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.
|
69
|
+
|
70
|
+
`TimedResult` has 2 lifecycle methods which can be overriden for customizing the report output.
|
71
|
+
1. `build_empty_observations`: Generates empty observations which are later filled with datapoints returned by report query.
|
72
|
+
2. `populate_observations`: Fill the empty observations with data returned via query.
|
73
|
+
|
74
|
+
`TimedObservation` defines a describes? method which can be overriden to change where the query results gets copied to.
|
75
|
+
|
76
|
+
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.
|
77
|
+
|
78
|
+
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.
|
79
|
+
|
80
|
+
|
81
|
+
Testing
|
82
|
+
-------
|
83
|
+
|
84
|
+
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`.
|
85
|
+
|
86
|
+
```shell
|
87
|
+
bundle
|
88
|
+
bundle exec rspec spec
|
89
|
+
```
|
90
|
+
|
91
|
+
Credits
|
92
|
+
-------
|
93
|
+
|
94
|
+
[![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)
|
95
|
+
|
96
|
+
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'] = 'spree_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,228 @@
|
|
1
|
+
//= require spree/backend/spree_admin_insights/paginator
|
2
|
+
//= require spree/backend/spree_admin_insights/searcher
|
3
|
+
//= require spree/backend/spree_admin_insights/table_sorter
|
4
|
+
|
5
|
+
function ReportLoader(inputs) {
|
6
|
+
this.$selectList = inputs.reportsSelectBox;
|
7
|
+
this.$insightsTableList = inputs.insightsDiv;
|
8
|
+
this.tableHelpers = inputs.tableHelpers;
|
9
|
+
this.pageSelector = inputs.tableHelpers.find('#page-selector');
|
10
|
+
this.perPageSelector = this.tableHelpers.find('#per_page');
|
11
|
+
this.pageHelpers = this.tableHelpers.find('#page-helpers');
|
12
|
+
this.resetButton = inputs.resetButton;
|
13
|
+
this.refreshButton = inputs.refreshButton;
|
14
|
+
this.filterDiv = inputs.filterDiv;
|
15
|
+
this.paginatorDiv = inputs.paginatorDiv;
|
16
|
+
this.removePaginationButton = inputs.removePaginationButton;
|
17
|
+
this.applyPaginationButton = inputs.applyPaginationButton;
|
18
|
+
this.chartContainer = inputs.chartContainer;
|
19
|
+
this.downloadLinks = inputs.downloadLinks;
|
20
|
+
this.requestUrl = '';
|
21
|
+
this.isStatePushable = true;
|
22
|
+
this.tableSorterObject = null;
|
23
|
+
this.searcherObject = null;
|
24
|
+
this.paginatorObject = null;
|
25
|
+
}
|
26
|
+
|
27
|
+
ReportLoader.prototype.init = function() {
|
28
|
+
var tableSorterInputs = {
|
29
|
+
$insightsTable: this.$insightsTableList,
|
30
|
+
reportLoader: this,
|
31
|
+
paginatorDiv: this.paginatorDiv
|
32
|
+
};
|
33
|
+
this.tableSorterObject = new TableSorter(tableSorterInputs);
|
34
|
+
this.tableSorterObject.bindEvents();
|
35
|
+
|
36
|
+
var searcherInputs = {
|
37
|
+
filterDiv: this.filterDiv,
|
38
|
+
insightsDiv: this.$insightsTableList,
|
39
|
+
tableSorterObject: this.tableSorterObject
|
40
|
+
};
|
41
|
+
this.searcherObject = new Searcher(searcherInputs, this);
|
42
|
+
this.searcherObject.bindEvents();
|
43
|
+
|
44
|
+
var paginatorInputs = {
|
45
|
+
paginatorDiv: this.paginatorDiv,
|
46
|
+
insightsDiv: this.$insightsTableList,
|
47
|
+
tableSorterObject: this.tableSorterObject,
|
48
|
+
removePaginationButton: this.removePaginationButton,
|
49
|
+
applyPaginationButton: this.applyPaginationButton
|
50
|
+
};
|
51
|
+
this.paginatorObject = new Paginator(paginatorInputs, this);
|
52
|
+
this.paginatorObject.bindEvents();
|
53
|
+
this.setDefaultReport();
|
54
|
+
};
|
55
|
+
|
56
|
+
ReportLoader.prototype.setDefaultReport = function() {
|
57
|
+
if(location.pathname == '/admin/insights') {
|
58
|
+
this.$selectList.val($(this.$selectList.find('option')[1]).val()).change();
|
59
|
+
}
|
60
|
+
window.history.pushState({}, '', this.$selectList.find(':selected').data('url'));
|
61
|
+
};
|
62
|
+
|
63
|
+
ReportLoader.prototype.bindEvents = function() {
|
64
|
+
var _this = this;
|
65
|
+
_this.$selectList.on('change', function() {
|
66
|
+
$(this).find('option').first().attr('disabled', true);
|
67
|
+
_this.paginatorObject.togglePaginatorButtons(_this.paginatorObject.removePaginationButton, _this.paginatorObject.applyPaginationButton);
|
68
|
+
_this.searcherObject.clearSearchFields();
|
69
|
+
_this.loadChart($(this).find(':selected'));
|
70
|
+
});
|
71
|
+
|
72
|
+
this.resetButton.on('click', function() {
|
73
|
+
_this.resetFilters(event);
|
74
|
+
});
|
75
|
+
|
76
|
+
this.refreshButton.on('click', function() {
|
77
|
+
_this.refreshPage(event);
|
78
|
+
});
|
79
|
+
|
80
|
+
_this.bindPopStateEventCallback();
|
81
|
+
};
|
82
|
+
|
83
|
+
ReportLoader.prototype.resetFilters = function(event) {
|
84
|
+
event.preventDefault();
|
85
|
+
var $element = $(event.target),
|
86
|
+
paginated = !this.removePaginationButton.closest('span').hasClass('hide');
|
87
|
+
$element.attr('href', this.perPageSelector.data('url') + '&paginate=' + paginated);
|
88
|
+
$element.data('url', this.perPageSelector.data('url') + '&paginate=' + paginated);
|
89
|
+
this.loadChart($element);
|
90
|
+
this.searcherObject.clearSearchFields();
|
91
|
+
};
|
92
|
+
|
93
|
+
ReportLoader.prototype.refreshPage = function(event) {
|
94
|
+
event.preventDefault();
|
95
|
+
var $element = $(event.target);
|
96
|
+
$element.attr('href', location.href);
|
97
|
+
$element.data('url', location.href);
|
98
|
+
this.loadChart($element);
|
99
|
+
};
|
100
|
+
|
101
|
+
ReportLoader.prototype.bindPopStateEventCallback = function() {
|
102
|
+
var _this = this;
|
103
|
+
window.onpopstate = function(event) {
|
104
|
+
event.state ? (report_name = event.state['report_name'] || '') : (report_name = '');
|
105
|
+
_this.$selectList.val(report_name);
|
106
|
+
_this.$selectList.select2('val', report_name);
|
107
|
+
var $selectedOption = _this.$selectList.find(':selected');
|
108
|
+
_this.fetchChartDataWithoutState(location.href, $selectedOption);
|
109
|
+
};
|
110
|
+
};
|
111
|
+
|
112
|
+
ReportLoader.prototype.loadChart = function($selectedOption) {
|
113
|
+
var requestPath = $selectedOption.data('url');
|
114
|
+
this.fetchChartData(requestPath, $selectedOption);
|
115
|
+
};
|
116
|
+
|
117
|
+
ReportLoader.prototype.fetchChartData = function(url, $selectedOption) {
|
118
|
+
var _this = this;
|
119
|
+
_this.requestUrl = url;
|
120
|
+
$.ajax({
|
121
|
+
type: 'GET',
|
122
|
+
url: url,
|
123
|
+
dataType: 'json',
|
124
|
+
success: function(data) {
|
125
|
+
(_this.isStatePushable ? _this.populateInsightsData(data) : _this.populateInsightsDataWithoutState(data))
|
126
|
+
if(data.headers != undefined) {
|
127
|
+
_this.tableHelpers.removeClass('hide');
|
128
|
+
if(data.pagination_required == false) {
|
129
|
+
_this.pageSelector.addClass('hide');
|
130
|
+
$.each(_this.pageHelpers.find('.col-md-2'), function(index, object) {
|
131
|
+
$(object).removeClass('col-md-2').addClass('col-md-3');
|
132
|
+
});
|
133
|
+
} else {
|
134
|
+
_this.pageSelector.removeClass('hide');
|
135
|
+
$.each(_this.pageHelpers.find('.col-md-3'), function(index, object) {
|
136
|
+
$(object).removeClass('col-md-3').addClass('col-md-2');
|
137
|
+
});
|
138
|
+
}
|
139
|
+
_this.perPageSelector.data('url', data['request_path'] + '?report_category=' + data['report_category']);
|
140
|
+
_this.setDownloadLinksPath();
|
141
|
+
_this.searcherObject.refreshSearcher($selectedOption, data);
|
142
|
+
_this.paginatorObject.refreshPaginator(data);
|
143
|
+
if(data.searched_fields != undefined)
|
144
|
+
_this.searcherObject.fillFormFields(data.searched_fields);
|
145
|
+
}
|
146
|
+
}
|
147
|
+
});
|
148
|
+
};
|
149
|
+
|
150
|
+
ReportLoader.prototype.buildChart = function(data) {
|
151
|
+
var chart_container = $('#chart-container');
|
152
|
+
if ((data['chart_json'] != undefined) && (data['chart_json']['chart'])) {
|
153
|
+
chart_container.empty().removeClass('hidden');
|
154
|
+
$.each(data['chart_json']['charts'], function(index, chart) {
|
155
|
+
var chart_div = $('<div>', { id: chart['id'] });
|
156
|
+
chart_container.append(chart_div);
|
157
|
+
chart_div.highcharts(chart['json']);
|
158
|
+
});
|
159
|
+
} else {
|
160
|
+
chart_container.addClass('hidden');
|
161
|
+
}
|
162
|
+
};
|
163
|
+
|
164
|
+
ReportLoader.prototype.fetchChartDataWithoutState = function(url, $selectedOption) {
|
165
|
+
this.isStatePushable = false;
|
166
|
+
this.fetchChartData(url, $selectedOption);
|
167
|
+
};
|
168
|
+
|
169
|
+
ReportLoader.prototype.populateInsightsData = function(data) {
|
170
|
+
if(data.headers != undefined) {
|
171
|
+
var $templateData = $(tmpl('tmpl', data));
|
172
|
+
this.$insightsTableList.empty().append($templateData);
|
173
|
+
this.buildChart(data);
|
174
|
+
} else {
|
175
|
+
this.$insightsTableList.empty();
|
176
|
+
this.paginatorDiv.empty();
|
177
|
+
this.filterDiv.addClass('hide');
|
178
|
+
this.chartContainer.addClass('hidden');
|
179
|
+
}
|
180
|
+
if(this.isStatePushable) {
|
181
|
+
this.pushUrlToHistory();
|
182
|
+
} else {
|
183
|
+
this.isStatePushable = true;
|
184
|
+
}
|
185
|
+
};
|
186
|
+
|
187
|
+
ReportLoader.prototype.setDownloadLinksPath = function($selectedOption) {
|
188
|
+
var _this = this;
|
189
|
+
$.each(this.downloadLinks, function() {
|
190
|
+
$(this).attr('href', $(this).data('url') + '?id=' + _this.$selectList.val() + '&paginate=false');
|
191
|
+
});
|
192
|
+
};
|
193
|
+
|
194
|
+
ReportLoader.prototype.populateInsightsDataWithoutState = function(data) {
|
195
|
+
this.isStatePushable = false;
|
196
|
+
this.populateInsightsData(data);
|
197
|
+
};
|
198
|
+
|
199
|
+
ReportLoader.prototype.pushUrlToHistory = function() {
|
200
|
+
var report_name = this.$selectList.find(':selected').val();
|
201
|
+
window.history.pushState({ report_name: report_name }, '', this.requestUrl);
|
202
|
+
this.requestUrl = '';
|
203
|
+
};
|
204
|
+
|
205
|
+
ReportLoader.prototype.populateInitialData = function() {
|
206
|
+
var $selectedOption = this.$selectList.find(':selected');
|
207
|
+
this.fetchChartDataWithoutState(location.href, $selectedOption);
|
208
|
+
};
|
209
|
+
|
210
|
+
$(function() {
|
211
|
+
var inputs = {
|
212
|
+
insightsDiv: $('#report-div'),
|
213
|
+
reportsSelectBox: $('#reports'),
|
214
|
+
resetButton: $('#reset'),
|
215
|
+
refreshButton: $('#refresh'),
|
216
|
+
removePaginationButton: $('#remove-pagination'),
|
217
|
+
applyPaginationButton: $('#apply-pagination'),
|
218
|
+
tableHelpers: $('#table-helpers'),
|
219
|
+
filterDiv: $('#search-div'),
|
220
|
+
paginatorDiv: $('#paginator-div'),
|
221
|
+
chartContainer: $('#chart-container'),
|
222
|
+
downloadLinks: $('.download-link')
|
223
|
+
},
|
224
|
+
reportLoader = new ReportLoader(inputs);
|
225
|
+
reportLoader.init();
|
226
|
+
reportLoader.bindEvents();
|
227
|
+
reportLoader.populateInitialData();
|
228
|
+
});
|