/dev/_lib/tiny_mce/plugins/resourcelink/js/resourcelink.js

https://github.com/xiannenglin/3akai-ux · JavaScript · 117 lines · 85 code · 21 blank · 11 comment · 25 complexity · 264bb082c3bc6bb89189bad01e66cbb7 MD5 · raw file

  1. tinyMCEPopup.requireLangPack();
  2. var ResourceLinkDialog = {
  3. init : function() {
  4. // Get the selected contents as text and place it in the input
  5. $("#linktext").val(tinyMCEPopup.editor.selection.getContent({format : 'text'}));
  6. //f.somearg.value = tinyMCEPopup.getWindowArg('some_custom_arg');
  7. if ($("#linktext").val().trim() !== "") {
  8. $("#insert").removeAttr('disabled');
  9. }
  10. // if a link is selected in the editor, prepopulate the dialog
  11. var curSelectionHTML = tinyMCEPopup.editor.selection.getContent();
  12. if ($(curSelectionHTML).is('a') || $(curSelectionHTML).find('a').length > 0) { // because you could select just the a or its container
  13. var curSelectionHref;
  14. if ($(curSelectionHTML).is('a'))
  15. curSelectionHref = $(curSelectionHTML).attr('href');
  16. else
  17. curSelectionHref = $(curSelectionHTML).find('a').attr('href');
  18. // check to see if its a resource link or if its a regular link
  19. if (curSelectionHref.indexOf('xythos') != -1) {
  20. $("#resource_filename").html(curSelectionHref);
  21. $("#resource_url").val(curSelectionHref);
  22. } else {
  23. $("#link_url").val(curSelectionHref);
  24. $("#link_to_web").attr("checked", "checked");
  25. $("#link_url").removeAttr("disabled");
  26. }
  27. }
  28. // enable the link to be inserted only if there is text to link to
  29. $("#linktext").live('change keyup click', function() {
  30. if ($("#linktext").val().trim() !== "") {
  31. $("#insert").removeAttr('disabled');
  32. } else {
  33. $("#insert").attr('disabled','disabled');
  34. }
  35. });
  36. // allow clicking on the disabled-by-default url field to enable it
  37. $("#link_url").live('change click keyup', function() { // yeah this doesn't work, event never fires on a disabled input, looking into it
  38. if ($("#linktext").val().trim() !== "") {
  39. $("#insert").removeAttr('disabled');
  40. } else {
  41. $("#insert").attr('disabled','disabled');
  42. }
  43. });
  44. },
  45. setResourceLink : function(filename, url) {
  46. var link_text = $("#linktext").val().trim();
  47. $("#resource_filename").html(url);
  48. $("#resource_url").val(url);
  49. if (link_text == "") {
  50. $("#linktext").val(filename);
  51. }
  52. $("#insert").removeAttr('disabled');
  53. },
  54. browse : function() {
  55. $("#link_to_res").attr('checked', 'checked');
  56. $("#link_url").val('url').attr('disabled', 'disabled');
  57. tinyMCEPopup.editor.execCommand('mceResourceLinkBrowse');
  58. },
  59. insert : function() {
  60. // Insert the contents from the input into the document
  61. var to_insert = '';
  62. var link_text = $("#linktext").val().trim();
  63. // first, check to see that the link text to be inserted isn't blank
  64. if (link_text == '') {
  65. return; // TODO display error message in-line for missing text
  66. }
  67. // now make sure that if we're lnking to a web resource,
  68. // that the url provided is a real url
  69. if ($("input[type=radio]:checked").attr('id') == 'link_to_web') {
  70. var regexp = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i // from jquery validate plugin
  71. var url = $("#link_url").val().trim();
  72. if (regexp.test(url)) {
  73. // is a url
  74. to_insert = "<a href='" + url + "'>" + link_text + "</a>";
  75. } else {
  76. alert('Please choose a valid link url');
  77. return;
  78. }
  79. } else {
  80. var url = $("#resource_url").val().trim();
  81. if (url != "") {
  82. to_insert = "<a href='" + url + "'>" + link_text + "</a>";
  83. }
  84. }
  85. tinyMCEPopup.editor.execCommand('mceInsertContent', false, to_insert);
  86. tinyMCEPopup.close();
  87. },
  88. change_location : function() {
  89. if ($("input[type=radio]:checked").attr('id') == 'link_to_res') {
  90. $("#link_url").attr('disabled', 'disabled').val('url');
  91. } else {
  92. $("#link_url").val('').removeAttr('disabled').focus();
  93. }
  94. },
  95. };
  96. tinyMCEPopup.onInit.add(ResourceLinkDialog.init, ResourceLinkDialog);