# HG changeset patch # User Goffi # Date 1308440009 -7200 # Node ID 12680e220b35e6d2f4476c4242f6295a697d9c11 # Parent 975e6be24e118c0e761f32d4d63463bfaba35bba browser side: refactoring: menu is now in its own module diff -r 975e6be24e11 -r 12680e220b35 browser_side/menu.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/browser_side/menu.py Sun Jun 19 01:33:29 2011 +0200 @@ -0,0 +1,229 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +""" +Libervia: a Salut à Toi frontend +Copyright (C) 2011 Jérôme Poisson + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . +""" + +import pyjd # this is dummy in pyjs +from pyjamas.ui.SimplePanel import SimplePanel +from pyjamas.ui.VerticalPanel import VerticalPanel +from pyjamas.ui.HorizontalPanel import HorizontalPanel +from pyjamas.ui.DialogBox import DialogBox +from pyjamas.ui.MenuBar import MenuBar +from pyjamas.ui.MenuItem import MenuItem +from pyjamas.ui.ListBox import ListBox +from pyjamas.ui.Label import Label +from pyjamas.ui.TextBox import TextBox +from pyjamas.ui.Button import Button +from pyjamas.ui.HTML import HTML +from pyjamas.ui.Frame import Frame +from pyjamas import Window +from jid import JID +from tools import html_sanitize +import dialog +import re + +class MenuCmd: + + def __init__(self, object, handler): + self._object = object + self._handler = handler + + def execute(self): + handler = getattr(self._object, self._handler) + handler() + +class LiberviaMenuBar(MenuBar): + + def __init__(self): + MenuBar.__init__(self, vertical=False) + + def doItemAction(self, item, fireCommand): + MenuBar.doItemAction(self, item, fireCommand) + if item == self.items[-1] and self.popup: + self.popup.setPopupPosition(Window.getClientWidth() - + self.popup.getOffsetWidth() - 22, + self.getAbsoluteTop() + + self.getOffsetHeight() - 1) + self.popup.addStyleName('menuLastPopup') + + +class Menu(SimplePanel): + + def __init__(self, host): + self.host = host + SimplePanel.__init__(self) + self.setStyleName('menuContainer') + + menu_general = MenuBar(vertical=True) + menu_general.addItem("Social contract", MenuCmd(self, "onSocialContract")) + menu_general.addItem("About", MenuCmd(self, "onAbout")) + + menu_contacts = MenuBar(vertical=True) + menu_contacts.addItem("add contact", MenuCmd(self, "onAddContact")) + menu_contacts.addItem("update contact", MenuCmd(self, "onUpdateContact")) + menu_contacts.addItem("remove contact", MenuCmd(self, "onRemoveContact")) + + menu_group = MenuBar(vertical=True) + menu_group.addItem("join room", MenuCmd(self, "onJoinRoom")) + + menu_games = MenuBar(vertical=True) + menu_games.addItem("Tarot", MenuCmd(self, "onTarotGame")) + menu_games.addItem("Xiangqi", MenuCmd(self, "onXiangqiGame")) + + menubar = LiberviaMenuBar() + menubar.addItem(MenuItem("General", menu_general)) + menubar.addItem(MenuItem("Contacts", menu_contacts)) + menubar.addItem(MenuItem("Groups", menu_group)) + _separator = MenuItem('', None) + _separator.setStyleName('menuSeparator') + menubar.addItem(_separator, None) + menubar.addItem(MenuItem("Games", True, menu_games)) + self.add(menubar) + + #General menu + def onSocialContract(self): + _frame = Frame('contrat_social.html') + _frame.setStyleName('infoFrame') + _dialog = dialog.GenericDialog("Contrat Social", _frame) + _dialog.setSize('80%', '80%') + _dialog.show() + + def onAbout(self): + _about = HTML("""Libervia, a Salut à Toi project
+
+You can contact the author at goffi@goffi.org
+Blog available (mainly in french) at http://www.goffi.org
+Project page: http://wiki.goffi.org/wiki/Salut_à_Toi
+
+Any help welcome :) +

This project is dedicated to Roger Poisson

+""") + _dialog = dialog.GenericDialog("About", _about) + _dialog.show() + + #Contact menu + def onAddContact(self): + """Q&D contact addition""" + _dialog = None + edit = TextBox() + + def addContactCb(sender): + if not re.match(r'^.+@.+\..+',edit.getText(), re.IGNORECASE): + Window.alert('You must enter a valid contact JID (like "contact@libervia.org")') + _dialog.show() + else: + self.host.bridge.call('addContact', None, edit.getText(), '', _dialog.getSelectedGroups() ) + + label = Label("new contact identifier (JID):") + edit.setText('@libervia.org') + edit.setWidth('100%') + _dialog = dialog.GroupSelector([label, edit], self.host.contact_panel.getGroups(), [], addContactCb) + _dialog.setHTML('Adding contact') + _dialog.show() + + def onUpdateContact(self): + _dialog = None + _contacts_list = ListBox() + + def updateContactCb(sender): + _jid = _contacts_list.getValue(_contacts_list.getSelectedIndex()) + self.host.bridge.call('updateContact', None, _jid, '', _dialog.getSelectedGroups()) + + def onContactChange(_list): + _jid = _contacts_list.getValue(_contacts_list.getSelectedIndex()) + groups = self.host.contact_panel.getContactGroups(_jid) + _dialog.setGroupsSelected(groups) + + for contact in self.host.contact_panel.getContacts(): + _contacts_list.addItem(contact) + _contacts_list.addChangeListener(onContactChange) + _jid = _contacts_list.getValue(_contacts_list.getSelectedIndex()) + _selected_groups = self.host.contact_panel.getContactGroups(_jid) + _dialog = dialog.GroupSelector([Label('Which contact do you want to update ?'), _contacts_list], + self.host.contact_panel.getGroups(), _selected_groups, updateContactCb) + _dialog.setHTML('Updating contact') + _dialog.show() + + def onRemoveContact(self): + _dialog = None + _contacts_list = ListBox() + + def secondConfirmCb(confirm): + if confirm: + for contact in _contacts_list.getSelectedValues(): + self.host.bridge.call('delContact', None, contact) + else: + _dialog.show() + + def dialogCb(confirm): + if confirm: + html_list = ''.join(['
  • %s
  • ' % html_sanitize(contact) for contact in _contacts_list.getSelectedValues()]) + html_body = "Are you sure to remove the following contacts from your contact list ?
      %s
    " % html_list + dialog.ConfirmDialog(secondConfirmCb, html_body).show() + + for contact in self.host.contact_panel.getContacts(): + _contacts_list.addItem(contact) + _dialog = dialog.GenericConfirmDialog([_contacts_list], dialogCb, "Who do you want to remove from your contacts ?") + _dialog.show() + + #Group menu + def onJoinRoom(self): + _dialog = None + _edit = None + + def onOK(sender): + if not _edit.getText(): + Window.alert('You must enter a room jid in the form libervia@conference.libervia.org') + if self.host.whoami: + nick = self.host.whoami.node + self.host.bridge.call('joinMUC', None, _edit.getText(), nick) + _dialog.hide() + + def onCancel(sender): + _dialog.hide() + + _main_panel = VerticalPanel() + _label = Label("Discussion room:") + _edit = TextBox() + _edit.setText('libervia@conference.libervia.org') + hpanel = HorizontalPanel() + hpanel.add(_label) + hpanel.add(_edit) + _main_panel.add(hpanel) + button_panel = HorizontalPanel() + button_panel.add(Button("Join", onOK)) + button_panel.add(Button("Cancel", onCancel)) + _main_panel.add(button_panel) + _dialog = DialogBox(centered=True) + _dialog.setHTML('Group discussions') + _dialog.setWidget(_main_panel) + _dialog.show() + + + #Game menu + + def onTarotGame(self): + #Window.alert("Tarot selected") + #self.host.tab_panel.add(EmptyPanel(self.host), "Tarot") + def onPlayersSelected(other_players): + self.host.bridge.call('launchTarotGame', None, other_players) + dialog.ContactsChooser(self.host, onPlayersSelected, 3, text="Please select 3 other players").getContacts() + + def onXiangqiGame(self): + Window.alert("A Xiangqi game is planed, but not available yet") diff -r 975e6be24e11 -r 12680e220b35 browser_side/panels.py --- a/browser_side/panels.py Sun Jun 19 00:48:43 2011 +0200 +++ b/browser_side/panels.py Sun Jun 19 01:33:29 2011 +0200 @@ -21,7 +21,6 @@ import pyjd # this is dummy in pyjs from pyjamas.ui.SimplePanel import SimplePanel -from pyjamas.ui.FlowPanel import FlowPanel from pyjamas.ui.AbsolutePanel import AbsolutePanel from pyjamas.ui.VerticalPanel import VerticalPanel from pyjamas.ui.HorizontalPanel import HorizontalPanel @@ -29,226 +28,23 @@ from pyjamas.ui.TabPanel import TabPanel from pyjamas.ui.HTMLPanel import HTMLPanel from pyjamas.ui.Grid import Grid -from pyjamas.ui.DialogBox import DialogBox from pyjamas.ui.TextArea import TextArea -from pyjamas.ui.MenuBar import MenuBar -from pyjamas.ui.MenuBarPopupPanel import MenuBarPopupPanel -from pyjamas.ui.MenuItem import MenuItem -from pyjamas.ui.ListBox import ListBox from pyjamas.ui.Label import Label -from pyjamas.ui.TextBox import TextBox -from pyjamas.ui.Button import Button from pyjamas.ui.HTML import HTML -from pyjamas.ui.Frame import Frame from pyjamas.ui.DropWidget import DropWidget from pyjamas.ui.ClickListener import ClickHandler from pyjamas.ui.KeyboardListener import KEY_ENTER from pyjamas.Timer import Timer from pyjamas import Window from pyjamas import DOM -from __pyjamas__ import JS, doc - -from pyjamas.dnd import makeDraggable -from pyjamas.ui.DragWidget import DragWidget, DragContainer -from card_game import CardPanel +from card_game import CardPanel +from menu import Menu from jid import JID from tools import html_sanitize from datetime import datetime from time import time import dialog -import re - -class MenuCmd: - - def __init__(self, object, handler): - self._object = object - self._handler = handler - - def execute(self): - handler = getattr(self._object, self._handler) - handler() - -class LiberviaMenuBar(MenuBar): - - def __init__(self): - MenuBar.__init__(self, vertical=False) - - def doItemAction(self, item, fireCommand): - MenuBar.doItemAction(self, item, fireCommand) - if item == self.items[-1] and self.popup: - print "DEBUG" - print self.popup.getOffsetWidth() - self.popup.setPopupPosition(Window.getClientWidth() - - self.popup.getOffsetWidth() - 22, - self.getAbsoluteTop() + - self.getOffsetHeight() - 1) - self.popup.addStyleName('menuLastPopup') - - -class Menu(SimplePanel): - - def __init__(self, host): - self.host = host - SimplePanel.__init__(self) - self.setStyleName('menuContainer') - - menu_general = MenuBar(vertical=True) - menu_general.addItem("Social contract", MenuCmd(self, "onSocialContract")) - menu_general.addItem("About", MenuCmd(self, "onAbout")) - - menu_contacts = MenuBar(vertical=True) - menu_contacts.addItem("add contact", MenuCmd(self, "onAddContact")) - menu_contacts.addItem("update contact", MenuCmd(self, "onUpdateContact")) - menu_contacts.addItem("remove contact", MenuCmd(self, "onRemoveContact")) - - menu_group = MenuBar(vertical=True) - menu_group.addItem("join room", MenuCmd(self, "onJoinRoom")) - - menu_games = MenuBar(vertical=True) - menu_games.addItem("Tarot", MenuCmd(self, "onTarotGame")) - menu_games.addItem("Xiangqi", MenuCmd(self, "onXiangqiGame")) - - menubar = LiberviaMenuBar() - menubar.addItem(MenuItem("General", menu_general)) - menubar.addItem(MenuItem("Contacts", menu_contacts)) - menubar.addItem(MenuItem("Groups", menu_group)) - _separator = MenuItem('', None) - _separator.setStyleName('menuSeparator') - menubar.addItem(_separator, None) - menubar.addItem(MenuItem("Games", True, menu_games)) - self.add(menubar) - - #General menu - def onSocialContract(self): - _frame = Frame('contrat_social.html') - _frame.setStyleName('infoFrame') - _dialog = dialog.GenericDialog("Contrat Social", _frame) - _dialog.setSize('80%', '80%') - _dialog.show() - - def onAbout(self): - _about = HTML("""Libervia, a Salut à Toi project
    -
    -You can contact the author at goffi@goffi.org
    -Blog available (mainly in french) at http://www.goffi.org
    -Project page: http://wiki.goffi.org/wiki/Salut_à_Toi
    -
    -Any help welcome :) -

    This project is dedicated to Roger Poisson

    -""") - _dialog = dialog.GenericDialog("About", _about) - _dialog.show() - - #Contact menu - def onAddContact(self): - """Q&D contact addition""" - _dialog = None - edit = TextBox() - - def addContactCb(sender): - if not re.match(r'^.+@.+\..+',edit.getText(), re.IGNORECASE): - Window.alert('You must enter a valid contact JID (like "contact@libervia.org")') - _dialog.show() - else: - self.host.bridge.call('addContact', None, edit.getText(), '', _dialog.getSelectedGroups() ) - - label = Label("new contact identifier (JID):") - edit.setText('@libervia.org') - edit.setWidth('100%') - _dialog = dialog.GroupSelector([label, edit], self.host.contact_panel.getGroups(), [], addContactCb) - _dialog.setHTML('Adding contact') - _dialog.show() - - def onUpdateContact(self): - _dialog = None - _contacts_list = ListBox() - - def updateContactCb(sender): - _jid = _contacts_list.getValue(_contacts_list.getSelectedIndex()) - self.host.bridge.call('updateContact', None, _jid, '', _dialog.getSelectedGroups()) - - def onContactChange(_list): - _jid = _contacts_list.getValue(_contacts_list.getSelectedIndex()) - groups = self.host.contact_panel.getContactGroups(_jid) - _dialog.setGroupsSelected(groups) - - for contact in self.host.contact_panel.getContacts(): - _contacts_list.addItem(contact) - _contacts_list.addChangeListener(onContactChange) - _jid = _contacts_list.getValue(_contacts_list.getSelectedIndex()) - _selected_groups = self.host.contact_panel.getContactGroups(_jid) - _dialog = dialog.GroupSelector([Label('Which contact do you want to update ?'), _contacts_list], - self.host.contact_panel.getGroups(), _selected_groups, updateContactCb) - _dialog.setHTML('Updating contact') - _dialog.show() - - def onRemoveContact(self): - _dialog = None - _contacts_list = ListBox() - - def secondConfirmCb(confirm): - if confirm: - for contact in _contacts_list.getSelectedValues(): - self.host.bridge.call('delContact', None, contact) - else: - _dialog.show() - - def dialogCb(confirm): - if confirm: - html_list = ''.join(['
  • %s
  • ' % html_sanitize(contact) for contact in _contacts_list.getSelectedValues()]) - html_body = "Are you sure to remove the following contacts from your contact list ?
      %s
    " % html_list - dialog.ConfirmDialog(secondConfirmCb, html_body).show() - - for contact in self.host.contact_panel.getContacts(): - _contacts_list.addItem(contact) - _dialog = dialog.GenericConfirmDialog([_contacts_list], dialogCb, "Who do you want to remove from your contacts ?") - _dialog.show() - - #Group menu - def onJoinRoom(self): - _dialog = None - _edit = None - - def onOK(sender): - if not _edit.getText(): - Window.alert('You must enter a room jid in the form libervia@conference.libervia.org') - if self.host.whoami: - nick = self.host.whoami.node - self.host.bridge.call('joinMUC', None, _edit.getText(), nick) - _dialog.hide() - - def onCancel(sender): - _dialog.hide() - - _main_panel = VerticalPanel() - _label = Label("Discussion room:") - _edit = TextBox() - _edit.setText('libervia@conference.libervia.org') - hpanel = HorizontalPanel() - hpanel.add(_label) - hpanel.add(_edit) - _main_panel.add(hpanel) - button_panel = HorizontalPanel() - button_panel.add(Button("Join", onOK)) - button_panel.add(Button("Cancel", onCancel)) - _main_panel.add(button_panel) - _dialog = DialogBox(centered=True) - _dialog.setHTML('Group discussions') - _dialog.setWidget(_main_panel) - _dialog.show() - - - #Game menu - - def onTarotGame(self): - #Window.alert("Tarot selected") - #self.host.tab_panel.add(EmptyPanel(self.host), "Tarot") - def onPlayersSelected(other_players): - self.host.bridge.call('launchTarotGame', None, other_players) - dialog.ContactsChooser(self.host, onPlayersSelected, 3, text="Please select 3 other players").getContacts() - - def onXiangqiGame(self): - Window.alert("A Xiangqi game is planed, but not available yet") +from __pyjamas__ import doc class DropCell(DropWidget): """Cell in the middle grid which replace itself with the dropped widget on DnD"""