commands.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # -*- coding: utf-8 -*-
  2. from . import settings as USettings
  3. from six.moves.urllib.parse import urljoin
  4. class UEditorEventHandler(object):
  5. """用来处理UEditor的事件侦听"""
  6. def on_selectionchange(self):
  7. return ""
  8. def on_contentchange(self):
  9. return ""
  10. def render(self, editorID):
  11. jscode = """
  12. %(editor)s.addListener('%(event)s', function () {
  13. %(event_code)s
  14. });"""
  15. event_codes = []
  16. # 列出所有on_打头的方法,然后在ueditor中进行侦听
  17. events = filter(lambda x: x[0:3] == "on_", dir(self))
  18. for event in events:
  19. try:
  20. event_code = getattr(self, event)()
  21. if event_code:
  22. event_code = event_code % {"editor": editorID}
  23. event_codes.append(jscode % {"editor": editorID, "event": event[
  24. 3:], "event_code": event_code})
  25. except:
  26. pass
  27. if len(event_codes) == 0:
  28. return ""
  29. else:
  30. return "\n".join(event_codes)
  31. class UEditorCommand(object):
  32. """
  33. 为前端增加按钮,下拉等扩展,
  34. """
  35. def __init__(self, **kwargs):
  36. self.uiName = kwargs.pop("uiName", "")
  37. self.index = kwargs.pop("index", 0)
  38. self.title = kwargs.pop("title", self.uiName)
  39. self.ajax_url = kwargs.pop("ajax_url", "")
  40. def render_ui(self, editor):
  41. """" 创建ueditor的ui扩展对象的js代码,如button,combo等 """
  42. raise NotImplementedError
  43. def render_ajax_command(self):
  44. """"生成通过ajax调用后端命令的前端ajax代码"""
  45. if not self.ajax_url:
  46. return ""
  47. return u"""
  48. UE.ajax.request( '%(ajax_url)s', {
  49. data: {
  50. name: 'ueditor'
  51. },
  52. onsuccess: function ( xhr ) {%(ajax_success)s},
  53. onerror: function ( xhr ){ %(ajax_error)s }
  54. });
  55. """ % {
  56. "ajax_url": self.ajax_url,
  57. "ajax_success": self.onExecuteAjaxCommand("success"),
  58. "ajax_error": self.onExecuteAjaxCommand("error")
  59. }
  60. def render_command(self):
  61. """" 返回注册命令的js定义 """
  62. cmd = self.onExecuteCommand()
  63. ajax_cmd = self.render_ajax_command()
  64. queryvalue_command = self.onExecuteQueryvalueCommand()
  65. cmds = []
  66. if cmd or ajax_cmd:
  67. cmds.append( u"""execCommand: function() {
  68. %(exec_cmd)s
  69. %(exec_ajax_cmd)s
  70. }
  71. """ % {"exec_cmd": cmd, "exec_ajax_cmd": ajax_cmd},)
  72. if queryvalue_command:
  73. cmds.append(u"""queryCommandValue:function(){
  74. %s
  75. }""" % queryvalue_command)
  76. if len(cmds) > 0:
  77. return u"""
  78. editor.registerCommand(uiName, {
  79. %s
  80. });
  81. """ % ",".join(cmds)
  82. else:
  83. return ""
  84. def render(self, editorID):
  85. return u"""
  86. UE.registerUI("%(uiName)s", function(editor, uiName) {
  87. %(registerCommand)s
  88. %(uiObject)s
  89. },%(index)s,"%(editor)s");
  90. """ % {
  91. "registerCommand": self.render_command(),
  92. "uiName": self.uiName,
  93. "uiObject": self.render_ui(editorID),
  94. "index": self.index,
  95. "editor": editorID
  96. }
  97. def onExecuteCommand(self):
  98. """ 返回执行Command时的js代码 """
  99. return ""
  100. def onExecuteAjaxCommand(self, state):
  101. """ 返回执行Command时发起Ajax调用成功与失败的js代码 """
  102. return ""
  103. def onExecuteQueryvalueCommand(self):
  104. """" 返回执行QueryvalueCommand时的js代码 """
  105. return ""
  106. class UEditorButtonCommand(UEditorCommand):
  107. def __init__(self, **kwargs):
  108. self.icon = kwargs.pop("icon", "")
  109. super(UEditorButtonCommand, self).__init__(**kwargs)
  110. def onClick(self):
  111. """"按钮单击js代码,默认执行uiName命令,默认会调用Command """
  112. return """
  113. editor.execCommand(uiName);
  114. """
  115. def render_ui(self, editorID):
  116. """ 创建button的js代码: """
  117. return """
  118. var btn = new UE.ui.Button({
  119. name: uiName,
  120. title: "%(title)s",
  121. cssRules: "background-image:url('%(icon)s')!important;",
  122. onclick: function() {
  123. %(onclick)s
  124. }
  125. });
  126. return btn
  127. """ % {
  128. "icon": urljoin(USettings.gSettings.MEDIA_URL, self.icon),
  129. "onclick": self.onClick(),
  130. "title": self.title
  131. }
  132. class UEditorComboCommand(UEditorCommand):
  133. def __init__(self, **kwargs):
  134. self.items = kwargs.pop("items", [])
  135. self.initValue = kwargs.pop("initValue", "")
  136. super(UEditorComboCommand, self).__init__(**kwargs)
  137. def get_items(self):
  138. return self.items
  139. def onSelect(self):
  140. return ""
  141. def render_ui(self, editorID):
  142. """ 创建combo的js代码: """
  143. return """
  144. var combox = new UE.ui.Combox({
  145. editor:editor,
  146. items:%(items)s,
  147. onselect:function (t, index) {
  148. %(onselect)s
  149. },
  150. title:'%(title)s',
  151. initValue:'%(initValue)s'
  152. });
  153. return combox;
  154. """ % {
  155. "title": self.title,
  156. "items": str(self.get_items()),
  157. "onselect": self.onSelect(),
  158. "initValue": self.initValue
  159. }
  160. class UEditorDialogCommand(UEditorCommand):
  161. pass