You are here

function imagepicker_js in Image Picker 5

Same name and namespace in other branches
  1. 5.2 imagepicker.module \imagepicker_js()
1 call to imagepicker_js()
imagepicker_image_select in ./imagepicker.module

File

./imagepicker.module, line 744
Enables permitted roles to upload images for insertion into configured nodes.

Code

function imagepicker_js($img) {
  $ret = "\n  function imagepickerInsert(button) {\n    // Get the form element\n    var imgpForm = document.getElementById('imagepicker-image-form');\n    if (imgpForm) {\n      var imgpShow = 'thumb';\n      var imgpLink = 'file';\n      var imgpAlign = 'none';\n      var imgpImagePath;\n      var imgpImageTitle = '" . ($img['img_title'] ? addslashes(check_plain($img['img_title'])) : t('Image')) . "';\n      var imgpFileLink = '" . imagepicker_get_image_path($img, 'full') . "';\n      var imgpThumbLink = '" . imagepicker_get_image_path($img, 'thumb') . "';\n      var imgpPageLink = '" . url('imagepicker/image/' . $img['img_id']) . "';\n      var imgpImageElement;\n      var imgpLinkElement;\n      var imgpImageStyle;\n      var imgpInsertion;\n      var i;\n\n      // Get show value\n      for (i = 0; i < imgpForm.show.length; i++) {\n        if(imgpForm.show[i].checked) {\n          var imgpShow = imgpForm.show[i].value\n        }\n      }\n      // Get link value\n      for (i = 0; i < imgpForm.link.length; i++) {\n        if(imgpForm.link[i].checked) {\n          var imgpLink = imgpForm.link[i].value\n        }\n      }\n";
  if (variable_get('imagepicker_default_align_show', 1)) {
    $ret .= "\n      // Get align value\n      for (i = 0; i < imgpForm.align.length; i++) {\n        if(imgpForm.align[i].checked) {\n          var imgpAlign = imgpForm.align[i].value\n        }\n      }\n\n      // Create a style for image holder\n      switch (imgpAlign) {\n        case 'fleft':\n          imgpImageStyle = '" . variable_get('imagepicker_default_fleft', 'style="float: left"') . "';\n          break;\n\n        case 'fright':\n          imgpImageStyle = '" . variable_get('imagepicker_default_fright', 'style="float: right"') . "';\n          break;\n\n        case 'none':\n        default:\n          imgpImageStyle = '';\n          break;\n      }\n";
  }
  else {
    $ret .= "\n      imgpImageStyle = '';\n";
  }
  $ret .= "\n      switch (imgpShow) {\n        case 'full': imgpImagePath = imgpFileLink; break;\n        case 'title': imgpImagePath = ''; break;\n        case 'thumb':\n        default: imgpImagePath = imgpThumbLink; break;\n      }\n\n      // Create an image or span (containing title) HTML string\n      if (imgpImagePath) {\n        imgpImageElement = '<img src=\"'+imgpImagePath+'\" alt=\"'+imgpImageTitle+'\" ' + imgpImageStyle + ' \\/>';\n      }\n      else {\n        imgpImageElement = '<span>'+imgpImageTitle+'<\\/span>'\n      }\n\n      // Create a link HTML string\n      switch (imgpLink) {\n        case 'none': imgpLinkElement = '%imgpImageElement%'; break;\n        case 'page': imgpLinkElement = '<a href=\"'+imgpPageLink+'\" title=\"'+imgpImageTitle+'\" >%imgpImageElement%<\\/a>'; break;\n        case 'file': imgpLinkElement = '<a href=\"'+imgpFileLink+'\" title=\"'+imgpImageTitle+'\" target=\"_blank\" >%imgpImageElement%<\\/a>'; break;\n";
  if (module_exists('lightbox2') && variable_get('imagepicker_lightbox2_enable', 1)) {
    $ret .= "\n        case 'lightbox': imgpLinkElement = '<a href=\"'+imgpFileLink+'\" title=\"'+imgpImageTitle+'\" rel=\"" . variable_get('imagepicker_lightbox2_insert', 'lightbox') . "\" >%imgpImageElement%<\\/a>'; break;\n";
  }
  $ret .= "\n        default: imgpLinkElement = '<a href=\"'+imgpFileLink+'\" title=\"'+imgpImageTitle+'\" target=\"_blank\" >%imgpImageElement%<\\/a>'; break;\n      }\n      // Create a HTML string which should be inserted in the node body\n      imgpInsertion = imgpLinkElement.replace('%imgpImageElement%', imgpImageElement);\n\n      // Get the parent window of imagepicker iframe\n      var win = window.opener ? window.opener : window.dialogArguments;\n      if (!win) {\n        win = top;\n      }\n      //var isTinyMCE = win.document.getElementById('mce_editor_0'); // buggy\n      var isTinyMCE = win.tinyMCE; // Will be undefined if tinyMCE isn't loaded. This isn't a sure-proof way of knowing if tinyMCE is loaded into a field, but it works.\n      if (isTinyMCE) {\n        win.tinyMCE.execCommand('mceInsertContent', false, imgpInsertion);\n      }\n      else {\n        var nodeBody = win.document.getElementById('edit-body');\n        var commentBody = win.document.getElementById('edit-comment');\n        if (nodeBody) {\n          insertAtCursor(nodeBody, imgpInsertion);\n        }\n        if (commentBody) {\n          insertAtCursor(commentBody, imgpInsertion);\n        }\n      }\n      win.location.hash='body_hash';\n    }\n  }\n\n  // Copy pasted from internet...\n  function insertAtCursor(myField, myValue) {\n    //IE support\n    if (document.selection) {\n      myField.focus();\n\n      //in effect we are creating a text range with zero\n      //length at the cursor location and replacing it\n      //with myValue\n      sel = document.selection.createRange();\n      sel.text = myValue;\n    }\n\n    //Mozilla/Firefox/Netscape 7+ support\n    else if (myField.selectionStart || myField.selectionStart == '0') {\n\n      //Here we get the start and end points of the\n      //selection. Then we create substrings up to the\n      //start of the selection and from the end point\n      //of the selection to the end of the field value.\n      //Then we concatenate the first substring, myValue,\n      //and the second substring to get the new value.\n      var startPos = myField.selectionStart;\n      var endPos = myField.selectionEnd;\n      myField.value = myField.value.substring(0, startPos)+ myValue + myField.value.substring(endPos, myField.value.length);\n\n    }\n    else {\n      myField.value += myValue;\n    }\n  }\n";
  return $ret;
}