744dcb38-0c27-4da6-b361-1b4c70aba14a.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. cc._RF.push(module, '744dcs4DCdNprNhG0xwq6FK', 'LocalizedLabel');
  3. // LocalizedLabel.js
  4. "use strict";
  5. var i18n = require('LanguageData'); // Returns a function, that, as long as it continues to be invoked, will not
  6. // be triggered. The function will be called after it stops being called for
  7. // N milliseconds. If `immediate` is passed, trigger the function on the
  8. // leading edge, instead of the trailing.
  9. function debounce(func, wait, immediate) {
  10. var timeout;
  11. return function () {
  12. var context = this,
  13. args = arguments;
  14. var later = function later() {
  15. timeout = null;
  16. if (!immediate) func.apply(context, args);
  17. };
  18. var callNow = immediate && !timeout;
  19. clearTimeout(timeout);
  20. timeout = setTimeout(later, wait);
  21. if (callNow) func.apply(context, args);
  22. };
  23. }
  24. cc.Class({
  25. "extends": cc.Component,
  26. editor: {
  27. executeInEditMode: true,
  28. menu: 'i18n/LocalizedLabel'
  29. },
  30. properties: {
  31. dataID: {
  32. get: function get() {
  33. return this._dataID;
  34. },
  35. set: function set(val) {
  36. if (this._dataID !== val) {
  37. this._dataID = val;
  38. if (CC_EDITOR) {
  39. this._debouncedUpdateLabel();
  40. } else {
  41. this.updateLabel();
  42. }
  43. }
  44. }
  45. },
  46. _dataID: ''
  47. },
  48. onLoad: function onLoad() {
  49. if (CC_EDITOR) {
  50. this._debouncedUpdateLabel = debounce(this.updateLabel, 200);
  51. }
  52. if (!i18n.inst) {
  53. i18n.init();
  54. } // cc.log('dataID: ' + this.dataID + ' value: ' + i18n.t(this.dataID));
  55. this.fetchRender();
  56. },
  57. fetchRender: function fetchRender() {
  58. var label = this.getComponent(cc.Label);
  59. if (label) {
  60. this.label = label;
  61. this.updateLabel();
  62. return;
  63. }
  64. },
  65. updateLabel: function updateLabel() {
  66. if (!this.label) {
  67. cc.error('Failed to update localized label, label component is invalid!');
  68. return;
  69. }
  70. var localizedString = i18n.t(this.dataID);
  71. if (localizedString) {
  72. this.label.string = i18n.t(this.dataID);
  73. }
  74. }
  75. });
  76. cc._RF.pop();