connors / photon

The fastest way to build beautiful Electron apps using simple HTML and CSS
photonkit.com
MIT License
10.01k stars 580 forks source link

menu.js error (missing: .js-context-menu) #49

Open ungoldman opened 8 years ago

ungoldman commented 8 years ago

I'm noticing dist/template-app/menu.js is trying to attach a click event to .js-context-menu but there's no element in dist/template-app/index.html that matches that. I looked around in each of the releases and wasn't able to find it there either.

Uncaught TypeError: Cannot read property 'addEventListener' of null
  (anonymous function) @ menu.js:24

https://github.com/connors/photon/blob/master/dist/template-app/js/menu.js#L24

Really cool project btw. Looks fantastic.

developit commented 8 years ago

I believe it's supposed to be referring to something similar to this: https://github.com/connors/photon/blob/master/docs/demo-app.html#L51-L53

... but that doesn't appear to have been included in the template-app example.

ungoldman commented 8 years ago

I think it's an incomplete adaptation of https://github.com/atom/electron/blob/master/docs/api/menu.md.

window.addEventListener('contextmenu', function (e) {
  e.preventDefault();
  menu.popup(remote.getCurrentWindow());
}, false);

One way around the error produced by the current implementation in this repo would be to modify it like this:

document.addEventListener('DOMContentLoaded', function () {
  var nodeList = document.querySelectorAll('.js-context-menu')

  Array.prototype.forEach.call(nodeList, function (item) {
    item.addEventListener('contextmenu', function (event) {
      menu.popup(remote.getCurrentWindow());
    })
  })
})

That way there will be no error if the node list is empty. I'm using this workaround in https://github.com/ngoldman/proton without any issues so far.