source: products/qSEOptimizer/branches/js-statistics/skins/qSEOptimizer/statistics.js.dtml @ 1

Last change on this file since 1 was 1, checked in by myroslav, 18 years ago

Building directory structure

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1/******************************************************************
2<dtml-with portal_properties>
3
4Word 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
11HTML 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>
23var stop_words = <dtml-var expr="list(seo_properties.stop_words)">;
24<dtml-except>
25var stop_words = [];
26</dtml-try>
27
28<dtml-try>
29var ids = <dtml-var expr="list(seo_properties.fields)">;
30<dtml-except>
31var ids = [];
32</dtml-try>
33
34var 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
36var stop_dict = {};
37
38for (var j=0; word=stop_words[j]; j++) {stop_dict[word] = 1;};
39
40function 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
64function 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
73function 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
90function 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
104registerPloneFunction(loadStatistics);
105/*</dtml-with> Register onload function */
Note: See TracBrowser for help on using the repository browser.