-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add ol.control.Control#setTarget #3139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
||
/** | ||
* This function is used to set a target element for the control. This | ||
* function will have no effect if it is called after the control has |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a limitation which is not necessary. With a little more code, you could use setTarget to move the control's markup around in the document. Or am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually gets much more complex if you go that path so I decided to go the simple way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
ol.control.Control.prototype.setTarget = function(target) {
var map = this.map_;
this.setMap(null);
this.target_ = target;
this.setMap(map);
};
If that does not work, you could at least add an assertion to ensure that the map is not set yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That may work but it's also three setMap calls if you add the control to the map and then calls setTarget. This will also add the control element to the map overlay container to finally remove it to add it to the final target. Not so nice I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may also be important to maintain the control's position in the collection (meaning it might be unexpected to have it jump to the end of the collection when you call setTarget
).
I imagine most people will expect setTarget
to work when called more than once. But I wouldn't be opposed to adding the method as is and then making it more useful later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Looks like a major effort indeed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may also be important to maintain the control's position in the collection (meaning it might be unexpected to have it jump to the end of the collection when you call setTarget).
setMap
is called when a control is added to or removed from a map's controls collection, but calling setMap
directly does not change the collection. So that part is not an issue.
This commit adds a setTarget method to ol.control.Control. This function can be used in cases where the target cannot be set at control construction time. For example, with Angular, it makes sense to create the control instance in an Angular controller, and have a "control" directive insert the control into the DOM.
Thanks for the reviews. I changed the docs as suggested by @tschaub. I'll merge this if there are no other comments. |
@ahocevar do you agree with merging this? |
Sure, please merge! |
Thanks. I just wanted to make sure. |
Add ol.control.Control#setTarget
This commit adds a
setTarget
method tool.control.Control
. This function can be used in cases where the target cannot be set at control construction time.For example, with Angular, it makes sense to create the control instance in an Angular controller, and have a "control" directive insert the control into the DOM.