Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <html>
<head>
	<title>005: Dynamically loading annotations</title>
    <link rel="stylesheet" href="css/005/styles.css"></link>
</head>
<body>
	<nav>
		<a href="#" >Home</a>
	</nav>
	<section>
		<article id="commentable-container">
		<p id="1">So, here is how we answered them:</p>
		<h3 id="2">How do we identify what comments were made on what paragraph?</h3> 
		<p id="3">We developed an engine in our content app, which assigns unique IDs to each content type on which annotations can be created by the 
		user before it is saved to the database. 
		For every article, these IDs start from 1. The obvious limitation being that the we cannot have annotations on a page where 
		more than one database entries are being displayed. I think that is okay, for annotations on blurb listing pages would be absurd for me. 
		Anytime the content gets updated, the previous IDs remain intact (unless the content is deleted). Even in the event of content deletion, 
		the ID is never reused (it is a linear list which constantly increments). The annotations are saved with keys to their article and its paragraph ID.
		</p>
		<h3 id="4">What if that paragraph or content is deleted?</h3>
		<p id="5">The answer lies not in how we save annotations, but in how we display it. Annotations are displayed in two places on a page. 
		One is where they belong, on the margins of the text. Next, they are also collected down in a separate tab adjacent to the comments tab. 
		If there are some orphaned annotations, you can find them in this annotation bucket. 
		Plus, if you want to treat them as regular comments, so be it, they are here, at the bottom of the page.</p> 

		<h3 id="6">What if the author of the article wants to respond but only let the author of annotation know?</h3>
		<p id="7">Since we have already proposed that we would want to maximize tunability of privacy in our projects, scalability of such flexibility is paramount. 
		But we have to make tradeoffs to make something work, and since we value your privacy more than anything, 
		we have found our answer in dealing with this problem outside the context of annotations. 
		But to understand the rationale of our solution, you will have to understand what annotations mean to us.
		Annotations are personal interjections, or ponderings that happen while reading an article. 
		They are not exactly debatable, but can be a short feedback of sorts to authors, something said by a person to a person, and not broadcasted to everyone.
		Thus, annotations that are aimed at starting conversations must result into conversations, but not on the post, but in the privacy of message boxes.
		Say, a person made a public annotation on an article, and somebody wanted to respond to it. He can respond to it via a message. 
		This is possible rather easily because the other person too is a member (since annotations are for signed up users). 
		This can lead to great friendships and establishing novel social study circles while keeping the article relatively clean of the informal chatter that might then take place. 
		The concept of group messaging shall allow more people to join in the conversation should the two (original people in conversation) oblige unanimously.
		Unanimous vote is required because even one person who does not oblige will be offended, and if that is so, we have a problem.
		</p> 
		</article>
	</section>
        
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="js/005/script.js"></script>
</body>
</html>
            
          
!
            
              //== Color Schemes
$text-color: rgb(33,33,33);


//== Media queries breakpoints
//
//## Define the breakpoints at which your layout will change, adapting to different screen sizes.

// Extra small screen / phone
//** Deprecated `$screen-xs` as of v3.0.1
$screen-xs:                  480px !default;
//** Deprecated `$screen-xs-min` as of v3.2.0
$screen-xs-min:              $screen-xs !default;
//** Deprecated `$screen-phone` as of v3.0.1
$screen-phone:               $screen-xs-min !default;

// Small screen / tablet
//** Deprecated `$screen-sm` as of v3.0.1
$screen-sm:                  768px !default;
$screen-sm-min:              $screen-sm !default;
//** Deprecated `$screen-tablet` as of v3.0.1
$screen-tablet:              $screen-sm-min !default;

// Medium screen / desktop
//** Deprecated `$screen-md` as of v3.0.1
$screen-md:                  992px !default;
$screen-md-min:              $screen-md !default;
//** Deprecated `$screen-desktop` as of v3.0.1
$screen-desktop:             $screen-md-min !default;

// Large screen / wide desktop
//** Deprecated `$screen-lg` as of v3.0.1
$screen-lg:                  1200px !default;
$screen-lg-min:              $screen-lg !default;
//** Deprecated `$screen-lg-desktop` as of v3.0.1
$screen-lg-desktop:          $screen-lg-min !default;

// So media queries don't overlap when required, provide a maximum
$screen-xs-max:              ($screen-sm-min - 1) !default;
$screen-sm-max:              ($screen-md-min - 1) !default;
$screen-md-max:              ($screen-lg-min - 1) !default;


//Mixins
@mixin border-radius($x-radius: 0px, $y-radius: 0px ) {
  -webkit-border-top-left-radius:$x-radius $y-radius;
  -webkit-border-top-right-radius:$x-radius $y-radius;
  -webkit-border-bottom-right-radius:$x-radius $y-radius;
  -webkit-border-bottom-left-radius:$x-radius $y-radius;
  //Escape 
  -moz-border-radius:#{$x-radius}/#{$y-radius};
  -ms-border-radius:#{$x-radius}/#{$y-radius};
  border-radius:#{$x-radius}/#{$y-radius};
}

@mixin border-bottom-right-radius($x-radius: 0px, $y-radius: 0px ){
  -webkit-border-bottom-right-radius:$x-radius $y-radius;
  -moz-border-radius-bottomright:$x-radius $y-radius;
  -o-border-bottom-right-radius:$x-radius $y-radius;
  border-bottom-right-radius:$x-radius $y-radius;
}

@mixin border-bottom-left-radius($x-radius: 0px, $y-radius: 0px ){
  -webkit-border-bottom-left-radius:$x-radius $y-radius;
  -moz-border-radius-bottomleft:$x-radius $y-radius;
  -ms-border-radius-bottom-left:$x-radius $y-radius;
  -o-border-bottom-left-radius:$x-radius $y-radius;
  border-bottom-left-radius:$x-radius $y-radius;
}

// Background color gradient
// Color stops are not available in IE9 and below.
@mixin background-gradient($start-color: #555, $end-color: #333, $start-percent: 0%, $end-percent: 100%){
  background:-webkit-gradient(linear, 0 $start-percent , 0 $end-percent, from( $start-color ), to( $end-color ));
  background:-moz-linear-gradient($start-color, $end-color);
  background:-o-linear-gradient($start-color, $end-color);
  background:linear-gradient($start-color, $end-color);
  background-repeat: repeat-x;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#{ie-hex-str($start-color)}', endColorstr='#{ie-hex-str($end-color)}', GradientType=1); // IE9 and down
 }
 
 @mixin transform-translate($x-transform: 0px, $y-transform:0px){
  -webkit-transform:translate($x-transform, $y-transform);
  -moz-transform:translate($x-transform, $y-transform);
  -ms-transform:translate($x-transform, $y-transform);
  -o-transform:translate($x-transform, $y-transform);
  transform:translate($x-transform, $y-transform);
 }
  
@mixin animation($function: fadeIn, $duration: .3s){
  -webkit-animation: $function $duration;
  animation: $function $duration;
}

//CSS Styling
//--------------

@-webkit-keyframes fadeIn {
    from { opacity: 0; }
      to { opacity: 1; }
}

@keyframes fadeIn {
    from { opacity: 0; }
      to { opacity: 1; }
}

html, body {
  font-size: 14px;
  margin: 0;
  padding: 0;
  width: 100%;
  color: $text-color;
}

nav {
  display: block;
  background-color: smokescreen;
  color: black;
  box-shadow: 0 2px 3px 0 rgba(33,33,33,0.6);
}

section{
  position: relative;
  width: 100%;
  @media(min-width: $screen-md){
     width: 80%;
    }
  margin: 0 auto;
  
  article{
    width:95%;    
    @media(min-width: $screen-md){
      width: 55rem;
    }
    margin: 0 auto;
    line-height: 1.5;
    letter-spacing: 1px;
    font-size: 1.5rem;
    text-align: justify;
    
    .annotation--container{
        position: relative;
        width: 100%;
        
        &>*{
          position: relative;
          display: block;
          @media(min-width: $screen-md){
            overflow: visible;
          }
        
        }
        
        &>*:not(.comments){
          padding-right: 60px;
        }
      
      .comments{
        
        @media(min-width: $screen-md){
          position: absolute;
          width: 60px;
          right: 0px;
          top: 0;
          
          z-index: 5;
        }
        
       &:focus, &:hover{
         border: none;
       }
       
       .comments--toggle{
       
         display:none;
              
         position: absolute;
         right: 0px;
         top: -50%;
         
         @media(min-width: $screen-md){
          top: 20px;
          left: 5px;
          right: auto;
         }
         padding: 1px 3px;
         margin:0;
         background-color: rgba(228, 226, 226, 1);
         //@include background-gradient(#dcdada, #dcdada);
         
         border-color: rgba(228, 226, 226, 0.8);
         
         &.has-annotations{
            display:inline-block;
          }
         
         &:hover{
          background-color: rgba(96, 148, 2, 1);
          //@include background-gradient(#b8db29, #5a8f00);
         
          border-color: rgba(96, 148, 2, 1);
         }
         
          /*
          &:not([class*="has-annotations"]){
            background-color: rgba(228, 226, 226, 0.8);
            border-color: rgba(228, 226, 226, 0.8);
            @include background-gradient(#dcdada, #dcdada);
          }
          */
         
         &.annotation-highlight{
          background-color: rgba(96, 148, 2, 1);
          border-color: rgba(96, 148, 2, 1);
        }
       }
       
       .comments--toggle__oval{
          display: inline-block;
          border-radius: 3px;
       }
       
       .comments--toggle__rectangle{
          display: inline-block;
       } 
       
       .comments-container{
          padding: 0;
          padding-left: 10%;
          position: relative;
                   
          @include animation(fadeIn, .15s);
          
         /* On larger screens, their widths are smaller, constrained*/
          @media(min-width: $screen-md){
            width: 30rem;
            padding: 20px 0 0 40px;
          }
        
          color: lighten($text-color, 40%);
          
          .comments-container--text{
            display: block;
            position: relative;
            padding-top: 20px;
            font-size: 1.2rem;
            
            &:first-child{
              padding-top: 5px;
            }
          }
        }    
      }
    }
    
    &.annotations-active{
        @media(min-width: $screen-md) and (max-width: $screen-lg){
          transform: translate(-25rem,0);
          transition: all 1s;        
        }
    }
    
  }
}


#commentable-container{
    @media(min-width: $screen-md) and (max-width: $screen-lg){
      transition: all 1s;        
    }
   .annotation--container:hover > .comments > .comments--toggle {
    display: block;
  }
}

.clearfix{
  &:before, &:after{
    content: " ";
    display: table;
  }
  
  &:after{
    clear: both;
  }
}

.hidden{
  display: none;
}


/* ------------------------------------------
PURE CSS SPEECH BUBBLES
by Nicolas Gallagher
- http://nicolasgallagher.com/pure-css-speech-bubbles/

http://nicolasgallagher.com
https://twitter.com/necolas

Created: 02 March 2010
Version: 1.2 (03 March 2011)

Dual licensed under MIT and GNU GPLv2 Nicolas Gallagher
------------------------------------------ */
/* ============================================================================================================================
== OVAL SPEECH BUBBLE (more CSS3)
** ============================================================================================================================ */

.oval-speech {
  position:relative;
  
  min-width: 25px;
  min-height: 20px;
  width:auto;
  
  padding: 0;
  margin: 0;
  
  text-align:center;
  color:#fff;
  background:#5a8f00;
  
  border-color: inherit;
  /* css3 */
  @include background-gradient(#b8db29, #5a8f00);
  
  @include border-radius(100%, 100%);
  
  p {
    font-size:0.5em;
    margin:0;
    padding:0 3px;  
  }
  
  /* creates part of the curve */
  &:before {
    content:"";
    position:absolute;
    z-index:-1;
    bottom:-7px;
    right:50%;
    
    background:inherit; /* need this for webkit - bug in handling of border-radius */
    
    border: 7px solid;
    border-color: inherit;
    border-top: 7px solid transparent;
    border-left: 7px solid transparent;
    /* css3 */
    @include border-bottom-right-radius(100%,100%);
    
    /* using translate to avoid undesired appearance in CSS2.1-capabable but CSS3-incapable browsers */
    @include transform-translate(0, -2px);
  }

/* creates part of the curved pointy bit */
  &:after {
    content:"";
    position:absolute;
    z-index:-1;
    bottom:-7px;
    right:50%;
    
    background:#fff;
    
    border: 7px solid white;
    border-top: 7px solid transparent;
    border-left: 7px solid transparent;
    /* css3 */
    @include border-bottom-right-radius(100%,150%);
    /* using translate to avoid undesired appearance in CSS2.1-capabable but CSS3-incapable browsers */
    @include transform-translate(-50%, -2px);
  }
}

.rectangular-speech{
  position:relative;
  
  min-width: 25px;
  min-height: 20px;
  width:auto;
  
  padding: 0;
  margin: 0;
  
  z-index: 2;
  
  text-align:center;
  color:#fff;
  background:inherit;
  border-color: inherit;
  /* css3 */
  //@include background-gradient(#b8db29, #5a8f00);
  
  @include border-radius(15%, 15%);
  
  p {
    font-size:0.5em;
    margin:0;
    padding:0 3px;  
  }
  
  &:before {
    content:"";
  }  
  /* creates the triangle */
  &:after {
    content:"";
    position:absolute;
    z-index:-1;
    bottom:-10px;
    right:10%;
        
    border: 10px solid;
    border-color: inherit;
    border-right: 10px solid transparent;
    border-bottom: 10px solid transparent;
    /* css3 */
        
    /* using translate to avoid undesired appearance in CSS2.1-capabable but CSS3-incapable browsers */
    @include transform-translate(0, -2px);
  }
}

// End
            
          
!
            
              $(document).ready(function(){
	
	var fixtures = {};
	
	fixtures = [
		             	{
							content_id: "1",
							paragraph_id: "1",
							annotation_body: "Lorem Ipsum",
							user : {
								user_id: "1",
								user_name: "John Doe",
								user_gravatar: "images/default_large.png",
								user_url: "#",
									},
		             	},
						{	
							content_id: "1",
							paragraph_id: "1",
							annotation_body: "Your documentation source should be written as regular Markdown files, and placed in a directory somewhere in your project."+ 
	                                         "Normally this directory will be named docs and will exist at the top level of your project,alongside the mkdocs.yml configuration file.",
							user : {
								user_id: "2",
								user_name: "Someone Else",
								user_gravatar: "images/female.png",
								user_url: "#",
									},
						},
						{	
							content_id: "1",
							paragraph_id: "6",
							annotation_body: "But we have to make tradeoffs to make something work",
							user : {
								user_id: "1",
								user_name: "John Doe",
								user_gravatar: "images/default_large.png",
								user_url: "#",
									},
						},
						{	
							content_id: "1",
							paragraph_id: "5",
							annotation_body: "Lorem Ipsum",
							user : {
								user_id: "1",
								user_name: "John Doe",
								user_gravatar: "images/default_large.png",
								user_url: "#",
									},
						},
						{	
							content_id: "1",
							paragraph_id: "5",
							annotation_body: "But we have to make tradeoffs to make something work",
							user : {
								user_id: "2",
								user_name: "Someone Else",
								user_gravatar: "images/female.png",
								user_url: "#",
									},
						},
						{	
							content_id: "1",
							paragraph_id: "3",
							annotation_body: "Lorem Ipsum",
							user : {
								user_id: "1",
								user_name: "John Doe",
								user_gravatar: "images/default_large.png",
								user_url: "#",
									},
						}
					];
	
	
	var annotations = {};
	annotations.currentAnnotation = 0;
	
	/**
	 * renderAnnotations
	 * @param data : Payload received from the server
	 * @brief Loads annotations in their respective containers.
	 * 
	 */
	var renderAnnotations = function(data){
		console.log('Data received: ');
//		console.log(data);
//		console.log('Length: '+ data.length);
		for(i=0; i< data.length;i++){
//			console.log(annotationCopy.children('[data-section-id="'+data[i]['paragraph_id']+'"]'));
			/* Find the parent container */
			currentObject = annotationCopy.children('[data-section-id="'+data[i]['paragraph_id']+'"]');
			/* Find the annotations container inside that and append the comment */
			currentComment = $('<span class="comments-container--text"></span>');
			currentComment.text(data[i]['annotation_body']);
			currentObject.find('.comments-container').append(currentComment);
			/* Update the annotation count on the button */
			temp = currentObject.find('.comments--toggle p').text();
//			console.log('Comment Count is ' +temp);
			commentCount = ((temp = currentObject.find('.comments--toggle p').text()) ==='+') ? 1 : (parseInt(temp)+1);
//			console.log(currentObject.find('.comments-container'));
			currentObject.find('.comments--toggle p').text(commentCount);
//			console.log('Updated comment count to '+ commentCount);
		}
	};
	
	
	/*
	 * Load Annotations
	 */
	var loadAnnotations = function(){
		/*
		 * At some point of time, this method will be making Ajax queries. For now, it is just calls renderAnnotations directly by passing fixtures.
		 */
		renderAnnotations(fixtures);
	};
	
	
	/**
	 * toggleAnnotations
	 * @param event 
	 * @brief Hides or unhides the annotations depending on their current state.
	 * @detail TODO
	 */
	annotations.toggleAnnotations = function(e){
		/* 
		 * If the clicked annotation is the same is the one that was active, we need to close its annotations only,
		 * otherwise we must close it and open the currently active's annotations.
		 */
		activeID = parseInt($(this).parents('.annotation--container').attr('data-section-id'));
		
		/* Hide all other annotations first */
		$('.comments').children('.comments-container').addClass('hidden');
		/* If we clicked on the same bubble, it must collapse, and we return to initial state */
		if(annotations.currentAnnotation === activeID ){
			/* Remove the higlight class from the bubble: .annotation-highlight */
			$(this).removeClass('annotation-highlight');
			$(this).parents("#commentable-container").removeClass('annotations-active');
			annotations.currentAnnotation = 0;
			/*
			 * Unbind 'click anywhere' also from the entire document
			 */
			$(document).unbind();
			return;
		}
		
		/* Else, we've selected a new bubble (and have already hidden everything.)*/
		/* 
		 * We could first find the previous object to remove the annotation-highlight class from it
		 * or we could directly ask jquery to remove that class from all.
		 * That is one reason why Jquery makes things seem very simple, though it might be a preformance hit
		 * internally. 
		 */
		//$('.comments--toggle').removeClass('annotation-highlight');
		/*
		 * If there is no previous annotation selected, then we must bind a 'click anywhere to close' 
		 * event to document.
		 */
		if(annotations.currentAnnotation === 0){
			$(document).on('click', function(e) {
				//console.log($(event.target));
				//console.log($(event.target).closest('.side-comment').length);

				/*
				 * If the element we clicked on is not close to the comments,
				 * close the annotations then.
				 */
				if (!($(e.target).closest('.comments').length)){
					/* Hide all other annotations */
					$('.comments').children('.comments-container').addClass('hidden');
					$('*[data-section-id="'+annotations.currentAnnotation+'"]').find('.comments--toggle').removeClass('annotation-highlight');
					$('#commentable-container').removeClass('annotations-active');
					/* Reset the currently selected Annotation state */
					annotations.currentAnnotation = 0;
				}
			});
		}
		
		/* OR the other method */
		$('*[data-section-id="'+annotations.currentAnnotation+'"]').find('.comments--toggle').removeClass('annotation-highlight');
		/* Update state variable */
		annotations.currentAnnotation = parseInt($(this).parents('.annotation--container').attr('data-section-id'));
		console.log('Current Annotation is: ' + annotations.currentAnnotation);
		/* find the parent with classname 'comments' */
		$(this).addClass('annotation-highlight');
		$(this).parent('.comments').children('.comments-container').removeClass('hidden');
		
		/* Add class annotations-active to the parent with ID commentable-container */
		if(!$(this).parents("#commentable-container").hasClass('annotations-active')){
			$(this).parents("#commentable-container").addClass('annotations-active');
		}
	};
	
	var bindAnnotations = function(){
		/* Find all annotation toggle buttons and bind the click event to them. */
		$('.comments--toggle').on('click', annotations.toggleAnnotations );
		console.log('Binding complete');
	};
	
	var wrapContent = function(){
		index = parseInt($(this).attr('id'));
		
		console.log('Paragraph ID: '+index);
		$(this).wrap('<div data-section-id="'+index+'" class="annotation--container clearfix"></div>');
		$('<div class="comments clearfix">'+
                '<h3 class="comments--toggle rectangular-speech"><p>+</p></h3>'+
                '<div class="comments-container hidden">'+
                '</div></div>').insertAfter($(this));
	}
	/*
	 * Find the master container, and remove its children from flow.
	 * Do not remove the container from the flow, for we don't want to keep track of its parent for re-insert.
	 * 
	 * On second thoughts, it would be a painful thing to do in Jquery. What we want is that the reflow of DOM must take 
	 * place the least number of times it is possible. The wrap function for each element would cause reflow, which isn't good.
	 * 
	 * So, we'll clone the overall container in Jquery, work on this clone, and when everything is done, we'll replace the 
	 * original one with its upgraded clone.
	 */
	
	/* 
	 * using true,true for clone because we are not assuming that the previous content will be purely html with no event handlers. 
	 */
	var annotationCopy = $('#commentable-container').clone(true,true);
	
	/*
	 * Here, we're assuming that only immediate level content blocks have IDs and need annotations.
	 * Loop through the children, wrapping them in a div first, and append annotations container after them inside the wrapping. 
	 */
	annotationCopy.children().each(wrapContent);
	
	/*
	 * Load annotations
	 */
	loadAnnotations();
	
	$('#commentable-container').replaceWith(annotationCopy);
	
	/*
	 * Now bind to annotation buttons for clicks
	 */
	bindAnnotations();
	
	
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console