Monday, 5 December 2016

Drag and Drop HTML 5 with Orignal or with copy

JAVASCRIPT FUNCTIONS :
 
 
function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  //ev.dataTransfer.setData("text", ev.target.id);  DI.trip_id = $(this).attr('trip_id');
  DI.drag_id = ev.target.id;
}


//Drop with originalfunction drop_original(ev) {
  ev.preventDefault();
  var data_id = DI.drag_id;
  var elm_id = ev.target.id;
  ev.target.appendChild(document.getElementById(DI.drag_id));
  //$("#"+elm_id).find('.time-slot-detail').append(document.getElementById(data));}
 
 
 
//Drop with copyfunction drop_copy(ev) {
  ev.preventDefault();
  var data=ev.dataTransfer.getData("text/html");
  /* If you use DOM manipulation functions, their default behaviour it not to   copy but to alter and move elements. By appending a ".cloneNode(true)",   you will not move the original element, but create a copy. */  var nodeCopy = document.getElementById(DI.drag_id).cloneNode(true);
  nodeCopy.id = "newId"; /* We cannot use the same ID */  ev.target.appendChild(nodeCopy);
} 
 
http://stackoverflow.com/questions/13007582/html5-drag-and-copy 
 

HTML==================>

 

 

<body>

<p>Drag the W3Schools image into the rectangle:</p>

<div id="div1" ondrop="drop_copy(event)" ondragover="allowDrop(event)"></div>
<br>
<img id="drag1" src="img_logo.gif" draggable="true" ondragstart="drag(event)" width="336" height="69">

</body>

Friday, 2 December 2016

Rails check url exist or check image exist on following url ruby on rails

http://stackoverflow.com/questions/5908017/check-if-url-exists-in-ruby



require "net/http"

def url_exist?(url_string)
  url = URI.parse(url_string)
  req = Net::HTTP.new(url.host, url.port)
  req.use_ssl = (url.scheme == 'https')
  path = url.path if url.path.present?
  res = req.request_head(path || '/')
  if res.kind_of?(Net::HTTPRedirection)
    url_exist?(res['location']) # Go after any redirect and make sure you can access the redirected URL  else    ! %W(4 5).include?(res.code[0]) # Not from 4xx or 5xx families  endrescue Errno::ENOENT  false #false if can't find the serverend

def url_exist_second?(url)
  uri = URI(url)

  request = Net::HTTP.new uri.host
  response= request.request_head uri.path
  return response.code.to_i == 200end
=========================================
Calling Helper  function :
url_exist?("url_string")
 
 
 
 ===================================Second Solution and better one==============================
File.file?(Url_string) 

Wednesday, 23 November 2016

Rails New Project

http://rakeroutes.com/blog/rvm-workflow-for-a-new-rails-app/


Creating a new Rails app

# create and use the new RVM gemset
$ rvm use --create 1.9.3@awesome_rails_project

# install rails into the blank gemset
$ gem install rails

# generate the new rails project
$ rails new awesome_rails_project

# go into the new project directory and create an .rvmrc for the gemset
$ cd awesome_rails_project
$ rvm --rvmrc 1.9.3@awesome_rails_project

# verify the rvmrc
$ cd ..; cd -

Friday, 21 October 2016

Email validation in rails and javascript by rejex

 

RAILS EMAIL VALIDATION ===>

 
# email = email.split(" ").join('')
email = "rajpurohitnitin7@gmail.com"email = email.delete(' ')
if !/\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/.match(email).nil?
    p "valid email address"
end



JAVASCRIPT EMAIL VALIDATION ===>

 
 
var email = "rajpurohitnitin7@gmail.com" 
var re = /\S+@\S+\.\S+/var valid = re.test(email_address)if (!valid) {
 
   console.log("valid email addresss"); 
}




Tokenfield Email validation ====>

 
 
  $(".token_filed_class").on('tokenfield:edittoken', function (e) {  }).on('tokenfield:createdtoken', function (e) {         var re = /\S+@\S+\.\S+/         var valid = re.test(e.attrs.value)         if (!valid) {            $(e.relatedTarget).empty().hide().attr('data-value', '');            //$(".share_with_emails").on('tokenfield:removedtoken', function (e) {})           //$(e.relatedTarget).addClass('invalid')         }  }).on('tokenfield:removedtoken', function (e) {  }),  showAutocompleteOnFocus: false,  createTokensOnBlur:true}); 

Monday, 10 October 2016

Upload Image from URL OR Convert Image URL into base 64 by Javascript

function getBase64ImageFromUrl(image_url){      var canvas = document.createElement("canvas");      var ctx = canvas.getContext("2d");      var img = new Image();      img.src = image_url;      canvas.width = img.width;      canvas.height = img.height;      ctx.drawImage(img, 0, 0);      var data = canvas.toDataURL("image/jpeg");
      return data;

}

OR
 
 
    var canvas = document.createElement("canvas");
    var ctx = canvas.getContext("2d");
    var img = new Image();
    img.onload = function () {
        canvas.width = img.width;
        canvas.height = img.height;
        ctx.drawImage(img, 0, 0);
        var data = canvas.toDataURL("image/jpeg");
        alert(data);
    };
    img.src = "http://localhost/MvcApplication3/test.png"; 
 
 
 
OR 
 
 
 function getBase64ImageFromUrl(image_url){ 
  var canvas = document.createElement("canvas"); 
  var ctx = canvas.getContext("2d"); 
  var img = new Image();  img.src = image_url; 
  canvas.width = img.width; 
  canvas.height = img.height; 
  ctx.drawImage(img, 0, 0); 
  var data = canvas.toDataURL("image/jpeg"); 
  return data;}
 
imgObj = $(this).parent().find('img');img = getBase64ImageFromUrl(imgObj[0].src)
 function getBase64ImageFromUrl(image_url){  var canvas = document.createElement("canvas");  var ctx = canvas.getContext("2d");  var img = new Image();  img.src = image_url;  canvas.width = img.width;  canvas.height = img.height;  ctx.drawImage(img, 0, 0);  var data = canvas.toDataURL("image/jpeg");  return data;}
  

Monday, 26 September 2016

jquery : How to select an option by its text?



down voteaccepted
This could help:
$('#test').find('option[text="B"]').val();
This would give you the option with text B and not the ones which has text that contains B. Hope this helps
EDIT:
For recent versions of jQuery the above does not work. As commented by Quandary below, this is what works for jQuery 1.9.1:
$('#test option').filter(function () { return $(this).html() == "B"; }).val();

jQuery( document ).ready(function() {
    var company_type = $("#user_company_type").val();// Option text val
    var user_type_id = $('#user_user_type_id option').filter(function () {  
               return $(this).html() == company_type }).val()
    document.getElementById('user_user_type_id').value = user_type_id;
});



                           
                           
                           
                           
                           

               RAILS SELECT TAG WITH FORM OR WITHOUT FORM

                           
= select_tag 'user_types_ids[]',options_for_select( 
       UserType.all.map {|data| [data.name, data.id]},@selected_user_types),
      {:multiple => :multiple,:id=>"user_types_ids"}



= f.select :knowledge_base_category_id, options_for_select(KnowledgeBaseCategory.all.map {|data| [data.name, data.id]},@article.knowledge_base_category_id)