PageRenderTime 38ms CodeModel.GetById 24ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/webapp/public/js/tags/list.js

http://thoughtsite.googlecode.com/
JavaScript | 137 lines | 107 code | 4 blank | 26 comment | 27 complexity | 4226830f7760e1fd7347bb594fcb051d MD5 | raw file
  1/* Copyright 2010 Google Inc.
  2 * 
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 * 
  7 *      http://www.apache.org/licenses/LICENSE-2.0
  8 * 
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS.
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License
 14 */
 15
 16google.setOnLoadCallback(function() {
 17    ie.progressStart();
 18    
 19    $('#tagKeyword').focus(function () {
 20        if($('#tagKeyword').val() == 'Search Tag') {
 21            $('#tagKeyword').val('');
 22        }
 23    });
 24    $('#tagKeyword').blur(function () {
 25        if($('#tagKeyword').val() == '') {
 26            $('#tagKeyword').val('Search Tag');
 27        }
 28    });
 29    $('#searchTag').click(function(){
 30        if('' == $.trim($('#tagKeyword').val()) || 'Search Tag' == $.trim($('#tagKeyword').val())) {
 31            alert('Please provide search keyword.');
 32            return false;
 33        }
 34        var elem = $('.page_sel');
 35        elem.removeClass('page_sel');
 36        elem.addClass('page_no');
 37        tagList.setListType('search');
 38        loadTags($('#tagKeyword').val());
 39    });
 40    // initialize history
 41    $.historyInit(loadTags, "tag-list");
 42    // start with char A
 43    var isFirstElem = true;
 44    $('.page_no').each(function() {
 45        // url with [A-Z]1, 
 46        $(this).attr('href', '#'+$(this).html());
 47        // so that we can easily handle browser back
 48        $(this).attr('id', 'tag-'+$(this).html());
 49        // override click
 50        $(this).click(function(){
 51            // for history management
 52            var hash = this.href;
 53            hash = hash.replace(/^.*#/, '');
 54            // moves to a new page. 
 55            // pageload is called at once. 
 56            // hash don't contain "#", "?"
 57            tagList.setListType('list');
 58            $.historyLoad(hash);
 59            return false;
 60        });
 61        // need to load first elements tag
 62        if(isFirstElem) {
 63            $(this).removeClass('page_no');
 64            $(this).addClass('page_sel');
 65            isFirstElem = false;
 66            loadTags($(this).html());
 67        }
 68    });
 69});
 70
 71var tagList = {};
 72tagList.setListType = function(listType) {
 73    this.type = listType;
 74}
 75tagList.getListType = function() {
 76    if(undefined == this.type)
 77        return 'list';
 78    return this.type;
 79}
 80function loadTags(keyword) {
 81    var url = '';
 82    if(tagList.getListType() == 'list') {
 83        url = ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'tags/list/'+ keyword + '.json');
 84        if(undefined == keyword || '' == keyword) {
 85            initial = 'A';
 86        }
 87        selectLink(keyword);
 88    }
 89    else {
 90        url = ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'tags/search?keyword='+ keyword);
 91    }
 92    $.get(
 93            url, 
 94            {}, 
 95            function (handle) {
 96                // render most recent idea
 97                if(ie.config.SUCCESS == handle.viewStatus.status) {
 98                    // TODO: Abhishek, Need to handle with Template :)
 99                    if(tagList.getListType() == 'search') {
100                        var html = ie.TagCloud.render(
101                                handle.viewStatus.data.tags, 
102                                {"maxWeight":10,"minWeight":10}, 
103                                {title : 'title', weightage : 'weightage', css: 'tag'},
104                                {url:ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'ideas/list?type=tag&tag=')}
105                            );
106                    }
107                    else {
108                        var html = ie.TagCloud.render(
109                            handle.viewStatus.data.tags, 
110                            handle.viewStatus.data.weights, 
111                            {title : 'title', weightage : 'weightage', css: 'tag'},
112                            {url:ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'ideas/list?type=tag&tag=')}
113                        );
114                    }
115                    $('#cloud-main').html(html);
116                    ie.progressEnd();
117                }
118                else if(ie.config.ERROR == handle.viewStatus.status) {
119                    $('#cloud-main').html(handle.viewStatus.messages.tags);
120                    ie.progressEnd();
121                }
122                else {
123                    ie.progressEnd();
124                    ie.globalError();
125                }
126            }, 
127            ie.config.RESPONSE_TYPE
128    );
129}
130
131function selectLink(initial) {
132    var selectedElem = $('.page_sel');
133    $(selectedElem).removeClass('page_sel');
134    $(selectedElem).addClass('page_no');
135    $('#tag-' + initial).removeClass('page_no');
136    $('#tag-' + initial).addClass('page_sel');
137}