edit_image.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // jQuery UI style "widget" for editing an image (file upload)
  2. ////////////////////////////////////////////////////////////////////////////////
  3. //
  4. // graph
  5. //
  6. $(function()
  7. {
  8. // the widget definition, where "itop" is the namespace,
  9. // "dashboard" the widget name
  10. $.widget( "itop.edit_image",
  11. {
  12. // default options
  13. options: {
  14. input_name: '_image_input_',
  15. max_file_size: 0,
  16. max_width_px: 32,
  17. max_height_px: 32,
  18. current_image_url: '',
  19. default_image_url: '',
  20. labels: {
  21. reset_button: 'Reset',
  22. remove_button: 'Remove',
  23. upload_button: 'Upload'
  24. }
  25. },
  26. // the constructor
  27. _create: function () {
  28. var me = this;
  29. me.bLoadedEmpty = (me.options.current_image_url == '');
  30. var sMarkup = '';
  31. sMarkup += '<input type="hidden" id="do_remove_' + me.options.input_name + '" name="' + me.options.input_name + '[remove]" value="0"/>';
  32. sMarkup += '<div id="preview_' + me.options.input_name + '" class="view-image" style="width: ' + me.options.max_width_px + 'px; height: ' + me.options.max_height_px + 'px;">';
  33. sMarkup += '<span class="helper-middle"></span>';
  34. sMarkup += '<img src="' + me.options.current_image_url + '" data-original-src="' + me.options.current_image_url + '" data-default-src="' + me.options.default_image_url + '" style="max-width: ' + me.options.max_width_px + 'px; max-height: ' + me.options.max_height_px + 'px">';
  35. sMarkup += '</div>';
  36. sMarkup += '<div id="buttons_' + me.options.input_name + '" class="edit-buttons">';
  37. sMarkup += '<div title="' + me.options.labels.reset_button + '" id="reset_' + me.options.input_name + '" class="button disabled"><div class="ui-icon ui-icon-arrowreturnthick-1-w"></div></div>';
  38. var sDisabled = me.bLoadedEmpty ? 'disabled' : '';
  39. var sLoadedDisabled = me.bLoadedEmpty ? 'yes' : 'no';
  40. sMarkup += '<div title="' + me.options.labels.remove_button + '" id="remove_' + me.options.input_name + '" data-loaded-disabled="' + sLoadedDisabled + '" class="button ' + sDisabled + '"><div class="ui-icon ui-icon-trash"></div></div>';
  41. sMarkup += '</div>';
  42. sMarkup += '<input type="hidden" name="MAX_FILE_SIZE" value="'+me.options.max_file_size+'" />';
  43. sMarkup += '<input class="file-input" title="' + me.options.labels.upload_button + '" name="' + me.options.input_name + '[fcontents]" type="file" id="file_' + me.options.input_name + '" />';
  44. this.element
  45. .addClass('edit-image')
  46. .append(sMarkup);
  47. $('#file_' + me.options.input_name).change(function () {
  48. $('#do_remove_' + me.options.input_name).val('0');
  49. me.previewImage(this, '#preview_' + me.options.input_name + ' img');
  50. var oImage = $('#preview_' + me.options.input_name + ' img');
  51. oImage.closest('.view-image').addClass('dirty');
  52. $('#reset_' + me.options.input_name).removeClass('disabled');
  53. $('#remove_' + me.options.input_name).removeClass('disabled');
  54. });
  55. $('#reset_' + me.options.input_name).click(function () {
  56. if ($(this).hasClass('disabled')) return;
  57. $('#do_remove_' + me.options.input_name).val('0');
  58. // Restore the image
  59. var oImage = $('#preview_' + me.options.input_name + ' img');
  60. oImage.attr('src', oImage.attr('data-original-src'));
  61. oImage.closest('.view-image').removeClass('dirty').removeClass('compat');
  62. // Reset the file input without losing events bound to it
  63. var oInput = $('#file_' + me.options.input_name);
  64. oInput.replaceWith(oInput.val('').clone(true));
  65. $('#reset_' + me.options.input_name).addClass('disabled');
  66. var oRemoveBtn = $('#remove_' + me.options.input_name);
  67. if (oRemoveBtn.attr('data-loaded-disabled') == 'yes') {
  68. oRemoveBtn.addClass('disabled');
  69. }
  70. else {
  71. oRemoveBtn.removeClass('disabled');
  72. }
  73. });
  74. $('#remove_' + me.options.input_name).click(function () {
  75. if ($(this).hasClass('disabled')) return;
  76. $('#do_remove_' + me.options.input_name).val('1');
  77. // Restore the default image
  78. var oImage = $('#preview_' + me.options.input_name + ' img');
  79. oImage.attr('src', oImage.attr('data-default-src'));
  80. oImage.closest('.view-image')
  81. .removeClass('compat')
  82. .addClass('dirty');
  83. // Reset the file input without losing events bound to it
  84. var oInput = $('#file_' + me.options.input_name);
  85. oInput.replaceWith(oInput.val('').clone(true));
  86. var oRemoveBtn = $('#remove_' + me.options.input_name);
  87. if (oRemoveBtn.attr('data-loaded-disabled') == 'yes') {
  88. $('#reset_' + me.options.input_name).addClass('disabled');
  89. }
  90. else {
  91. $('#reset_' + me.options.input_name).removeClass('disabled');
  92. }
  93. oRemoveBtn.addClass('disabled');
  94. });
  95. },
  96. // called when created, and later when changing options
  97. _refresh: function () {
  98. },
  99. // events bound via _bind are removed automatically
  100. // revert other modifications here
  101. _destroy: function () {
  102. this.element.removeClass('edit-image');
  103. },
  104. // _setOptions is called with a hash of all options that are changing
  105. _setOptions: function () {
  106. this._superApply(arguments);
  107. },
  108. // _setOption is called for each individual option that is changing
  109. _setOption: function (key, value) {
  110. this._superApply(arguments);
  111. },
  112. previewImage: function (input, sImageSelector) {
  113. if (input.files && input.files[0]) {
  114. if (window.FileReader) {
  115. var reader = new FileReader();
  116. reader.onload = function (e) {
  117. $(sImageSelector).attr('src', e.target.result);
  118. }
  119. reader.readAsDataURL(input.files[0]);
  120. }
  121. else {
  122. $(sImageSelector).closest('.view-image').addClass('compat');
  123. }
  124. }
  125. else {
  126. $(sImageSelector).closest('.view-image').addClass('compat');
  127. }
  128. }
  129. });
  130. });