2012-03-16 59 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。我希望这可以帮助其他任何可能存在此问题的人,但如果任何人对该名称不起作用有更好的解释,请发表评论。

非常感谢您的帮助。