source: oceandb/jQuery_Prototype/script/oceandb.js @ 65

Last change on this file since 65 was 65, checked in by jazz, 15 years ago
  • 改寫成從 data/map-menu.json 讀取 map-menu 的選單設定
File size: 2.8 KB
Line 
1// 註冊 onReady Event
2// 參考: http://docs.jquery.com/Events
3$(document).ready(function() {
4
5  var PARENT_ID = [];   // 產生一個空的陣列
6  PARENT_ID[1] = "定點";
7  PARENT_ID[2] = "船測";
8
9  // 註冊 AJAX 非同步處理函式
10  // 從 data/map-menu.json 讀取資料來產生 map-menu 的 Accordion 選單
11  $.getJSON("data/map-menu.json", function(data){
12    // 讀取進來的 JSON 內容必須用 eval 函數才能變成 javascript 可以識別的陣列
13    var json = eval(data);
14    // 註: 因為 JSON 內容按 type_id 排序,
15    //     故用 type_id 來判斷第一次出現的 type_id 產生新的 DOM
16    var type_id = -1;
17    $.each(json, function(i, item){
18      // 確認 type_id 是否存在,否則產生一個新的 DOM
19      if(item.type_id != type_id )
20      {     
21  $('#map-menu').append("<div><h3><a href='#'>" + item.type_name 
22            + " (" + PARENT_ID[item.parent_id] + ")</a>"
23            + "</h3><div><ul id='map-menu-" + item.type_id 
24            + "'></ul><br/></div></div>");
25  type_id = item.type_id;
26  $("#map-menu-" + item.type_id).append("<li><input type='checkbox'>"
27           + "<font color='red'><b>=== 以下全選 ===</b></font>"
28           + "</input></li>");
29  var temp = $("#map-menu-" + item.type_id).find("input");
30  temp.click(function(){
31    // TODO: 加入全選處理函式
32    this.checked ? alert("map-menu-" + item.type_id + " is clicked!") : 
33       alert("map-menu-" + item.type_id + " is unclicked!");
34  });
35      }
36      // 根據 type_id 逐一加入 owner_org
37      $("#map-menu-" + item.type_id).append("<li><input type='checkbox'>"
38         + item.owner_org + "</input></li>");
39
40      // TODO: 定義 checkbox checked 跟 unchecked 對應的處理函式
41    });
42
43    // 設定左側選單的 Accordion 風格
44    // 註: 這行的擺放位置很重要,放在迴圈外會造成 CSS 樣式(class)的問題
45    // 主因: getJSON 是非同步處理,得把動態產生 DOM 的相關設定擺在 callback 中
46    $('#map-menu').accordion({ header: "h3" });
47
48    // 完成從 JSON 讀入資料,因此把 loading 提示移除。
49    $('#loading').remove();
50  });
51
52  // 設定地圖高度
53  var map_height=document.documentElement.clientHeight - 68;
54  $('#main').css( { height: map_height } );
55
56  // 設定點選 "MENU" 的行為
57  $('#btnMenu').click(function()
58  {
59    if($('#map-menu').css("display") != "none")
60    {
61      $('#map-menu').css({ display: 'none' });
62      $('#map').css({ width: '100%' });
63    } else {
64      $('#map-menu').css({ display: 'block'});
65      $('#map').css({ width: '80%'});
66    }
67  });
68});
69
70// 註冊 window 物件的 onResize Event
71// 參考: http://docs.jquery.com/Events
72$(window).resize(function() {
73  // 當改變瀏覽器大小時,重新設定地圖高度
74  var map_height=document.documentElement.clientHeight - 68;
75  $('#main').css( { height: map_height } );
76});
Note: See TracBrowser for help on using the repository browser.