【问题标题】:How to use input field inside docking panel of autodesk viewer api?如何在 Autodesk 查看器 api 的停靠面板内使用输入字段?
【发布时间】:2020-05-01 00:05:41
【问题描述】:

我试图在 Autodesk 查看器的停靠面板内添加增量和减量计数器输入。

我什至尝试在其中使用基本的文本输入字段。它不工作。面板与输入字段一起显示得很好。但是为什么我不能使用它。就像它是我未启用的输入字段一样,无法键入或更改它的值。在柜台的情况下,同样。

这是故意的吗?如果是这样,我已经看到使用类似输入的计数器的应用程序。谁能告诉我我的方法有什么问题?

我在下面给出的代码示例,

截图: https://i.stack.imgur.com/hyEBr.png

注意:我也试过用一个简单的文本输入字段代替计数器,它的工作方式相同

编辑:从容器中禁用移动处理程序解决了这个问题。来自 samuel-middendorp 的评论有帮助。

    <head>
        <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=no" />
        <meta charset="utf-8">

        <link rel="stylesheet" href="https://developer.api.autodesk.com/modelderivative/v2/viewers/7.*/style.min.css" type="text/css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://developer.api.autodesk.com/modelderivative/v2/viewers/7.*/viewer3D.min.js"></script>
        <style>
            body {
                margin: 0;
            }
            #forgeViewer {
                width: 100%;
                height: 100%;
                margin: 0;
                background-color: #F0F8FF;
            }

            </style>
    </head>
    <body>
        <div id="forgeViewer"></div>
    </body>
   <script>

   var viewer;
   var closable = null;
    var options = {
        env: 'AutodeskProduction',
        api: 'derivativeV2',  // for models uploaded to EMEA change this option to 'derivativeV2_EU'
        getAccessToken: function(onTokenReady) {
            var token = 'access token';
            var timeInSeconds = 3600; // Use value provided by Forge Authentication (OAuth) API
            onTokenReady(token, timeInSeconds);
        }
    };

SimplePanel = function(parentContainer, id, title, content, x, y)
{
  this.content = content;
Autodesk.Viewing.UI.DockingPanel.call(this, parentContainer, id, title,{shadow:false});

// Auto-fit to the content and don't allow resize.  Position at the coordinates given.

this.container.style.height = "250px";
this.container.style.width = "450px";
this.container.style.resize = "auto";
this.container.style.left = x + "px";
this.container.style.top = y + "px"; 
this.container.style.zIndex = 2;

};

SimplePanel.prototype = Object.create(Autodesk.Viewing.UI.DockingPanel.prototype);
SimplePanel.prototype.constructor = SimplePanel;

SimplePanel.prototype.initialize = function()
{ 
        this.title = this.createTitleBar(this.titleLabel || this.container.id);
this.container.appendChild(this.title);

this.container.appendChild(this.content);
this.initializeMoveHandlers(this.container);

this.closer = document.createElement("span");
this.closer = this.createCloseButton();
this.initializeCloseHandler(this.closer);
this.container.appendChild(this.closer);

var op = {left:false,heightAdjustment:45,marginTop:0};
this.scrollcontainer = this.createScrollContainer(op);

var id = viewer.getSelection();
var dataItem;
var data = [
    {
        id:"2648",
        name:"Chiller",
        temp:"300 deg",
        serviceReq:5,
        reservations:"3"
    },
    {
        id:"2228",
        name:"Door",
        temp:"150 deg",
        serviceReq:2,
        reservations:"4"
    },
    {
        id:"2198",
        name:"Cooler",
        temp:"400 deg",
        serviceReq:6,
        reservations:"2"
    }
]

data.forEach(item => {
    if(item.id == id){
        dataItem = item;
    }
})

//this is the portion I have the issue - here I appending an input tag inside of a table cell. But the problem is the input field is not focussing,can't able to edit, not enabled 

var html = [
    '<table>',
    '<thead>',
    '<th>Key</th><th>Value</th>',
    '</thead>',
    '<tbody>',
    '<tr><td>ID</td><td>'+dataItem.id+'</td></tr>',
    '<tr><td>Name</td><td>'+dataItem.name+'</td></tr>',
    '<tr><td>Temperature</td><td>'+dataItem.temp+'</td></tr>',
    '<tr><td>Service Requests</td><td contenteditable="true"> <input type=“text”/></td></tr>',
    '<tr><td>Reservations</td><td>'+dataItem.reservations+'</td></tr>',
    '</tbody>',
    '</table>',
].join('\n');


$(this.scrollContainer).append(html);

this.initializeMoveHandlers(this.title);    
};

    Autodesk.Viewing.Initializer(options, function() {   
    var htmlDiv = document.getElementById('forgeViewer');
    viewer = new Autodesk.Viewing.GuiViewer3D(htmlDiv,{ extensions: ['CustomPropertyPanelExtension'] });
    var startedCode = viewer.start();
    if (startedCode > 0) {
        console.error('Failed to create a Viewer: WebGL not supported.');
        return;
    }

    console.log('Initialization complete, loading a model next...');

});

var documentId = 'urn:my urn';
Autodesk.Viewing.Document.load(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);

function onDocumentLoadSuccess(viewerDocument) {

    var defaultModel = viewerDocument.getRoot().getDefaultGeometry();
    viewer.loadDocumentNode(viewerDocument, defaultModel);
    viewer.addEventListener( Autodesk.Viewing.SELECTION_CHANGED_EVENT, event=>{

        if( closable != null ) {
           closable.uninitialize();
           closable = null;
        }
        var content = document.createElement('div');
        var mypanel = new  SimplePanel(NOP_VIEWER.container,'mypanel','Asset properties',content,20,20);
        closable = mypanel;
        mypanel.setVisible(true);
})
}

function onDocumentLoadFailure() {
    console.error('Failed fetching Forge manifest');
}
   </script>
</html>


【问题讨论】:

  • 如果可以,请提供minimal reproducable example。这段代码中有很多内容,很难缩小范围。
  • 我已经编辑了我的代码 - 简化了一点。另外,我在这里甚至简化了部分var html = [ ‘&lt;div&gt;’ ‘&lt;input type=“text”/&gt;’ ‘&lt;/div&gt;’ ].join('\n');
  • 展望未来,您可以在 jsbin/codepen 或任何类似于this oi this 的平台上创建一个可重复性最低的示例,以帮助我们调查问题,我强烈建议您咨询一个同事/朋友/(网络开发)社区,对网络开发的接触最少,可以更快/更全面地回答您的问题(因为他们通常比 Forge 或 Viewer 更关心前端开发),以免妨碍您的进步...

标签: autodesk-forge autodesk-viewer


【解决方案1】:

编辑

如果您坚持让滚动容器可拖动,这对我来说看起来很反直觉,您可以轻松阻止输入上的事件(鼠标按下)被容器上启用的处理程序捕获/拦截(这让我们回到面板处理程序将捕获事件而不是让它们冒泡的反模式方式...

查看更新后的sample here

inputElement.addEventListener('mousedown',e=>e.stopPropagation())

原答案

很遗憾,我无法用我自己的示例 here 重现问题 - 输入功能与面板中的预期相同:

innerHTML=[
    '<table>',
    '<thead>',
    '<th>Key</th><th>Value</th>',
    '</thead>',
    '<tbody>',
    '<tr><td>ID</td><td>233</td></tr>',
    '<tr><td>Name</td><td>233</td></tr>',
    '<tr><td>Temperature</td><td>233</td></tr>',
    '<tr><td>Service Requests</td><td contenteditable="true"> <input type=“text”/></td></tr>',
    '<tr><td>Reservations</td><td>233</td></tr>',
    '</tbody>',
    '</table>',
].join('\n')  

看起来如果没有您的其他代码和/或可能有问题的 CSS,该问题无法完全重现,因此您可以克隆并添加自己的代码以重现错误并在下面发表评论,以便我知道要检查吗??

编辑

仍然无法重现该问题...在 Firefox (62.0) 和 Chrome (81) 上尝试了您的现场演示(工作一个 here,您的已过期...)并且输入按预期工作:

【讨论】:

  • 你能检查一下吗,请jsbin.com/vivodaq/edit?html,css,js,output
  • 点击节点时会打开一个停靠面板,点击部分请参考上图链接
  • 禁用移动处理程序由于某种原因解决了这个问题。也许移动处理程序会监听面板中的点击?从而覆盖面板中发生的任何其他操作?禁用容器上的移动处理程序(或者根本不添加它们)仍然允许在单击其标题栏时移动面板。
  • @Bryan 在我看来,最初在面板内的容器上初始化了移动处理程序。因此,这使得整个面板可以移动,甚至可以从容器内部移动。删除这些(或不添加)可以解决问题。您发布的新 jsbin 确实注释了 initializemovehandlers 行。如果您将其添加回来,则可以从面板中的任何位置移动面板,但我们会失去任何其他点击功能。
猜你喜欢
  • 2020-08-06
  • 1970-01-01
  • 2020-08-06
  • 1970-01-01
  • 1970-01-01
  • 2019-05-15
  • 2013-10-15
  • 2011-12-01
  • 2013-11-24
相关资源
最近更新 更多