Skip to content

Commit

Permalink
disable auto decovery iframe
Browse files Browse the repository at this point in the history
and change the api

ichord/At.js#160
  • Loading branch information
ichord committed Jul 12, 2014
1 parent 35b03e3 commit 37d4c5e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 60 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ $('#inputor').caret('pos'); // => 15
$('#inputor').caret('pos', 15);

// set iframe context
// oftenly you don't need to set iframe context because caret.js will find the iframe object automatically
// but some iframe editor will prevent caret.js to finding for security reasons,
// so you may have to set the iframe manually
$('#inputor').caret({iframe: theIframe});
$('#inputor').caret('offset');
$('#inputor').caret('pos', 15);
<!-- $('#inputor').caret({iframe: theIframe}); -->
$('#inputor').caret('offset', {iframe: theIframe});
$('#inputor').caret('pos', 15, {iframe: theIframe});

```
4 changes: 2 additions & 2 deletions dist/jquery.caret.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@
ifrBody.contentEditable = true;
ifrBody.id = 'ifrBody';
ifrBody.innerHTML = 'For <strong>WYSIWYG</strong> such as <strong>ckeditor</strong>'
$(ifrBody).caret({iframe: ifr});
$(ifrBody).on('keyup mouseup', function() {
resizeBox($(ifrBody).caret('offset'));
resizeBox($(ifrBody).caret('offset', {iframe: ifr}));
});


Expand Down
47 changes: 24 additions & 23 deletions src/jquery.caret.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -274,32 +274,33 @@
oDocument = null
oWindow = null
oFrame = null
setContextBy = (iframe) ->
oFrame = iframe
oWindow = iframe.contentWindow
oDocument = iframe.contentDocument || oWindow.document
configure = ($dom, settings) ->
if $.isPlainObject(settings) and iframe = settings.iframe
$dom.data('caret-iframe', iframe)
setContextBy iframe
else if iframe = $dom.data('caret-iframe')
setContextBy iframe
setContextBy = (settings) ->
if iframe = settings?.iframe
oFrame = iframe
oWindow = iframe.contentWindow
oDocument = iframe.contentDocument || oWindow.document
else
oDocument = $dom[0].ownerDocument
oWindow = oDocument.defaultView || oDocument.parentWindow
try
oFrame = oWindow.frameElement
catch error
# throws error in cross-domain iframes
$.fn.caret = (method) ->
oFrame = undefined
oWindow = window
oDocument = document
discoveryIframeOf = ($dom) ->
oDocument = $dom[0].ownerDocument
oWindow = oDocument.defaultView || oDocument.parentWindow
try
oFrame = oWindow.frameElement
catch error
# throws error in cross-domain iframes

$.fn.caret = (method, value, settings) ->
# http://stackoverflow.com/questions/16010204/get-reference-of-window-object-from-a-dom-element
if typeof method is 'object'
configure this, method
this
else if methods[method]
configure this
if methods[method]
if $.isPlainObject(value)
setContextBy value
value = undefined
else
setContextBy settings
caret = if Utils.contentEditable(this) then new EditableCaret(this) else new InputCaret(this)
methods[method].apply caret, Array::slice.call(arguments, 1)
methods[method].apply caret, [value]
else
$.error "Method #{method} does not exist on jQuery.caret"

Expand Down
56 changes: 29 additions & 27 deletions src/jquery.caret.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}
})(function($) {
"use strict";
var EditableCaret, InputCaret, Mirror, Utils, configure, methods, oDocument, oFrame, oWindow, pluginName, setContextBy;
var EditableCaret, InputCaret, Mirror, Utils, discoveryIframeOf, methods, oDocument, oFrame, oWindow, pluginName, setContextBy;
pluginName = 'caret';
EditableCaret = (function() {
function EditableCaret($inputor) {
Expand Down Expand Up @@ -319,37 +319,39 @@
oDocument = null;
oWindow = null;
oFrame = null;
setContextBy = function(iframe) {
oFrame = iframe;
oWindow = iframe.contentWindow;
return oDocument = iframe.contentDocument || oWindow.document;
};
configure = function($dom, settings) {
var error, iframe;
if ($.isPlainObject(settings) && (iframe = settings.iframe)) {
$dom.data('caret-iframe', iframe);
return setContextBy(iframe);
} else if (iframe = $dom.data('caret-iframe')) {
return setContextBy(iframe);
setContextBy = function(settings) {
var iframe;
if (iframe = settings != null ? settings.iframe : void 0) {
oFrame = iframe;
oWindow = iframe.contentWindow;
return oDocument = iframe.contentDocument || oWindow.document;
} else {
oDocument = $dom[0].ownerDocument;
oWindow = oDocument.defaultView || oDocument.parentWindow;
try {
return oFrame = oWindow.frameElement;
} catch (_error) {
error = _error;
}
oFrame = void 0;
oWindow = window;
return oDocument = document;
}
};
discoveryIframeOf = function($dom) {
var error;
oDocument = $dom[0].ownerDocument;
oWindow = oDocument.defaultView || oDocument.parentWindow;
try {
return oFrame = oWindow.frameElement;
} catch (_error) {
error = _error;
}
};
$.fn.caret = function(method) {
$.fn.caret = function(method, value, settings) {
var caret;
if (typeof method === 'object') {
configure(this, method);
return this;
} else if (methods[method]) {
configure(this);
if (methods[method]) {
if ($.isPlainObject(value)) {
setContextBy(value);
value = void 0;
} else {
setContextBy(settings);
}
caret = Utils.contentEditable(this) ? new EditableCaret(this) : new InputCaret(this);
return methods[method].apply(caret, Array.prototype.slice.call(arguments, 1));
return methods[method].apply(caret, [value]);
} else {
return $.error("Method " + method + " does not exist on jQuery.caret");
}
Expand Down

0 comments on commit 37d4c5e

Please sign in to comment.