comparison booket.js @ 12:948048e40fab

Add option for displaying tag list as a tag cloud
author Guido Berhoerster <guido+booket@berhoerster.name>
date Thu, 18 Sep 2014 20:28:09 +0200
parents ef5d75bcac5e
children 64272f2105da
comparison
equal deleted inserted replaced
11:ef5d75bcac5e 12:948048e40fab
488 var tagCount; 488 var tagCount;
489 489
490 if (this._tagCount.has(tag)) { 490 if (this._tagCount.has(tag)) {
491 tagCount = this._tagCount.get(tag) + 1; 491 tagCount = this._tagCount.get(tag) + 1;
492 this._tagCount.set(tag, tagCount); 492 this._tagCount.set(tag, tagCount);
493 this.notify('tag-count-changed', tag, tagCount); 493 this.notify('tag-count-changed', tag, this._tagCount);
494 } else { 494 } else {
495 this._tagCount.set(tag, 1); 495 this._tagCount.set(tag, 1);
496 this.notify('tag-added', tag); 496 this.notify('tag-added', tag, this._tagCount);
497 } 497 }
498 }, this); 498 }, this);
499 }, this); 499 }, this);
500 500
501 // apply tag filter and search added bookmarks 501 // apply tag filter and search added bookmarks
535 if (this._tagCount.has(tag)) { 535 if (this._tagCount.has(tag)) {
536 tagCount = this._tagCount.get(tag); 536 tagCount = this._tagCount.get(tag);
537 if (tagCount > 1) { 537 if (tagCount > 1) {
538 tagCount--; 538 tagCount--;
539 this._tagCount.set(tag, tagCount); 539 this._tagCount.set(tag, tagCount);
540 this.notify('tag-count-changed', tag, tagCount); 540 this.notify('tag-count-changed', tag, this._tagCount);
541 } else { 541 } else {
542 this._tagCount.delete(tag); 542 this._tagCount.delete(tag);
543 this.notify('tag-deleted', tag); 543 this.notify('tag-deleted', tag, this._tagCount);
544 544
545 if (this._filterTags.has(tag)) { 545 if (this._filterTags.has(tag)) {
546 this._filterTags.delete(tag); 546 this._filterTags.delete(tag);
547 needUpdateFilterTags = true; 547 needUpdateFilterTags = true;
548 } 548 }
884 /* 884 /*
885 * view 885 * view
886 */ 886 */
887 887
888 var TagView = function () { 888 var TagView = function () {
889 var tagsElement;
890
889 ObservableMixin.call(this); 891 ObservableMixin.call(this);
890 892
891 this.tagListElement = document.querySelector('#tags ul.tag-list'); 893 tagsElement = document.querySelector('#tags');
892 this.tagListElement.addEventListener('click', this); 894 tagsElement.addEventListener('click', this);
895
896 this.tagListElement = tagsElement.querySelector('ul.tag-list');
893 897
894 this.tagDatalistElement = document.querySelector('#tag-datalist'); 898 this.tagDatalistElement = document.querySelector('#tag-datalist');
895 899
896 this.tagTemplate = document.querySelector('#tag-template'); 900 this.tagTemplate = document.querySelector('#tag-template');
897 }; 901 };
898 902
899 extend(TagView, ObservableMixin); 903 extend(TagView, ObservableMixin);
900 904
901 TagView.prototype.onTagAdded = function (tag) { 905 TagView.prototype.onTagAdded = function (tag, tagCount) {
902 var newNode; 906 var newNode;
903 var tagElement; 907 var tagElement;
904 var setTagButton; 908 var setTagButton;
905 var toggleTagButton; 909 var toggleTagButton;
906 var tagElements; 910 var tagElements;
936 } 940 }
937 this.tagListElement.insertBefore(newNode, (referenceNode !== undefined) ? 941 this.tagListElement.insertBefore(newNode, (referenceNode !== undefined) ?
938 referenceNode.nextSibling : this.tagListElement.firstChild); 942 referenceNode.nextSibling : this.tagListElement.firstChild);
939 943
940 // initialize tag count 944 // initialize tag count
941 this.onTagCountChanged(tag, 1); 945 this.onTagCountChanged(tag, tagCount);
942 946
943 // add to datalist 947 // add to datalist
944 for (i = 0; i < this.tagDatalistElement.options.length; i++) { 948 for (i = 0; i < this.tagDatalistElement.options.length; i++) {
945 if (this.tagDatalistElement.options[i].value == tag) { 949 if (this.tagDatalistElement.options[i].value == tag) {
946 isInDatalist = true; 950 isInDatalist = true;
952 tagOptionElement.value = tag; 956 tagOptionElement.value = tag;
953 this.tagDatalistElement.appendChild(tagOptionElement); 957 this.tagDatalistElement.appendChild(tagOptionElement);
954 } 958 }
955 }; 959 };
956 960
961 TagView.prototype.updateTagCloud = function (tagCount) {
962 var tagElements;
963 var i;
964 var j;
965 var tagCountMax = 1;
966
967 tagCount.forEach(function (count) {
968 if (count > tagCountMax) {
969 tagCountMax = count;
970 }
971 }, this);
972
973 tagElements = this.tagListElement.querySelectorAll('ul.tag-list > li');
974 for (i = 0; i < tagElements.length; i++) {
975 for (j = 1; j <= 10; j++) {
976 tagElements[i].classList.remove('tag-frequency-' + j);
977 }
978 tagElements[i].classList.add('tag-frequency-' +
979 (Math.floor(tagCount.get(tagElements[i].dataset.tag) /
980 (tagCountMax / 9)) + 1));
981 }
982 };
983
957 TagView.prototype.onTagCountChanged = function (tag, tagCount) { 984 TagView.prototype.onTagCountChanged = function (tag, tagCount) {
958 this.tagListElement.querySelector('li' + createDatasetSelector('tag', tag) + 985 this.tagListElement.querySelector('li' + createDatasetSelector('tag', tag) +
959 ' .tag-count').textContent = '(' + tagCount + ')'; 986 ' .tag-count').textContent = '(' + tagCount.get(tag) + ')';
960 }; 987 this.updateTagCloud(tagCount);
961 988 };
962 TagView.prototype.onTagDeleted = function (tag) { 989
990 TagView.prototype.onTagDeleted = function (tag, tagCount) {
963 var tagElement; 991 var tagElement;
964 992
965 // remove from tag list 993 // remove from tag list
966 tagElement = this.tagListElement.querySelector('li' + 994 tagElement = this.tagListElement.querySelector('li' +
967 createDatasetSelector('tag', tag)); 995 createDatasetSelector('tag', tag));
968 tagElement.parentNode.removeChild(tagElement); 996 tagElement.parentNode.removeChild(tagElement);
997
998 this.updateTagCloud(tagCount);
969 }; 999 };
970 1000
971 TagView.prototype.onFilterTagsSearchChanged = function (filteredBookmarks, 1001 TagView.prototype.onFilterTagsSearchChanged = function (filteredBookmarks,
972 newFilterTags, newSearchTerm) { 1002 newFilterTags, newSearchTerm) {
973 var tagElements; 1003 var tagElements;
991 } 1021 }
992 } 1022 }
993 }; 1023 };
994 1024
995 TagView.prototype.handleEvent = function (e) { 1025 TagView.prototype.handleEvent = function (e) {
996 if (e.type === 'click' && (e.target.name === 'set-tag' || 1026 if (e.type === 'click') {
997 e.target.name === 'toggle-tag')) { 1027 if (e.target.name === 'set-tag' ||
1028 e.target.name === 'toggle-tag') {
998 e.target.blur(); 1029 e.target.blur();
999 1030
1000 this.notify(e.target.name, getAncestorElementDatasetItem(e.target, 1031 this.notify(e.target.name, getAncestorElementDatasetItem(e.target,
1001 'tag')); 1032 'tag'));
1033 } else if (e.target.name === 'show-tag-cloud') {
1034 this.tagListElement.classList.toggle('tag-cloud', e.target.checked);
1035 }
1002 } 1036 }
1003 }; 1037 };
1004 1038
1005 1039
1006 var ActionsView = function () { 1040 var ActionsView = function () {