TableAjax.php.GeomField.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. getCsvUrl: '',
  17. linkGetCsvAddClassNames: '', // 'glyphicon glyphicon-download'
  18. linkRemoveClassName: 'remove',
  19. linkRemoveAddClassNames: '', // 'glyphicon glyphicon-remove'
  20. onSelect: null,
  21. onRemove: null,
  22. debug: false
  23. };
  24. priv.init = function() {
  25. priv.render();
  26. };
  27. priv.render = function() {
  28. var state = priv.options;
  29. var cellMapSelect = jQuery('<i title="Pokaż/utwórz obiekt na mapie"></i>');
  30. cellMapSelect.addClass(state.linkClassNamePrefix + '-' + state.linkSelectClassName);
  31. if (state.linkSelectAddClassNames) {
  32. cellMapSelect.addClass(state.linkSelectAddClassNames);
  33. }
  34. cellMapSelect.on('click', {recordID: priv.options.recordID, fieldValue: priv.options.fieldValue}, function(e) {
  35. if (typeof state.onSelect == "function") {
  36. state.onSelect.call(this, e.data.recordID, e.data.fieldValue);
  37. }
  38. });
  39. var cellMapRemove = jQuery('<i title="Usuń obiekt z mapy"></i>');
  40. cellMapRemove.addClass(state.linkClassNamePrefix + '-' + state.linkRemoveClassName);
  41. if (state.linkRemoveAddClassNames) {
  42. cellMapRemove.addClass(state.linkRemoveAddClassNames);
  43. }
  44. cellMapRemove.on('click', {recordID: state.recordID, fieldValue: state.fieldValue}, function(e) {
  45. if (typeof state.onRemove == "function") {
  46. state.onRemove.call(this, publ, e.data.recordID, e.data.fieldValue);
  47. }
  48. });
  49. var cellMapGetCsv = jQuery('<a href="'+priv.options.getCsvUrl+'&id='+priv.options.recordID+'"><i title="Pobierz punkty referencyjne w CSV" class="'+priv.options.linkGetCsvAddClassNames+'"></i></a>');
  50. var node = $(state.id);
  51. node.empty();
  52. node.addClass(state.linkClassNamePrefix);
  53. node.removeClass(state.hasValueClassName);
  54. if (state.fieldValue) {
  55. node.addClass(state.hasValueClassName);
  56. }
  57. node.append(cellMapSelect);
  58. if (state.fieldValue) node.append(cellMapGetCsv);
  59. node.append(cellMapRemove);
  60. };
  61. publ.setValue = function(value) {
  62. if (priv.options.debug) console.log('TEST setValue: ', value, 'this', this);
  63. priv.options.fieldValue = value;
  64. priv.render();
  65. };
  66. publ.init = function(options) {
  67. if (priv.options.debug) console.log('TableAjaxGeomField initialization...', options);
  68. //merge supplied options with defaults
  69. $.extend(priv.options, defaults, options);
  70. priv.init();
  71. return publ;
  72. };
  73. return publ;
  74. };
  75. $.fn.TableAjaxGeomFieldSetValue = function(value) {
  76. return this.each(function() {
  77. var tblAjaxMapGeomFld = $(this).data('TableAjaxGeomField');
  78. if (tblAjaxMapGeomFld) {
  79. tblAjaxMapGeomFld.setValue(value);
  80. }
  81. });
  82. return this;
  83. };
  84. $.fn.TableAjaxGeomField = function(options) {
  85. options = options || {};
  86. return this.each(function() {
  87. options.id = this;
  88. if (!$(this).data('TableAjaxGeomField')) {
  89. $(this).data('TableAjaxGeomField', new TableAjaxGeomField().init(options));
  90. }
  91. });
  92. return this;
  93. };
  94. }(jQuery));
  95. // global['...'] = ...