resrcit/resrc.js — pull request #3 by peyerluk (2014-11-17 09:31:24+00:00)
Title: Always update image source when calling resrc.run()

This pull-request fixes a problem where calling `resrc.run(elem)` would not reset the src attribute of the elem even though the `data-src` attribute of that element was changed.

This is because of an internal logic that will abort setting the source under the following conditions:
```javascript
if (options.resrcOnResizeDown === false) {
// Return if the last width of the element is >= to the current width.
if (elem.lastWidth >= currentElemWidth) {
return;
}
}
```
code: https://github.com/resrcit/resrc.js/blob/31a398ad083a8dbf68460f35e9a05ecae06f7274/src/resrc.js#L731

In our use case of a WYSIWYG editor where users can change image urls or image crops and we reset the `data-src` attribute to update the image this behaviour makes using the resrc.it script as it is unusable unless we set `resrcOnResizeDown` to true. Which is counter-intuitive and very error prone to changes.

So we refactored the `run` command and split up the logic to conditionally update an element into its own method which is currently only called after pinching an element. Calls to `resrc.run` will now always update the src if the `data-src` attribute was changed.

Additionally we added a convenience method `setImageUrl()` which allows to update source of an image with a new url.

Here's a quick example:
```javascript
// new
resrc.setImageUrl(elem, 'http://image.com/1.jpg');

// currently
elem.setAttribute('data-src', 'http://image.com/1.jpg');
resrc.run(elem);
```

Finally we added a few tests and had to include karma for it. To run the tests execute `grunt karma`.