2012-03-16 63 views
-2

我爲SharePoint 2010創建了一個.aspx網頁,該網頁使用JQuery從SharePoint列表中提取列表項數據,然後使用該數據填充Simile TimelineJavaScript函數未運行(控制檯返回函數「不是函數」)

該頁面包括過濾器,我相信使用某種形式的AJAX的,更新/過濾器什麼事件基於什麼是在文本框中輸入的時間軸上顯示(見例如here)。出於某種原因,當我在SharePoint上實現所有這些功能時(只需將.aspx文件和其他必需文件放入SharePoint庫中),除濾鏡外,其他所有功能都可以使用。當我檢查控制檯日誌(使用Firebug)時,給出的錯誤是

「onKeyPress不是函數」。

onKeyPress是參與執行過濾器的功能之一,但我不知道爲什麼瀏覽器是不是來自同一個.js文件中看到此功能 - 大量的其他功能(包括調用onkeypress事件的功能開始)運行得很好。更奇怪的是,如果我在函數運行之前放置一個斷點,然後讓它在停止後繼續運行,那麼一切正常。

這使我相信它可能是一個與時間有關的問題,但我一直在修改window.onload, $(document).ready,並且一個名爲_spBodyOnLoadFunctionName的SharePoint特定函數約一天,並且沒有看到對代碼行爲的影響。

,似乎是最相關的情況的文件是listdetails.aspx(下同)

<%@ Page Language="C#" MasterPageFile="~masterurl/default.master" inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" %> 

<asp:Content runat="server" ContentPlaceHolderID="PlaceHolderAdditionalPageHead"> 

    <script> 
     Timeline_ajax_url="timeline/timeline_ajax/simile-ajax-api.js"; 
     Timeline_urlPrefix='timeline/timeline_js/'; 
     Timeline_parameters='bundle=true'; 
    </script> 
    <script src="timeline/timeline_js/timeline-api.js" type="text/javascript"></script> 

    <script src="examples.js" type="text/javascript"></script> 
    <script> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> 
    <script src="tline.js" type="text/javascript"></script> 
    <script type="text/javascript"> 

     $(document).ready(function() { 
      // Build the URL of the Lists.asmx web service. 
      // This is done by stripping the last two parts (/doclib/page) of the URL. 
      var hrefParts = window.location.href.split('/'); 
      var wsURL = ""; 
      for (i = 0; i < (hrefParts.length - 2); i++) { 
       if (i > 0) 
        wsURL += "/"; 
       wsURL += hrefParts[i]; 
      } 
      wsURL += "/_vti_bin/lists.asmx"; 

      // The SOAP Envelope to send to the Lists.asmx web service. 
      // Tip: this XML can be copied from /_vti_bin/lists.asmx?op=GetListCollection 
      var soapEnv = 
       "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \ 
        <soapenv:Body xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \ 
         <GetListItems> \ 
          <listName>SimileData</listName> \ 
         </GetListItems> \ 
        </soapenv:Body> \ 
       </soapenv:Envelope>"; 

      // Do the web service call async. 
      $.ajax({ 
       url: wsURL, 
       type: "POST", 
       dataType: "xml", 
       data: soapEnv, 
       complete: processResult, 
       contentType: "text/xml; charset=\"utf-8\"" 
      }); 
     }); 

     window.onresize=onResize; 

    </script> 

</asp:Content> 
<asp:Content runat="server" ContentPlaceHolderID="PlaceHolderMain"> 
    <ul id="data"> 
    </ul> 
    <div id="body"> 
     <h1>Simile Timeline Demo</h1> 
     <p>Timeline version <span id='tl_ver'></span>.</p> 
     <script>Timeline.writeVersion('tl_ver')</script> 

     <div id="tl" class="timeline-default" style="height: 300px;"> 
     </div> 
     <div class="footnotes"> 
      <br /> <br /> 
     </div> 

     <div class="controls" id="controls"> 
     </div> 
     <div class="theButton" id="theButton"> 
      <button type="button">Load Timeline</button> 
     </div> 
    </div> 

</asp:Content> 
<asp:Content runat="server" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"> 
    List Details 
</asp:Content> 
<asp:Content runat="server" ContentPlaceHolderID="PlaceHolderPageTitle"> 
    List Details 
</asp:Content> 

tline.js(下同),

var tl; 
var eventSource = new Timeline.DefaultEventSource(); 

function processResult(xData, status) { 
    var tStart, tEnd, tLate, tEarly, tTitle, tText, tLink; 
    var tInst = true; 
    var evt; 
    $(xData.responseXML).find("z\\:row").each(function() { 
     tTitle = $(this).attr("ows_Title0").split("#")[1]; 
     tStart = Timeline.DateTime.parseGregorianDateTime($(this).attr("ows_Delivery_x0020_Date").split(" ")[0]); 
     tText = $(this).attr("ows_Description_x0020_of_x0020_Chang"); 
     tLink = $(this).attr("ows_ID"); 
     tLink = "some_link_stuff" + tLink + "some_link_stuff"; 

     var evt = new Timeline.DefaultEventSource.Event(tStart, tEnd, tLate, tEarly, tInst, tTitle, tText); 
     evt._start = tStart; 
     evt._end = tStart; 
     evt._earliestEnd = tStart; 
     evt._latestStart = tStart; 
     evt._description = tText; 
     evt._title = tTitle; 
     evt._text = tTitle; 
     evt._link = tLink; 
     eventSource.add(evt); 
    }); 

    onLoad(); 
} 

function onLoad() { 

    var theme = Timeline.ClassicTheme.create(); 
    theme.event.bubble.width = 250; 
    theme.mouseWheel = 'zoom'; 

    var rightNow = new Date(); 
    var theDay = rightNow.getDate(); 
    theDay = theDay.toString(); 
    if(theDay.length < 2){ 
     theDay = "0" + theDay; 
    } 
    var theYear = rightNow.getFullYear(); 
    var theMonth = rightNow.getMonth() + 1; 
    theMonth = theMonth.toString(); 
    if(theMonth.length < 2){ 
     theMonth = "0" + theMonth; 
    } 

    var theDate = theYear + "-" + theMonth + "-" + theDay; 
    var date = Timeline.DateTime.parseGregorianDateTime(theDate); 

    var bandInfos = [ 
     Timeline.createBandInfo({ 
      width:   "80%", 
      intervalUnit: Timeline.DateTime.MONTH, 
      intervalPixels: 600, 
      eventSource: eventSource, 
      zoomIndex:  10, 
      zoomSteps:  new Array(
       {pixelsPerInterval: 280, unit: Timeline.DateTime.HOUR}, 
       {pixelsPerInterval: 140, unit: Timeline.DateTime.HOUR}, 
       {pixelsPerInterval: 70, unit: Timeline.DateTime.HOUR}, 
       {pixelsPerInterval: 35, unit: Timeline.DateTime.HOUR}, 
       {pixelsPerInterval: 400, unit: Timeline.DateTime.DAY}, 
       {pixelsPerInterval: 200, unit: Timeline.DateTime.DAY}, 
       {pixelsPerInterval: 100, unit: Timeline.DateTime.DAY}, 
       {pixelsPerInterval: 50, unit: Timeline.DateTime.DAY}, 
       {pixelsPerInterval: 1000, unit: Timeline.DateTime.MONTH}, 
       {pixelsPerInterval: 800, unit: Timeline.DateTime.MONTH}, 
       {pixelsPerInterval: 600, unit: Timeline.DateTime.MONTH}, 
       {pixelsPerInterval: 400, unit: Timeline.DateTime.MONTH}, 
       {pixelsPerInterval: 200, unit: Timeline.DateTime.MONTH}, 
       {pixelsPerInterval: 100, unit: Timeline.DateTime.MONTH}, 
       {pixelsPerInterval: 50, unit: Timeline.DateTime.MONTH}, 
       {pixelsPerInterval: 400, unit: Timeline.DateTime.YEAR}, 
       {pixelsPerInterval: 200, unit: Timeline.DateTime.YEAR}, 
       {pixelsPerInterval: 100, unit: Timeline.DateTime.YEAR}, 
       {pixelsPerInterval: 50, unit: Timeline.DateTime.YEAR}, 
       {pixelsPerInterval: 400, unit: Timeline.DateTime.DECADE}, 
       {pixelsPerInterval: 200, unit: Timeline.DateTime.DECADE}, 
       {pixelsPerInterval: 100, unit: Timeline.DateTime.DECADE}, 
       {pixelsPerInterval: 50, unit: Timeline.DateTime.DECADE} 
      ), 
      date:   date, 
      theme:   theme 
     }), 
     Timeline.createBandInfo({ 
      width:   "20%", 
      intervalUnit: Timeline.DateTime.MONTH, 
      intervalPixels: 200, 
      eventSource: eventSource, 
      date:   date, 
      overview:  true, 
      theme:   theme 
     }) 
    ]; 
    bandInfos[1].syncWith = 0; 

    bandInfos[1].highlight = true; 

    tl = Timeline.create(document.getElementById("tl"), bandInfos, Timeline.HORIZONTAL); 
    // tl.loadXML("https://portal.gtri.gatech.edu/gtri/elsys/joshtest/experiment/elsys.xml", function(xml, url) { eventSource.loadXML(xml, url); }); 

    setupFilterHighlightControls(document.getElementById("controls"), tl, [0,1], theme); 
} 
var resizeTimerID = null; 
function onResize() { 
    if (resizeTimerID == null) { 
     resizeTimerID = window.setTimeout(function() { 
      resizeTimerID = null; 
      tl.layout(); 
     }, 500); 
    } 
} 

和examples.js(下)

function centerSimileAjax(date) { 
    tl.getBand(0).setCenterVisibleDate(SimileAjax.DateTime.parseGregorianDateTime(date)); 
} 

function setupFilterHighlightControls(div, timeline, bandIndices, theme) { 
    var table = document.createElement("table"); 
    var tr = table.insertRow(0); 

    var td = tr.insertCell(0); 
    td.innerHTML = "Filter:"; 

    td = tr.insertCell(1); 
    td.innerHTML = "Highlight:"; 

    var handler = function(elmt, evt, target) { 
     onKeyPress(timeline, bandIndices, table); 
    }; 

    tr = table.insertRow(1); 
    tr.style.verticalAlign = "top"; 

    td = tr.insertCell(0); 

    var input = document.createElement("input"); 
    input.type = "text"; 
    SimileAjax.DOM.registerEvent(input, "keypress", handler); 
    td.appendChild(input); 

    for (var i = 0; i < theme.event.highlightColors.length; i++) { 
     td = tr.insertCell(i + 1); 

     input = document.createElement("input"); 
     input.type = "text"; 
     SimileAjax.DOM.registerEvent(input, "keypress", handler); 
     td.appendChild(input); 

     var divColor = document.createElement("div"); 
     divColor.style.height = "0.5em"; 
     divColor.style.background = theme.event.highlightColors[i]; 
     td.appendChild(divColor); 
    } 

    td = tr.insertCell(tr.cells.length); 
    var button = document.createElement("button"); 
    button.innerHTML = "Clear All"; 
    SimileAjax.DOM.registerEvent(button, "click", function() { 
     clearAll(timeline, bandIndices, table); 
    }); 
    td.appendChild(button); 

    div.appendChild(table); 
} 

var timerID = null; 
function onKeyPress(timeline, bandIndices, table) { 
    if (timerID != null) { 
     window.clearTimeout(timerID); 
    } 
    timerID = window.setTimeout(function() { 
     performFiltering(timeline, bandIndices, table); 
    }, 300); 
} 
function cleanString(s) { 
    return s.replace(/^\s+/, '').replace(/\s+$/, ''); 
} 
function performFiltering(timeline, bandIndices, table) { 
    timerID = null; 

    var tr = table.rows[1]; 
    var text = cleanString(tr.cells[0].firstChild.value); 

    var filterMatcher = null; 
    if (text.length > 0) { 
     var regex = new RegExp(text, "i"); 
     filterMatcher = function(evt) { 
      return regex.test(evt.getText()) || regex.test(evt.getDescription()); 
     }; 
    } 

    var regexes = []; 
    var hasHighlights = false; 
    for (var x = 1; x < tr.cells.length - 1; x++) { 
     var input = tr.cells[x].firstChild; 
     var text2 = cleanString(input.value); 
     if (text2.length > 0) { 
      hasHighlights = true; 
      regexes.push(new RegExp(text2, "i")); 
     } else { 
      regexes.push(null); 
     } 
    } 
    var highlightMatcher = hasHighlights ? function(evt) { 
     var text = evt.getText(); 
     var description = evt.getDescription(); 
     for (var x = 0; x < regexes.length; x++) { 
      var regex = regexes[x]; 
      if (regex != null && (regex.test(text) || regex.test(description))) { 
       return x; 
      } 
     } 
     return -1; 
    } : null; 

    for (var i = 0; i < bandIndices.length; i++) { 
     var bandIndex = bandIndices[i]; 
     timeline.getBand(bandIndex).getEventPainter().setFilterMatcher(filterMatcher); 
     timeline.getBand(bandIndex).getEventPainter().setHighlightMatcher(highlightMatcher); 
    } 
    timeline.paint(); 
} 
function clearAll(timeline, bandIndices, table) { 
    var tr = table.rows[1]; 
    for (var x = 0; x < tr.cells.length - 1; x++) { 
     tr.cells[x].firstChild.value = ""; 
    } 

    for (var i = 0; i < bandIndices.length; i++) { 
     var bandIndex = bandIndices[i]; 
     timeline.getBand(bandIndex).getEventPainter().setFilterMatcher(null); 
     timeline.getBand(bandIndex).getEventPainter().setHighlightMatcher(null); 
    } 
    timeline.paint(); 
} 

未加載的函數(onKeyPress)在examples.js(上面)中。提前感謝您的幫助。

+0

我只是猜測,但我認爲onKeyPress是所有瀏覽器中的受保護關鍵字。你寫了examples.js嗎?你有沒有試過改變函數名? – dtryan 2012-03-16 15:50:14

+0

@dtryan剛剛在chrome中打開了控制檯,並且名爲onKeyPress的函數正常工作。 – Snuffleupagus 2012-03-16 15:53:23

+0

函數名稱應該都很好。我沒有編寫examples.js,我從[這個例子]中得到了它(http://simile-widgets.googlecode.com/svn/timeline/tags/latest/src/webapp/examples/jfk/jfk.html) 。 – user1226409 2012-03-16 16:00:13

回答

0

我試着改變函數的名稱,並以某種方式修復它,所以謝謝dtryan。然而,我真的不明白爲什麼這個名字至關重要,因爲,正如user1090190所說,在任何其他語境中名稱並不重要:直到我將它放在SharePoint內部時,代碼都工作得很好,而且我最重要的是搜索具有相同名稱的功能 - 內置瀏覽器功能甚至SharePoint功能 - 什麼都不返回,因此我不確定此問題來自何處。 SharePoint確實有稱爲OnKeyPress和onkeypress的函數,因此它們也許還有一些其他未公開的函數,稱爲onKeyPress。我希望這可以幫助其他任何可能存在此問題的人,但如果任何人對該名稱不起作用有更好的解釋,請發表評論。

非常感謝您的幫助。