Last active
August 29, 2015 14:13
-
-
Save ZempTime/859706f2b63d6e1fecba to your computer and use it in GitHub Desktop.
Check for video, audio, picture content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready -> | |
$("#post_content").keyup -> | |
urls = findUrls($("#post_content").text()) | |
youtube_pattern = ///http://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)(\w*)(&(amp;)?[\w\?=]*)?/// | |
flickr_pattern = ///flickr.com\/photos/// | |
soundcloud_pattern = ///w.soundcloud.com/// | |
check_for_assets(youtube_pattern, urls, $("#post_has_video")) | |
check_for_assets(flickr_pattern, urls, $("#post_has_picture")) | |
check_for_assets(soundcloud_pattern, urls, $("#post_has_audio")) | |
return | |
findUrls = (text) -> | |
source = (text or "").toString() | |
urlArray = [] | |
url = undefined | |
matchArray = undefined | |
# Regular expression to find FTP, HTTP(S) and email URLs. | |
regexToken = /(((ftp|https?):\/\/)[\-\w@:%_\+.~#?,&\/\/=]+)|((mailto:)?[_.\w-]+@([\w][\w\-]+\.)+[a-zA-Z]{2,3})/g | |
# Iterate through any URLs in the text. | |
while (matchArray = regexToken.exec(source)) isnt null | |
token = matchArray[0] | |
urlArray.push token | |
urlArray | |
check_for_assets = (source_pattern, urls_array, checkbox) -> | |
source_present = false | |
source_present = true if $.inArray(source_pattern, urls_array) | |
console.log source_present, source_pattern, urls_array | |
if source_present | |
check(checkbox) | |
source_present = false | |
else | |
uncheck(checkbox) | |
return | |
check = (checkbox) -> | |
checkbox.prop('checked', true) | |
uncheck = (checkbox) -> | |
checkbox.prop('checked', false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the problem is, they all end up as true...