Commit c953c9dab72258946721b0716a87ead22fae906d
Linked-data page now shows tag cloud of users and tags.
| | | | 730 | } | 730 | } |
---|
731 | }; | 731 | }; |
---|
732 | //swtr.AppView = AppView; | 732 | //swtr.AppView = AppView; |
---|
| | 733 | var LDSwts = Backbone.Collection.extend({ |
---|
| | 734 | getAll: function(options) { |
---|
| | 735 | //get all sweets of ocd-anno type |
---|
| | 736 | // error checking |
---|
| | 737 | if(!options.what) { |
---|
| | 738 | throw Error('"what" option must be passed to get sweets of a URI'); |
---|
| | 739 | return false; |
---|
| | 740 | } |
---|
| | 741 | // setting up params |
---|
| | 742 | var what = options.what; |
---|
| | 743 | url = swtr.swtstoreURL() + swtr.endpoints.get + '?what=' + |
---|
| | 744 | encodeURIComponent(what) + '&access_token=' + swtr.access_token; |
---|
| | 745 | // get them! |
---|
| | 746 | this.sync('read', this, { |
---|
| | 747 | url: url, |
---|
| | 748 | success: function() { |
---|
| | 749 | if(typeof options.success === 'function') { |
---|
| | 750 | options.success.apply(this, arguments); |
---|
| | 751 | } |
---|
| | 752 | }, |
---|
| | 753 | error: function() { |
---|
| | 754 | if(typeof options.error === 'function') { |
---|
| | 755 | options.error.apply(this, arguments); |
---|
| | 756 | } |
---|
| | 757 | } |
---|
| | 758 | }); |
---|
| | 759 | } |
---|
| | 760 | }); |
---|
733 | | 761 | |
---|
| | 762 | var LDView = Backbone.View.extend({ |
---|
| | 763 | initialize: function() { |
---|
| | 764 | swtr.LDs = new LDSwts(); |
---|
| | 765 | swtr.LDs.getAll({what: 'img-anno', |
---|
| | 766 | success: function(data) { |
---|
| | 767 | swtr.LDs.add(data); |
---|
| | 768 | new TagCloudView({collection: swtr.LDs}); |
---|
| | 769 | }}); |
---|
| | 770 | }, |
---|
| | 771 | render: function() { |
---|
| | 772 | |
---|
| | 773 | } |
---|
| | 774 | }); |
---|
| | 775 | |
---|
| | 776 | var TagCloudView = Backbone.View.extend({ |
---|
| | 777 | el: $('#tag-cloud'), |
---|
| | 778 | initialize: function() { |
---|
| | 779 | this.user_tag_el = $('#user-tag-cloud'); |
---|
| | 780 | this.tags_tag_el = $('#tags-tag-cloud'); |
---|
| | 781 | this.render(); |
---|
| | 782 | |
---|
| | 783 | }, |
---|
| | 784 | render: function() { |
---|
| | 785 | this.renderUserTagCloud(); |
---|
| | 786 | this.renderTagsTagCloud(); |
---|
| | 787 | }, |
---|
| | 788 | renderUserTagCloud: function() { |
---|
| | 789 | var fill = d3.scale.category20(); |
---|
| | 790 | var words = _.uniq(swtr.LDs.pluck('who')); |
---|
| | 791 | var weight = swtr.LDs.countBy('who'); |
---|
| | 792 | d3.layout.cloud().size([300, 300]) |
---|
| | 793 | .words(words.map(function(d) { |
---|
| | 794 | return {text: d, size: weight[d]}; |
---|
| | 795 | })) |
---|
| | 796 | .padding(5) |
---|
| | 797 | .rotate(function() { return ~~(Math.random() * 2) * 90; }) |
---|
| | 798 | .font("Impact") |
---|
| | 799 | .fontSize(function(d) { return d.size; }) |
---|
| | 800 | .on("end", this.draw) |
---|
| | 801 | .start(); |
---|
| | 802 | |
---|
| | 803 | }, |
---|
| | 804 | draw: function (words) { |
---|
| | 805 | d3.select("#user-tag-cloud").append("svg") |
---|
| | 806 | .attr("width", 300) |
---|
| | 807 | .attr("height", 300) |
---|
| | 808 | .append("g") |
---|
| | 809 | .attr("transform", "translate(150,150)") |
---|
| | 810 | .selectAll("text") |
---|
| | 811 | .data(words) |
---|
| | 812 | .enter().append("text") |
---|
| | 813 | .style("font-size", function(d) { return "16px"; }) |
---|
| | 814 | .style("font-family", "Impact") |
---|
| | 815 | .style("fill", function(d, i) {console.log(i); return 'red'; }) |
---|
| | 816 | .attr("text-anchor", "middle") |
---|
| | 817 | .attr("transform", function(d) { |
---|
| | 818 | return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")"; |
---|
| | 819 | }) |
---|
| | 820 | .text(function(d) {console.log(d); return d.text; }); |
---|
| | 821 | }, |
---|
| | 822 | renderTagsTagCloud: function() { |
---|
| | 823 | var fill = d3.scale.category20(); |
---|
| | 824 | var sweetsWithTags = swtr.LDs.filter(function(k) { |
---|
| | 825 | if(k.get('how').tags) { |
---|
| | 826 | return k; |
---|
| | 827 | } |
---|
| | 828 | }); |
---|
| | 829 | var tags = []; |
---|
| | 830 | _.each(sweetsWithTags, function(sweet) { |
---|
| | 831 | tags.push(sweet.get('how').tags); |
---|
| | 832 | }); |
---|
| | 833 | tags = _.flatten(tags); |
---|
| | 834 | d3.layout.cloud().size([300, 300]) |
---|
| | 835 | .words(tags.map(function(d) { |
---|
| | 836 | return {text: d, size: 10}; |
---|
| | 837 | })) |
---|
| | 838 | .padding(5) |
---|
| | 839 | .rotate(function() { return ~~(Math.random() * 2) * 90; }) |
---|
| | 840 | .font("Impact") |
---|
| | 841 | .fontSize(function(d) { return d.size; }) |
---|
| | 842 | .on("end", this.drawTags) |
---|
| | 843 | .start(); |
---|
| | 844 | |
---|
| | 845 | }, |
---|
| | 846 | drawTags: function(words) { |
---|
| | 847 | d3.select("#tags-tag-cloud").append("svg") |
---|
| | 848 | .attr("width", 300) |
---|
| | 849 | .attr("height", 300) |
---|
| | 850 | .append("g") |
---|
| | 851 | .attr("transform", "translate(150,150)") |
---|
| | 852 | .selectAll("text") |
---|
| | 853 | .data(words) |
---|
| | 854 | .enter().append("text") |
---|
| | 855 | .style("font-size", function(d) { return "16px"; }) |
---|
| | 856 | .style("font-family", "Impact") |
---|
| | 857 | .style("fill", function(d, i) {console.log(i); return 'red'; }) |
---|
| | 858 | .attr("text-anchor", "middle") |
---|
| | 859 | .attr("transform", function(d) { |
---|
| | 860 | return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")"; |
---|
| | 861 | }) |
---|
| | 862 | .text(function(d) {console.log(d); return d.text; }); |
---|
| | 863 | } |
---|
| | 864 | |
---|
| | 865 | }); |
---|
| | 866 | |
---|
734 | var AppRouter = Backbone.Router.extend({ | 867 | var AppRouter = Backbone.Router.extend({ |
---|
735 | routes: { | 868 | routes: { |
---|
736 | 'home': 'home', | 869 | 'home': 'home', |
---|
… | | … | |
---|
878 | linkedData: function() { | 878 | linkedData: function() { |
---|
879 | this.hideAll(); | 879 | this.hideAll(); |
---|
880 | this.show('linked-data-page'); | 880 | this.show('linked-data-page'); |
---|
| | 881 | new LDView(); |
---|
881 | }, | 882 | }, |
---|
882 | play: function() { | 883 | play: function() { |
---|
883 | this.hideAll(); | 884 | this.hideAll(); |
---|
| | | | 164 | | 164 | |
---|
165 | </div> | 165 | </div> |
---|
166 | | 166 | |
---|
167 | <div id="linked-data-page" class="page"></div> | | <div id="linked-data-page" class="page"></div> |
---|
| | 167 | <div id="linked-data-page" class="page"> | | | 168 | <div id="tag-cloud"> |
---|
| | 169 | <div id="user-tag-cloud" class="row"></div> |
---|
| | 170 | <div id="tags-tag-cloud" class="row"></div> |
---|
| | 171 | </div> |
---|
| | 172 | </div> |
---|
168 | <div id="search-page" class="page"></div> | 173 | <div id="search-page" class="page"></div> |
---|
169 | | 174 | |
---|
170 | </div> | 175 | </div> |
---|
… | | … | |
---|
194 | }; | 194 | }; |
---|
195 | </script> | 195 | </script> |
---|
196 | <script src="{{ url_for('static', filename='js/lib/jquery-1.10.2.min.js') }}"></script> | 196 | <script src="{{ url_for('static', filename='js/lib/jquery-1.10.2.min.js') }}"></script> |
---|
| | 197 | <script src="{{ url_for('static', filename='js/lib/d3.js') }}"></script> |
---|
| | 198 | <script src="{{ url_for('static', filename='js/lib/d3.layout.cloud.js') }}"></script> |
---|
197 | <script src="{{ url_for('static', filename='js/lib/bootstrap.min.js') }}"></script> | 199 | <script src="{{ url_for('static', filename='js/lib/bootstrap.min.js') }}"></script> |
---|
198 | <script src="{{ url_for('static', filename='js/lib/underscore-1.5.2.min.js') }}"></script> | 200 | <script src="{{ url_for('static', filename='js/lib/underscore-1.5.2.min.js') }}"></script> |
---|
199 | <script src="{{ url_for('static', filename='js/lib/backbone-1.0.0.min.js') }}"></script> | 201 | <script src="{{ url_for('static', filename='js/lib/backbone-1.0.0.min.js') }}"></script> |
---|