1 | /****************************************************************** |
---|
2 | <dtml-with portal_properties> |
---|
3 | |
---|
4 | Word Wrapping Rules: |
---|
5 | |
---|
6 | 1. multiple spaces == one space 1 |
---|
7 | 2. word can include "-" and "'" // [\-\'] |
---|
8 | 3. token that not contain a-zA-Z0-9 symbols isn't a word |
---|
9 | 4. non-word characters inside (or on the edge of) word divide it in smaller words |
---|
10 | |
---|
11 | HTML code for text statistics: // for else fields than title in html code title will change accordingly to the field's id |
---|
12 | |
---|
13 | <div id="title-statistics" class="statistics"> |
---|
14 | <span class="total-words">Total Words: <span class="value">5</span></span> |
---|
15 | <span class="stop-words">Stop Words: <span class="value">2</span></span> |
---|
16 | <span class="used-words">Used Words: <span class="value">3</span></span> |
---|
17 | <span class="length-words">Length: <span class="value">24</span></span> |
---|
18 | </div> |
---|
19 | |
---|
20 | *******************************************************************/ |
---|
21 | |
---|
22 | <dtml-try> |
---|
23 | var stop_words = <dtml-var expr="list(seo_properties.stop_words)">; |
---|
24 | <dtml-except> |
---|
25 | var stop_words = []; |
---|
26 | </dtml-try> |
---|
27 | |
---|
28 | <dtml-try> |
---|
29 | var ids = <dtml-var expr="list(seo_properties.fields)">; |
---|
30 | <dtml-except> |
---|
31 | var ids = []; |
---|
32 | </dtml-try> |
---|
33 | |
---|
34 | var template = '<span class="total-words">Total Words: <span class="value">total_row</span></span> <span class="stop-words">Stop Words: <span class="value">stop_row</span></span> <span class="used-words">Used Words: <span class="value">used_row</span></span> <span class="length-words">Length: <span class="value">length_row</span></span>'; |
---|
35 | |
---|
36 | var stop_dict = {}; |
---|
37 | |
---|
38 | for (var j=0; word=stop_words[j]; j++) {stop_dict[word] = 1;}; |
---|
39 | |
---|
40 | function countWords(data) { |
---|
41 | var total = 0; |
---|
42 | var stop = 0; |
---|
43 | var used = 0; |
---|
44 | var len = data.length || 0; |
---|
45 | if (len != 0) { |
---|
46 | data = data // replace all non-word character with space |
---|
47 | .replace(/[^a-zA-Z0-9\-\'\u2019\"\`]+/g, ' ') |
---|
48 | // replace "-" and "'" symbols if it create groups event inside of token |
---|
49 | .replace(/[\-\'\u2019\"\`]{2,}/g, ' ') |
---|
50 | // replace all non-word characters and "-", "'" if it stay at word edge |
---|
51 | .replace(/(?:^|\s+)[^a-zA-Z0-9]+|[^a-zA-Z0-9]+(?:\s+|$)/g, ' ') |
---|
52 | // strip whitespaces |
---|
53 | .replace(/^\s*(.*?)\s*$/, '$1'); |
---|
54 | var data_list = data.split(/[^a-zA-Z0-9\-\'\u2019\"\`]+/); |
---|
55 | for (var i=0; word=data_list[i]; i++) { |
---|
56 | stop += stop_dict[word.toLowerCase()] ? 1:0; |
---|
57 | }; |
---|
58 | total = data_list.length; |
---|
59 | used = total - stop; |
---|
60 | }; |
---|
61 | return {'total': total, 'stop': stop, 'used': used, 'length': len}; |
---|
62 | }; |
---|
63 | |
---|
64 | function getHTML(source) { |
---|
65 | var stats = countWords(source); |
---|
66 | var html = template; |
---|
67 | for (var p in stats) { |
---|
68 | html = html.replace(p+'_row', stats[p]); |
---|
69 | }; |
---|
70 | return html; |
---|
71 | }; |
---|
72 | |
---|
73 | function listenField(event) { |
---|
74 | var event = event ? event:window.event; |
---|
75 | var target = null; |
---|
76 | if (event.target) { |
---|
77 | target = event.target; |
---|
78 | } else if (event.srcElement) { |
---|
79 | target = event.srcElement; |
---|
80 | }; |
---|
81 | if (!target) {return false;}; |
---|
82 | var update = document.getElementById(target.id+'-statistics'); |
---|
83 | if (update) { |
---|
84 | update.innerHTML = getHTML(target.value||''); |
---|
85 | } else { |
---|
86 | window.status = "Couldn\'t find element with id = \'"+target.id+"-statistics\' on this page"; |
---|
87 | }; |
---|
88 | }; |
---|
89 | |
---|
90 | function loadStatistics(event){ |
---|
91 | for (var i=0; id=ids[i]; i++) { |
---|
92 | var el = document.getElementById(id); |
---|
93 | if (el && (typeof(el.value) != 'undefined')) { |
---|
94 | var div = document.createElement('DIV'); |
---|
95 | div.id = id+'-statistics'; |
---|
96 | div.className = 'statistics'; |
---|
97 | div.innerHTML = getHTML(el.value||''); |
---|
98 | el.parentNode.insertBefore(div, el); |
---|
99 | registerEventListener(el, 'keyup', listenField); |
---|
100 | }; |
---|
101 | }; |
---|
102 | }; |
---|
103 | |
---|
104 | registerPloneFunction(loadStatistics); |
---|
105 | /*</dtml-with> Register onload function */ |
---|