WME UI

UI Library for Waze Map Editor Greasy Fork scripts

当前为 2022-08-28 提交的版本,查看 最新版本

此脚本不应直接安装,它是供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/450320/1086733/WME%20UI.js

  1. // ==UserScript==
  2. // @name WME UI
  3. // @namespace https://greasyfork.org/users/227648-anton-shevchuk
  4. // @version 0.0.1
  5. // @description UI Library for Waze Map Editor Greasy Fork scripts
  6. // @license MIT License
  7. // @match https://www.waze.com/editor*
  8. // @match https://www.waze.com/*/editor*
  9. // @match https://beta.waze.com/editor*
  10. // @match https://beta.waze.com/*/editor*
  11. // @exclude https://www.waze.com/user/editor*
  12. // @exclude https://beta.waze.com/user/editor*
  13. // @icon https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https://anton.shevchuk.name&size=64
  14. // @grant none
  15. // ==/UserScript==
  16.  
  17. /* jshint esversion: 8 */
  18.  
  19. /* global jQuery, W */
  20.  
  21. class WMEUI {
  22. /**
  23. * Normalize title or UID
  24. * @param string
  25. * @returns {string}
  26. */
  27. static normalize (string) {
  28. return string.replace(/\W+/gi, '-').toLowerCase()
  29. }
  30.  
  31. /**
  32. * Apply CSS styles
  33. */
  34. static addStyle (css) {
  35. let style = document.createElement('style')
  36. style.type = 'text/css' // is required
  37. style.innerHTML = css
  38. document.querySelector('head').appendChild(style)
  39. }
  40.  
  41. /**
  42. * @param {String} uid
  43. * @param {Object} data
  44. */
  45. static addTranslation (uid, data) {
  46. if (!data.en) {
  47. console.error('Default translation `en` is required')
  48. }
  49. let locale = I18n.currentLocale()
  50. I18n.translations[locale][uid] = data[locale] || data.en
  51. }
  52. }
  53.  
  54. /**
  55. * Based on the code from the WazeWrap library
  56. */
  57. class WMEUIShortcut {
  58. /**
  59. * @param {String} name
  60. * @param {String} desc
  61. * @param {String} group
  62. * @param {String} title
  63. * @param {String} shortcut
  64. * @param {Function} callback
  65. * @param {Object} scope
  66. * @return {WMEUIShortcut}
  67. */
  68. constructor (name, desc, group, title, shortcut, callback, scope= null) {
  69. this.name = name
  70. this.desc = desc
  71. this.group = group || 'default'
  72. this.title = title
  73. this.shortcut = {}
  74. this.callback = callback
  75. this.scope = ('object' === typeof scope) ? scope : null
  76.  
  77. /* Setup translation for shortcut */
  78. if (shortcut.length > 0) {
  79. this.shortcut = {[shortcut]:name}
  80. WMEUIShortcut.addTranslation(this.group, this.title, this.name, this.desc)
  81. }
  82.  
  83. /* Try to initialize new group */
  84. this.addGroup()
  85.  
  86. /* Clear existing actions with same name and create new */
  87. this.addAction()
  88.  
  89. /* Try to register new event */
  90. this.addEvent()
  91.  
  92. /* Finally, register the shortcut */
  93. this.registerShortcut()
  94. }
  95.  
  96. /**
  97. * @param {String} group name
  98. * @param {String} title of the shortcut section
  99. * @param {String} name of the shortcut
  100. * @param {String} description of the shortcut
  101. */
  102. static addTranslation(group, title, name, description) {
  103. if (!I18n.translations[I18n.currentLocale()].keyboard_shortcuts.groups[group]) {
  104. I18n.translations[I18n.currentLocale()].keyboard_shortcuts.groups[group] = {
  105. description: title,
  106. members: {
  107. [name]: description
  108. }
  109. }
  110. }
  111. I18n.translations[I18n.currentLocale()].keyboard_shortcuts.groups[group].members[name] = description
  112. }
  113.  
  114. /**
  115. * Determines if the shortcut's action already exists.
  116. * @private
  117. */
  118. doesGroupExist () {
  119. return 'undefined' !== typeof W.accelerators.Groups[this.group]
  120. && 'undefined' !== typeof W.accelerators.Groups[this.group].members
  121. }
  122.  
  123. /**
  124. * Determines if the shortcut's action already exists.
  125. * @private
  126. */
  127. doesActionExist () {
  128. return 'undefined' !== typeof W.accelerators.Actions[this.name]
  129. }
  130.  
  131. /**
  132. * Determines if the shortcut's event already exists.
  133. * @private
  134. */
  135. doesEventExist () {
  136. return 'undefined' !== typeof W.accelerators.events.dispatcher._events[this.name]
  137. && W.accelerators.events.dispatcher._events[this.name].length > 0
  138. && this.callback === W.accelerators.events.dispatcher._events[this.name][0].func
  139. && this.scope === W.accelerators.events.dispatcher._events[this.name][0].obj
  140. }
  141.  
  142. /**
  143. * Creates the shortcut's group.
  144. * @private
  145. */
  146. addGroup () {
  147. if (this.doesGroupExist()) return
  148.  
  149. W.accelerators.Groups[this.group] = []
  150. W.accelerators.Groups[this.group].members = []
  151. }
  152.  
  153. /**
  154. * Registers the shortcut's action.
  155. * @private
  156. */
  157. addAction () {
  158. if (this.doesActionExist()) {
  159. W.accelerators.Actions[this.name] = null
  160. }
  161. W.accelerators.addAction(this.name, { group: this.group })
  162. }
  163.  
  164. /**
  165. * Registers the shortcut's event.
  166. * @private
  167. */
  168. addEvent () {
  169. if (this.doesEventExist()) return
  170. W.accelerators.events.register(this.name, this.scope, this.callback)
  171. }
  172.  
  173. /**
  174. * Registers the shortcut's keyboard shortcut.
  175. * @private
  176. */
  177. registerShortcut () {
  178. W.accelerators._registerShortcuts(this.shortcut)
  179. }
  180. }