TableAjax.php.GeomField.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // @require jQuery
  2. // @require variables:
  3. // if ('undefined' === typeof UNIQ_HASH) throw "Missing UNIQ_HASH"; // $this->_htmlID
  4. (function($) {
  5. var TableAjaxGeomField = function() {
  6. var priv = {}; //private api
  7. var publ = {}; //public api
  8. priv.options = {};
  9. var defaults = {
  10. recordID: '',
  11. fieldValue: '',
  12. hasValueClassName: 'TableAjaxGeomField-hasValue',
  13. linkClassNamePrefix: 'TableAjaxGeomField',
  14. linkSelectClassName: 'select',
  15. linkSelectAddClassNames: '', // 'glyphicon glyphicon-map-marker'
  16. linkRemoveClassName: 'remove',
  17. linkRemoveAddClassNames: '', // 'glyphicon glyphicon-remove'
  18. removeUrl: '',
  19. onSelect: null,
  20. onRemove: null,
  21. debug: false
  22. };
  23. priv.init = function() {
  24. priv.render();
  25. };
  26. priv.render = function() {
  27. var state = priv.options;
  28. var cellMapSelect = jQuery('<i title="Pokaż/utwórz obiekt na mapie"></i>');
  29. cellMapSelect.addClass(state.linkClassNamePrefix + '-' + state.linkSelectClassName);
  30. if (state.linkSelectAddClassNames) {
  31. cellMapSelect.addClass(state.linkSelectAddClassNames);
  32. }
  33. cellMapSelect.on('click', {recordID: priv.options.recordID, fieldValue: priv.options.fieldValue}, function(e) {
  34. if (typeof state.onSelect == "function") {
  35. state.onSelect.call(this, e.data.recordID, e.data.fieldValue);
  36. }
  37. });
  38. var cellMapRemove = jQuery('<i title="Usuń obiekt z mapy"></i>');
  39. cellMapRemove.addClass(state.linkClassNamePrefix + '-' + state.linkRemoveClassName);
  40. if (state.linkRemoveAddClassNames) {
  41. cellMapRemove.addClass(state.linkRemoveAddClassNames);
  42. }
  43. cellMapRemove.on('click', {recordID: state.recordID, fieldValue: state.fieldValue}, function(e) {
  44. if (typeof state.onRemove == "function") {
  45. state.onRemove.call(this, publ, e.data.recordID, e.data.fieldValue);
  46. }
  47. });
  48. var node = $(state.id);
  49. node.empty();
  50. node.addClass(state.linkClassNamePrefix);
  51. node.removeClass(state.hasValueClassName);
  52. if (state.fieldValue) {
  53. node.addClass(state.hasValueClassName);
  54. }
  55. node.append(cellMapSelect);
  56. node.append(cellMapRemove);
  57. };
  58. publ.setValue = function(value) {
  59. if (priv.options.debug) console.log('TEST setValue: ', value, 'this', this);
  60. priv.options.fieldValue = value;
  61. priv.render();
  62. };
  63. publ.init = function(options) {
  64. if (priv.options.debug) console.log('TableAjaxGeomField initialization...', options);
  65. //merge supplied options with defaults
  66. $.extend(priv.options, defaults, options);
  67. priv.init();
  68. return publ;
  69. };
  70. return publ;
  71. };
  72. $.fn.TableAjaxGeomFieldSetValue = function(value) {
  73. return this.each(function() {
  74. var tblAjaxMapGeomFld = $(this).data('TableAjaxGeomField');
  75. if (tblAjaxMapGeomFld) {
  76. tblAjaxMapGeomFld.setValue(value);
  77. }
  78. });
  79. return this;
  80. };
  81. $.fn.TableAjaxGeomField = function(options) {
  82. options = options || {};
  83. return this.each(function() {
  84. options.id = this;
  85. if (!$(this).data('TableAjaxGeomField')) {
  86. $(this).data('TableAjaxGeomField', new TableAjaxGeomField().init(options));
  87. }
  88. });
  89. return this;
  90. };
  91. }(jQuery));
  92. // global['...'] = ...