WazeWrap Beta

A base library for WME script writers

当前为 2017-01-03 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/24870/167249/WazeWrap%20Beta.js

  1. // ==UserScript==
  2. // @name WazeWrap Beta
  3. // @namespace https://gf.qytechs.cn/users/30701-justins83-waze
  4. // @version 0.1.10
  5. // @description A base library for WME script writers
  6. // @author JustinS83/MapOMatic
  7. // @include https://beta.waze.com/*editor/*
  8. // @include https://www.waze.com/*editor/*
  9. // @exclude https://www.waze.com/*user/editor/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. /* global W */
  14. /* global WazeWrapBeta */
  15. var WazeWrapBeta = {};
  16.  
  17. (function() {
  18.  
  19. function bootstrap(tries) {
  20. tries = tries || 1;
  21. if (window.W &&
  22. window.W.map &&
  23. window.W.model &&
  24. window.W.loginManager &&
  25. $) {
  26. init();
  27. } else if (tries < 1000) {
  28. setTimeout(function () { bootstrap(tries++); }, 200);
  29. } else {
  30. console.log('WazeWrap failed to load');
  31. }
  32. }
  33.  
  34. bootstrap();
  35.  
  36. function init(){
  37. console.log("WazeWrapBeta initializing...");
  38. var oldLib = window.WazeWrap;
  39. var root = this;
  40.  
  41. WazeWrapBeta .Version = GM_info.script.version;
  42. WazeWrapBeta .isBetaEditor = /beta/.test(location.href);
  43.  
  44. SetUpRequire();
  45.  
  46. WazeWrapBeta .test = "test";
  47. WazeWrapBeta .Geometry = new Geometry;
  48. WazeWrapBeta .Model = new Model;
  49. WazeWrapBeta .Interface = new Interface;
  50. WazeWrapBeta .User = new User;
  51. WazeWrapBeta .Util = new Util;
  52. root.WazeWrapBeta = WazeWrapBeta ;
  53.  
  54. console.log('WazeWrapBeta Loaded');
  55. };
  56.  
  57.  
  58. function SetUpRequire(){
  59. //if(this.isBetaEditor || typeof window.require !== "undefined")
  60. // return;
  61.  
  62. console.log("Setting d2's require fix...");
  63.  
  64. // setup one global var and put all in
  65. var WMEAPI = {};
  66.  
  67. // detect URL of WME source code
  68. WMEAPI.scripts = document.getElementsByTagName('script');
  69. WMEAPI.url=null;
  70. for (i=0;i<WMEAPI.scripts.length;i++){
  71. if (WMEAPI.scripts[i].src.indexOf('/assets-editor/js/app')!=-1)
  72. {
  73. WMEAPI.url=WMEAPI.scripts[i].src;
  74. break;
  75. }
  76. }
  77. if (WMEAPI.url==null)
  78. throw new Error("WME Hack: can't detect WME main JS");
  79.  
  80.  
  81. // setup a fake require and require.define
  82. WMEAPI.require=function (e) {
  83. if (WMEAPI.require.define.modules.hasOwnProperty(e))
  84. return WMEAPI.require.define.modules[e];
  85. else
  86. console.error('Require failed on ' + e, WMEAPI.require.define.modules);
  87. return null;
  88. };
  89.  
  90. WMEAPI.require.define=function (m) {
  91. if (WMEAPI.require.define.hasOwnProperty('modules')==false)
  92. WMEAPI.require.define.modules={};
  93. for (var p in m){
  94. WMEAPI.require.define.modules[p]=m[p];
  95. }
  96. };
  97.  
  98. // save the original webpackJsonp function
  99. WMEAPI.tmp = window.webpackJsonp;
  100.  
  101. // taken from WME code: this function is a wrapper that setup the API and may call recursively other functions
  102. WMEAPI.t = function (n) {
  103. if (WMEAPI.s[n]) return WMEAPI.s[n].exports;
  104. var r = WMEAPI.s[n] = {
  105. exports: {},
  106. id: n,
  107. loaded: !1
  108. };
  109. return WMEAPI.e[n].call(r.exports, r, r.exports, WMEAPI.t), r.loaded = !0, r.exports;
  110. };
  111. console.log("0.1.10");
  112. // e is a copy of all WME funcs because function t need to access to this list
  113. WMEAPI.e=[];
  114.  
  115. // the patch
  116. window.webpackJsonp = function(a, i) {
  117. // our API but we will use it only to build the require stuffs
  118. var api={};
  119. // taken from WME code. a is [1], so...
  120. for (var o, d, u = 0, l = []; u < a.length; u++) d = a[u], WMEAPI.r[d] && l.push.apply(l, WMEAPI.r[d]), WMEAPI.r[d] = 0;
  121. var unknownCount=0;
  122. var classname, funcStr;
  123. // copy i in e and keep a link from classname to index in e
  124. for (o in i){
  125. WMEAPI.e[o] = i[o];
  126. funcStr = i[o].toString();
  127. classname = funcStr.match(/CLASS_NAME:\"([^\"]*)\"/);
  128. if (classname){
  129. // keep the link.
  130. api[classname[1].replace(/\./g,'/').replace(/^W\//, 'Waze/')]={index: o, func: WMEAPI.e[o]};
  131. }
  132. else{
  133. api['Waze/Unknown/' + unknownCount]={index: o, func: WMEAPI.e[o]};
  134. unknownCount++;
  135. }
  136. }
  137. // taken from WME code: it calls the original webpackJsonp and do something else, but I don't really know what.
  138. // removed the call to the original webpackJsonp: still works...
  139. //for (tmp && tmp(a, i); l.length;) l.shift().call(null, t);
  140. for (; l.length;) l.shift().call(null, WMEAPI.t);
  141. WMEAPI.s[0] = 0;
  142. // run the first func of WME. This first func will call recusrsively all funcs needed to setup the API.
  143. // After this call, s will contain all instanciables classes.
  144. //var ret = WMEAPI.t(0);
  145. // now, build the requires thanks to the link we've built in var api.
  146. var module={};
  147. var apiFuncName;
  148. unknownCount=0;
  149. for (o in i){
  150. funcStr = i[o].toString();
  151. classname = funcStr.match(/CLASS_NAME:\"([^\"]*)\"/);
  152. if (classname){
  153. module={};
  154. apiFuncName = classname[1].replace(/\./g,'/').replace(/^W\//, 'Waze/');
  155. module[apiFuncName]=WMEAPI.t(api[apiFuncName].index);
  156. WMEAPI.require.define(module);
  157. }
  158. else{
  159. var matches = funcStr.match(/SEGMENT:"segment",/);
  160. if (matches){
  161. module={};
  162. apiFuncName='Waze/Model/ObjectType';
  163. module[apiFuncName]=WMEAPI.t(api['Waze/Unknown/' + unknownCount].index);
  164. WMEAPI.require.define(module);
  165. }
  166. unknownCount++;
  167. }
  168. }
  169.  
  170. // restore the original func
  171. window.webpackJsonp=WMEAPI.tmp;
  172.  
  173. // set the require public if needed
  174. // if so: others scripts must wait for the window.require to be available before using it.
  175. WazeWrapBeta.require = WMEAPI.require;
  176. console.log(WMEAPI.require);
  177. console.log(WazeWrapBeta.require);
  178. // all available functions are in WMEAPI.require.define.modules
  179. // console.debug this variable to read it:
  180. // console.debug('Modules: ', WMEAPI.require.define.modules);
  181. // run your script here:
  182. // setTimeout(yourscript);
  183. // again taken from WME code. Not sure about what it does.
  184. //if (i[0]) return ret;
  185. };
  186.  
  187. // some kind of global vars and init
  188. WMEAPI.s = {};
  189. WMEAPI.r = {
  190. 0: 0
  191. };
  192.  
  193. // hacking finished
  194.  
  195. // load again WME through our patched func
  196. WMEAPI.WMEHACK_Injected_script = document.createElement("script");
  197. WMEAPI.WMEHACK_Injected_script.setAttribute("type", "application/javascript");
  198. WMEAPI.WMEHACK_Injected_script.src = WMEAPI.url;
  199. document.body.appendChild(WMEAPI.WMEHACK_Injected_script);
  200. console.log("d2 fix complete");
  201. }
  202.  
  203. function Geometry(){
  204. //var geometry = WazeWrap.Geometry;
  205.  
  206. //Converts to "normal" GPS coordinates
  207. this.ConvertTo4326 = function (long, lat){
  208. var projI=new OpenLayers.Projection("EPSG:900913");
  209. var projE=new OpenLayers.Projection("EPSG:4326");
  210. return (new OpenLayers.LonLat(long, lat)).transform(projI,projE);
  211. };
  212. this.ConvertTo900913 = function (long, lat){
  213. var projI=new OpenLayers.Projection("EPSG:900913");
  214. var projE=new OpenLayers.Projection("EPSG:4326");
  215. return (new OpenLayers.LonLat(long, lat)).transform(projE,projI);
  216. };
  217.  
  218. //Converts the Longitudinal offset to an offset in 4326 gps coordinates
  219. this.CalculateLongOffsetGPS = function(longMetersOffset, long, lat)
  220. {
  221. var R= 6378137; //Earth's radius
  222. var dLon = longMetersOffset / (R * Math.cos(Math.PI * lat / 180)); //offset in radians
  223. var lon0 = dLon * (180 / Math.PI); //offset degrees
  224.  
  225. return lon0;
  226. };
  227.  
  228. //Converts the Latitudinal offset to an offset in 4326 gps coordinates
  229. this.CalculateLatOffsetGPS = function(latMetersOffset, lat)
  230. {
  231. var R= 6378137; //Earth's radius
  232. var dLat = latMetersOffset/R;
  233. var lat0 = dLat * (180 /Math.PI); //offset degrees
  234.  
  235. return lat0;
  236. };
  237. /**
  238. * Checks if the given geometry is on screen
  239. * @function WazeWrap.Model.isGeometryInMapExtent
  240. * @param {OpenLayers.Geometry} Geometry to check if any part of is in the current viewport
  241. */
  242. this.isLonLatInMapExtent = function (lonLat) {
  243. 'use strict';
  244. return lonLat && W.map.getExtent().containsLonLat(lonLat);
  245. };
  246. /**
  247. * Checks if the given geometry is on screen
  248. * @function WazeWrap.Model.isGeometryInMapExtent
  249. * @param {OpenLayers.Geometry} Geometry to check if any part of is in the current viewport
  250. */
  251. this.isGeometryInMapExtent = function (geometry) {
  252. 'use strict';
  253. return geometry && geometry.getBounds &&
  254. W.map.getExtent().intersectsBounds(geometry.getBounds());
  255. };
  256. /**
  257. * Calculates the distance between two given points
  258. * @function WazeWrap.Model.calculateDistance
  259. * @param {OpenLayers.Geometry.Point} An array of OL.Geometry.Point with which to measure the total distance. A minimum of 2 points is needed.
  260. */
  261. this.calculateDistance = function(pointArray) {
  262. if(pointArray.length < 2)
  263. return 0;
  264.  
  265. var line = new OpenLayers.Geometry.LineString(pointArray);
  266. length = line.getGeodesicLength(W.map.getProjectionObject());
  267. return length; //multiply by 3.28084 to convert to feet
  268. };
  269.  
  270. this.findClosestSegment = function(mygeometry){
  271. var onscreenSegments = WazeWrapBeta.Model.getOnscreenSegments();
  272. var minDistance = Infinity;
  273. var closestSegment;
  274. for (s in onscreenSegments) {
  275. if (!onscreenSegments.hasOwnProperty(s))
  276. continue;
  277.  
  278. segmentType = onscreenSegments[s].attributes.roadType;
  279. if (segmentType === 10 || segmentType === 3 || segmentType === 16 || segmentType === 18 || segmentType === 19) //10 ped boardwalk, 16 stairway, 18 railroad, 19 runway, 3 freeway
  280. continue;
  281.  
  282. distanceToSegment = mygeometry.distanceTo(onscreenSegments[s].geometry, {details: true});
  283.  
  284. if (distanceToSegment.distance < minDistance) {
  285. minDistance = distanceToSegment.distance;
  286. closestSegment = onscreenSegments[s];//.seg = s;
  287. //closestSegment.point = new OL.Geometry.Point(distanceToSegment.x1, distanceToSegment.y1);
  288. }
  289. }
  290. return closestSegment;
  291. };
  292. };
  293.  
  294. function Model(){
  295.  
  296. this.getPrimaryStreetID = function(segmentID){
  297. return W.model.segments.get(segmentID).attributes.primaryStreetID;
  298. };
  299.  
  300. this.getStreetName = function(primaryStreetID){
  301. return W.model.streets.get(PrimaryStreetID).name;
  302. };
  303.  
  304. this.getCityID = function(primaryStreetID){
  305. return W.model.streets.get(primaryStreetID).cityID;
  306. };
  307.  
  308. this.getCityName = function(primaryStreetID){
  309. return W.model.cities.get(this.getCityID(primaryStreetID)).attributes.Name;
  310. };
  311.  
  312. this.getStateName = function(primaryStreetID){
  313. return W.model.states.get(getStateID(primaryStreetID)).Name;
  314. };
  315.  
  316. this.getStateID = function(primaryStreetID){
  317. return W.model.cities.get(primaryStreetID).attributes.stateID;
  318. };
  319.  
  320. this.getCountryID = function(primaryStreetID){
  321. return W.model.cities.get(this.getCityID(primaryStreetID)).attributes.CountryID;
  322. };
  323.  
  324. this.getCountryName = function(primaryStreetID){
  325. return W.model.countries.get(getCountryID(primaryStreetID)).name;
  326. };
  327.  
  328. this.getCityNameFromSegmentObj = function(segObj){
  329. return this.getCityName(segObj.attributes.primaryStreetID);
  330. };
  331.  
  332. this.getStateNameFromSegmentObj = function(segObj){
  333. return this.getStateName(segObj.attributes.primaryStreetID);
  334. };
  335.  
  336. //returns an array of segmentIDs for all segments that are part of the same roundabout as the passed segment
  337. this.getAllRoundaboutSegmentsFromObj = function(segObj){
  338. if(segObj.model.attributes.junctionID === null)
  339. return null;
  340.  
  341. return W.model.junctions.objects[segObj.model.attributes.junctionID].segIDs;
  342. };
  343.  
  344. this.getAllRoundaboutJunctionNodesFromObj = function(segObj){
  345. var RASegs = this.getAllRoundaboutSegmentsFromObj(segObj);
  346. var RAJunctionNodes = [];
  347. for(i=0; i< RASegs.length; i++){
  348. RAJunctionNodes.push(W.model.nodes.objects[W.model.segments.get(RASegs[i]).attributes.toNodeID]);
  349.  
  350. }
  351. return RAJunctionNodes;
  352. };
  353.  
  354. this.isRoundaboutSegmentID = function(segmentID){
  355. if(W.model.segments.get(segmentID).attributes.junctionID === null)
  356. return false;
  357. else
  358. return true;
  359. };
  360.  
  361. this.isRoundaboutSegmentObj = function(segObj){
  362. if(segObj.model.attributes.junctionID === null)
  363. return false;
  364. else
  365. return true;
  366. };
  367.  
  368.  
  369. this.getOnscreenSegments = function(){
  370. var segments = W.model.segments.objects;
  371. var mapExtent = W.map.getExtent();
  372. var onScreenSegments = [];
  373. var seg;
  374.  
  375. for (s in segments) {
  376. if (!segments.hasOwnProperty(s))
  377. continue;
  378.  
  379. seg = W.model.segments.get(s);
  380. if (mapExtent.intersectsBounds(seg.geometry.getBounds()))
  381. onScreenSegments.push(seg);
  382. }
  383. return onScreenSegments;
  384. };
  385.  
  386. /**
  387. * Defers execution of a callback function until the WME map and data
  388. * model are ready. Call this function before calling a function that
  389. * causes a map and model reload, such as W.map.moveTo(). After the
  390. * move is completed the callback function will be executed.
  391. * @function WazeWrap.Model.onModelReady
  392. * @param {Function} callback The callback function to be executed.
  393. * @param {Boolean} now Whether or not to call the callback now if the
  394. * model is currently ready.
  395. * @param {Object} context The context in which to call the callback.
  396. */
  397. this.onModelReady = function (callback, now, context) {
  398. var deferModelReady = function () {
  399. return $.Deferred(function (dfd) {
  400. var resolve = function () {
  401. dfd.resolve();
  402. W.model.events.unregister('mergeend', null, resolve);
  403. };
  404. W.model.events.register('mergeend', null, resolve);
  405. }).promise();
  406. };
  407. var deferMapReady = function () {
  408. return $.Deferred(function (dfd) {
  409. var resolve = function () {
  410. dfd.resolve();
  411. W.vent.off('operationDone', resolve);
  412. };
  413. W.vent.on('operationDone', resolve);
  414. }).promise();
  415. };
  416.  
  417. if (typeof callback === 'function') {
  418. context = context || callback;
  419. if (now && WazeWrapBeta.Util.mapReady() && WazeWrapBeta.Util.modelReady()) {
  420. callback.call(context);
  421. } else {
  422. $.when(deferMapReady() && deferModelReady()).
  423. then(function () {
  424. callback.call(context);
  425. });
  426. }
  427. }
  428. };
  429. };
  430. function User(){
  431. this.Rank = function(){
  432. return W.loginManager.user.normalizedLevel;
  433. };
  434.  
  435. this.Username = function(){
  436. return W.loginManager.user.userName;
  437. };
  438.  
  439. this.isCM = function(){
  440. if(W.loginManager.user.editableCountryIDs.length > 0)
  441. return true;
  442. else
  443. return false;
  444. };
  445. this.isAM = function(){
  446. return W.loginManager.user.isAreaManager;
  447. };
  448. };
  449. function Util(){
  450. /**
  451. * Function to defer function execution until an element is present on
  452. * the page.
  453. * @function WazeWrap.Util.waitForElement
  454. * @param {String} selector The CSS selector string or a jQuery object
  455. * to find before executing the callback.
  456. * @param {Function} callback The function to call when the page
  457. * element is detected.
  458. * @param {Object} [context] The context in which to call the callback.
  459. */
  460. this.waitForElement = function (selector, callback, context) {
  461. var jqObj;
  462.  
  463. if (!selector || typeof callback !== 'function') {
  464. return;
  465. }
  466.  
  467. jqObj = typeof selector === 'string' ?
  468. $(selector) : selector instanceof $ ? selector : null;
  469.  
  470. if (!jqObj.size()) {
  471. window.requestAnimationFrame(function () {
  472. WazeWrapBeta.Util.waitForElement(selector, callback, context);
  473. });
  474. } else {
  475. callback.call(context || callback);
  476. }
  477. };
  478.  
  479. /**
  480. * Function to track the ready state of the map.
  481. * @function WazeWrap.Util.mapReady
  482. * @return {Boolean} Whether or not a map operation is pending or
  483. * undefined if the function has not yet seen a map ready event fired.
  484. */
  485. this.mapReady = function () {
  486. var mapReady = true;
  487. W.vent.on('operationPending', function () {
  488. mapReady = false;
  489. });
  490. W.vent.on('operationDone', function () {
  491. mapReady = true;
  492. });
  493. return function () {
  494. return mapReady;
  495. };
  496. } ();
  497.  
  498. /**
  499. * Function to track the ready state of the model.
  500. * @function WazeWrap.Util.modelReady
  501. * @return {Boolean} Whether or not the model has loaded objects or
  502. * undefined if the function has not yet seen a model ready event fired.
  503. */
  504. this.modelReady = function () {
  505. var modelReady = true;
  506. W.model.events.register('mergestart', null, function () {
  507. modelReady = false;
  508. });
  509. W.model.events.register('mergeend', null, function () {
  510. modelReady = true;
  511. });
  512. return function () {
  513. return modelReady;
  514. };
  515. } ();
  516. };
  517.  
  518. function Interface() {
  519. /**
  520. * Generates id for message bars.
  521. * @private
  522. */
  523. var getNextID = function () {
  524. var id = 1;
  525. return function () {
  526. return id++;
  527. };
  528. } ();
  529. this.Shortcut = OL.Class(this, /** @lends WazeWrap.Interface.Shortcut.prototype */ {
  530. name: null,
  531. group: null,
  532. shortcut: {},
  533. callback: null,
  534. scope: null,
  535. groupExists: false,
  536. actionExists: false,
  537. eventExists: false,
  538. defaults: {
  539. group: 'default'
  540. },
  541. /**
  542. * Creates a new {WazeWrap.Interface.Shortcut}.
  543. * @class
  544. * @name WazeWrap.Interface.Shortcut
  545. * @param name {String} The name of the shortcut.
  546. * @param group {String} The name of the shortcut group.
  547. * @param shortcut {String} The shortcut key(s). The shortcut
  548. * should be of the form 'i' where i is the keyboard shortuct or
  549. * include modifier keys such as 'CSA+i', where C = the control
  550. * key, S = the shift key, A = the alt key, and i = the desired
  551. * keyboard shortcut. The modifier keys are optional.
  552. * @param callback {Function} The function to be called by the
  553. * shortcut.
  554. * @param scope {Object} The object to be used as this by the
  555. * callback.
  556. * @return {WazeWrap.Interface.Shortcut} The new shortcut object.
  557. * @example //Creates new shortcut and adds it to the map.
  558. * shortcut = new WazeWrap.Interface.Shortcut('myName', 'myGroup', 'C+p', callbackFunc, null).add();
  559. */
  560. initialize: function (name, group, shortcut, callback, scope) {
  561. if ('string' === typeof name && name.length > 0 &&
  562. 'string' === typeof shortcut && shortcut.length > 0 &&
  563. 'function' === typeof callback) {
  564. this.name = name;
  565. this.group = group || this.defaults.group;
  566. this.callback = callback;
  567. this.shortcut[shortcut] = name;
  568. if ('object' !== typeof scope) {
  569. this.scope = null;
  570. } else {
  571. this.scope = scope;
  572. }
  573. return this;
  574. }
  575. },
  576. /**
  577. * Determines if the shortcut's group already exists.
  578. * @private
  579. */
  580. doesGroupExist: function () {
  581. this.groupExists = 'undefined' !== typeof W.accelerators.Groups[this.group] &&
  582. undefined !== typeof W.accelerators.Groups[this.group].members &&
  583. W.accelerators.Groups[this.group].length > 0;
  584. return this.groupExists;
  585. },
  586. /**
  587. * Determines if the shortcut's action already exists.
  588. * @private
  589. */
  590. doesActionExist: function () {
  591. this.actionExists = 'undefined' !== typeof W.accelerators.Actions[this.name];
  592. return this.actionExists;
  593. },
  594. /**
  595. * Determines if the shortcut's event already exists.
  596. * @private
  597. */
  598. doesEventExist: function () {
  599. this.eventExists = 'undefined' !== typeof W.accelerators.events.listeners[this.name] &&
  600. W.accelerators.events.listeners[this.name].length > 0 &&
  601. this.callback === W.accelerators.events.listeners[this.name][0].func &&
  602. this.scope === W.accelerators.events.listeners[this.name][0].obj;
  603. return this.eventExists;
  604. },
  605. /**
  606. * Creates the shortcut's group.
  607. * @private
  608. */
  609. createGroup: function () {
  610. W.accelerators.Groups[this.group] = [];
  611. W.accelerators.Groups[this.group].members = [];
  612. },
  613. /**
  614. * Registers the shortcut's action.
  615. * @private
  616. */
  617. addAction: function () {
  618. W.accelerators.addAction(this.name, { group: this.group });
  619. },
  620. /**
  621. * Registers the shortcut's event.
  622. * @private
  623. */
  624. addEvent: function () {
  625. W.accelerators.events.register(this.name, this.scope, this.callback);
  626. },
  627. /**
  628. * Registers the shortcut's keyboard shortcut.
  629. * @private
  630. */
  631. registerShortcut: function () {
  632. W.accelerators._registerShortcuts(this.shortcut);
  633. },
  634. /**
  635. * Adds the keyboard shortcut to the map.
  636. * @return {WazeWrap.Interface.Shortcut} The keyboard shortcut.
  637. */
  638. add: function () {
  639. /* If the group is not already defined, initialize the group. */
  640. if (!this.doesGroupExist()) {
  641. this.createGroup();
  642. }
  643.  
  644. /* Clear existing actions with same name */
  645. if (this.doesActionExist()) {
  646. W.accelerators.Actions[this.name] = null;
  647. }
  648. this.addAction();
  649.  
  650. /* Register event only if it's not already registered */
  651. if (!this.doesEventExist()) {
  652. this.addEvent();
  653. }
  654.  
  655. /* Finally, register the shortcut. */
  656. this.registerShortcut();
  657. return this;
  658. },
  659. /**
  660. * Removes the keyboard shortcut from the map.
  661. * @return {WazeWrap.Interface.Shortcut} The keyboard shortcut.
  662. */
  663. remove: function () {
  664. if (this.doesEventExist()) {
  665. W.accelerators.events.unregister(this.name, this.scope, this.callback);
  666. }
  667. if (this.doesActionExist()) {
  668. delete W.accelerators.Actions[this.name];
  669. }
  670. //remove shortcut?
  671. return this;
  672. },
  673. /**
  674. * Changes the keyboard shortcut and applies changes to the map.
  675. * @return {WazeWrap.Interface.Shortcut} The keyboard shortcut.
  676. */
  677. change: function (shortcut) {
  678. if (shortcut) {
  679. this.shortcut = {};
  680. this.shortcut[shortcut] = this.name;
  681. this.registerShortcut();
  682. }
  683. return this;
  684. }
  685. }),
  686.  
  687. this.Tab = OL.Class(this, {
  688. /** @lends WazeWrap.Interface.Tab */
  689. TAB_SELECTOR: '#user-tabs ul.nav-tabs',
  690. CONTENT_SELECTOR: '#user-info div.tab-content',
  691. callback: null,
  692. $content: null,
  693. context: null,
  694. $tab: null,
  695. /**
  696. * Creates a new WazeWrap.Interface.Tab. The tab is appended to the WME
  697. * editor sidebar and contains the passed HTML content.
  698. * @class
  699. * @name WazeWrap.Interface.Tab
  700. * @param {String} name The name of the tab. Should not contain any
  701. * special characters.
  702. * @param {String} content The HTML content of the tab.
  703. * @param {Function} [callback] A function to call upon successfully
  704. * appending the tab.
  705. * @param {Object} [context] The context in which to call the callback
  706. * function.
  707. * @return {WazeWrap.Interface.Tab} The new tab object.
  708. * @example //Creates new tab and adds it to the page.
  709. * new WazeWrap.Interface.Tab('thebestscriptever', '<div>Hello World!</div>');
  710. */
  711. initialize: function (name, content, callback, context) {
  712. var idName, i = 0;
  713. if (name && 'string' === typeof name &&
  714. content && 'string' === typeof content) {
  715. if (callback && 'function' === typeof callback) {
  716. this.callback = callback;
  717. this.context = context || callback;
  718. }
  719. /* Sanitize name for html id attribute */
  720. idName = name.toLowerCase().replace(/[^a-z-_]/g, '');
  721. /* Make sure id will be unique on page */
  722. while (
  723. $('#sidepanel-' + (i ? idName + i : idName)).length > 0) {
  724. i++;
  725. }
  726. if (i) {
  727. idName = idName + i;
  728. }
  729. /* Create tab and content */
  730. this.$tab = $('<li/>')
  731. .append($('<a/>')
  732. .attr({
  733. 'href': '#sidepanel-' + idName,
  734. 'data-toggle': 'tab',
  735. })
  736. .text(name));
  737. this.$content = $('<div/>')
  738. .addClass('tab-pane')
  739. .attr('id', 'sidepanel-' + idName)
  740. .html(content);
  741.  
  742. this.appendTab();
  743. }
  744. },
  745.  
  746. append: function (content) {
  747. this.$content.append(content);
  748. },
  749.  
  750. appendTab: function () {
  751. WazeWrapBeta.Util.waitForElement(
  752. this.TAB_SELECTOR + ',' + this.CONTENT_SELECTOR,
  753. function () {
  754. $(this.TAB_SELECTOR).append(this.$tab);
  755. $(this.CONTENT_SELECTOR).first().append(this.$content);
  756. if (this.callback) {
  757. this.callback.call(this.context);
  758. }
  759. }, this);
  760. },
  761.  
  762. clearContent: function () {
  763. this.$content.empty();
  764. },
  765.  
  766. destroy: function () {
  767. this.$tab.remove();
  768. this.$content.remove();
  769. }
  770. });
  771. };
  772.  
  773. }.call(this));

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址