libevent: rename `bind_to` to the more precise `bind_tcp`
[nit.git] / share / nitdoc / js / nitdoc.utils.js
1 /*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15 /* Utils module */
16
17 String.prototype.startsWith = function(prefix, caseSensitive) {
18 if(caseSensitive) {
19 return this.toUpperCase().indexOf(prefix.toUpperCase()) === 0;
20 }
21 return this.indexOf(prefix) === 0;
22 }
23
24 // Compare two strings using Sorensen-Dice Coefficient
25 // see: http://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient
26 String.prototype.dice = function(other) {
27 var length1 = this.length - 1;
28 var length2 = other.length - 1;
29 if(length1 < 1 || length2 < 1) return 0;
30
31 var bigrams2 = [];
32 for(var i = 0; i < length2; i++) {
33 bigrams2.push(other.substr(i, 2));
34 }
35
36 var intersection = 0;
37 for(var i = 0; i < length1; i++) {
38 var bigram1 = this.substr(i, 2);
39 for(var j = 0; j < length2; j++) {
40 if(bigram1 == bigrams2[j]) {
41 intersection++;
42 bigrams2[j] = null;
43 break;
44 }
45 }
46 }
47 return (2.0 * intersection) / (length1 + length2);
48 }
49
50 var Utils = {
51 delayEvent: function(handler, event) {
52 if(this.delayEvent.timeout) {
53 clearTimeout(this.delayEvent.timeout);
54 }
55 this.delayEvent.timeout = setTimeout(function() {
56 handler.call(event);
57 }, 100);
58 },
59
60 scrollTo: function(target) {
61 var element = $(target);
62 if(element[0]) {
63 $("body, html").animate({
64 scrollTop: element.offset().top - 60
65 });
66 }
67 },
68
69 openTab: function(e) {
70 // Open tab
71 var url = document.location.toString();
72 if (url.match('#')) {
73 var hash = url.split('#')[1];
74 var element = $('.nav-tabs a[href="#' + hash + '"]');
75 if(element[0]) {
76 element.tab('show');
77 } else {
78 Utils.scrollTo('#' + hash);
79 }
80 }
81
82 // Jump to id
83 var obj = new URL(url);
84 var arg = obj.searchParams.get("def");
85 if(arg) {
86 var def = '#' + arg;
87 $('.card.active').removeClass('active');
88 $(def).addClass('active');
89 $(def).find('.collapse').collapse();
90 Utils.scrollTo(def);
91 }
92 }
93 };
94
95 Utils.openTab();
96
97 window.addEventListener("hashchange", Utils.openTab, false);
98
99 // Scroll on hash click
100 $('.summary a[href*=#]').on('click', function(e) {
101 e.preventDefault();
102 Utils.scrollTo(e.currentTarget.hash);
103 history.pushState({}, '', e.currentTarget.hash);
104 });
105
106 // Change hash for page-reload
107 $('.nav-tabs a[href]').on('shown.bs.tab', function (e) {
108 history.pushState({}, '', e.target.hash)
109 });