Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body ng-app="app" ng-controller="MainController as vm" layout="column" ng-cloak>
  
  <md-toolbar>
    <div class="md-toolbar-tools">
      <span>{{vm.title}}</span>
      <span flex></span>
      <div class="name-container">
        <ng-md-icon class="name-icon" icon="person" size="29" ng-attr-style="fill: {{vm.user.color}}; stroke: rgba(255,255,255,0.5); stroke-width: 0.5;"></ng-md-icon>
        {{vm.user.name}}
      </div>      
    </div>
  </md-toolbar>

  <md-content ng-hide="vm.ready">
    <h2 class="ready-status">{{vm.status}}</h2>
  </md-content>
  
  <!-- MAIN CONTENT -->
  <md-content class="main-content" layout="row" ng-show="vm.ready" flex>
    
    <!-- CHAT SECTION -->
    <section class="chat-section" layout="column">
      <header layout-margin>
        <md-input-container class="md-block search-message" md-no-float>
          <input type="text" ng-model="vm.search" placeholder="Search messages..." />
        </md-input-container>
      </header>
      
      <section class="message-section" scroll-glue flex>        
        <div class="msg-box"
             layout-margin
             ng-repeat="msg in vm.messages | filter:vm.search track by msg.$id ">
          
          <div class="msg-box-body" layout="row">          
            
            <div>
              <ng-md-icon icon="{{msg.icon}}" size="20" ng-attr-style="fill: {{msg.color}}"></ng-md-icon>
            </div>
            
            <div flex>
              <strong>{{msg.from == vm.user.name ? "You" : msg.from}}</strong>
              <time am-time-ago="{{msg.date}}"></time>
              <div class="msg-box-message" ng-bind-html="msg.content"></div>
            </div>
            
            <div>
              <ng-md-icon icon="clear" size="20" ng-attr-style="fill: red" ng-click="vm.messages.$remove(msg)"></ng-md-icon>
            </div>          
            
          </div>
        </div>        
      </section>
      
      <footer>
        <form ng-submit="vm.addUserMessage()">
          <md-input-container class="md-block add-message" md-no-float>
            <input type="text" ng-model="vm.message" placeholder="Type something..." />
          </md-input-container>          
        </form>        
      </footer>      
    </section>
    
    <!-- DRAG SECTION -->
    <section class="drag-section" layout="column" flex>
      
      <div>
        <md-button class="md-primary md-raised add-button" ng-click="vm.addBox()">Add Box</md-button>
      </div>
      
      <div class="drag-area" layout-fill flex>     
        <gs-box class="gs-box md-whiteframe-1dp"
                ng-repeat="box in vm.boxes track by box.$id"
                ng-style="::gsBox.style"
                ref-id="box.$id"
                ref-box="vm.boxRef"
                color="::box.color"
                z-index="box.zIndex"
                label="::box.label"
                root="::vm">
        </gs-box>        
      </div>      
    </section>
  </md-content>

  <!-- TEMPLATES -->
  <script type="text/ng-template" id="gs-box.html">
    <div class="gs-box-content" layout="row">      
      <div class="gs-box-label" flex>{{gsBox.label}}</div>
      <div class="gs-box-close"><a href="" ng-click="gsBox.remove()">x</a></div>
    </div>
  </script>  
</body>

              
            
!

CSS

              
                html, body {
  height: 100%;
}

.content-wrapper {
  position: relative;
}

.main-content {
  position: relative;
}

.chat-section,
.drag-section {
  min-height: calc(100vh - 64px);
}

.drag-section {
  padding: 10px;
}

.drag-section .add-button {
  z-index: 9999999;
}

.chat-section {
  min-width: 350px;
  width: 350px;
  box-shadow: 0 2px 4px -1px rgba(0,0,0,.2),0 4px 5px 0 rgba(0,0,0,.14),0 1px 10px 0 rgba(0,0,0,.12);
}

.chat-section > header {
  box-shadow: 0 2px 4px -1px rgba(0,0,0,.2),0 4px 5px 0 rgba(0,0,0,.14),0 1px 10px 0 rgba(0,0,0,.12);
}

.drag-area {
  position: relative;
  height: 100%;
}

gs-box {
  width: 100px;
  height: 100px;
  color: rgba(255,255,255,0.87);
  position: absolute;
  display: block;
  font-size: 15px;
}

.gs-box-label,
.gs-box-close {
  padding: 6px;
  background: rgba(0, 0, 0, 0.2);
}

.gs-box-close a,
.gs-box-close a:visited,
.gs-box-close a:hover {
  text-decoration: none;
  color: rgba(255,255,255,0.87);
}

.chat-section > footer {
  margin-top: 10px;
}

.msg-box {
  border: 1px solid rgba(0,0,0,0.2);
}

.msg-box-message {
  padding: 3px 0;
}

.msg-box-body {  
  font-size: 15px;
}

.msg-box-body ng-md-icon {
  padding: 0 6px 0 0;
}

.msg-box-content > div {
  padding: 3px;
}

.message-section {
  overflow-x: hidden;
  overflow-y: scroll;
}

.add-message,
.search-message {
  margin: 10px 10px 0 10px;
}

.name-container {
  position: relative;
}

.name-icon {
  position: absolute;
  transform: translate(-32px, -3px);
}

.ready-status {
  margin: 15px;
}

              
            
!

JS

              
                console.clear();
var log = console.log.bind(console);

//
// MAIN CONTROLLER
// ========================================================================
class MainController {
  
  constructor($firebaseObject, $firebaseArray, $scope, $timeout, FBURL) {
    
    _.bindAll(this);
    
    this.$firebaseObject = $firebaseObject;
    this.$firebaseArray  = $firebaseArray;
    this.$timeout = $timeout;
    this.$scope   = $scope;
        
    this.user = {
      color : `hsl(${_.random(360)}, 100%, 50%)`,
      name  : `Guest-${_.random(100, 999)}`,
    };
    
    this.rootRef = new Firebase(FBURL);
    this.title   = "Drag N' Chat";    
    this.label   = 1;
    this.ready   = false;
    this.status  = "Connecting...";
    this.message = "";
    this.search  = "";
    
    // Everything below should be in a service
    var loaded = {
      messages : false,
      boxes    : false
    };    
    
    this.msgRef = this.rootRef.child("/messages").limitToLast(50);
    this.boxRef = this.rootRef.child("/boxes");
    
    this.messages = this.$firebaseArray(this.msgRef);
    this.boxes = this.$firebaseArray(this.boxRef);
    
    var setLoaded = name => {
      
      loaded[name] = true;
      
      if (loaded.boxes && loaded.messages) {        
        this.init();
      }
    };
    
    var onError = err => {
      this.status = "Cannot connect. Please try again.";
      $scope.$apply();
    };
       
    this.messages.$loaded()
      .then(() => setLoaded("messages"))
      .catch(onError);
    
    this.boxes.$loaded()
      .then(() => setLoaded("boxes"))
      .catch(onError);    
  }
  
  init() {
    this.status = "Ready";
    this.ready = true;
    this.update();
    this.$timeout();
  }
  
  addUserMessage() {
    
    if (!this.message) return;
      
    this.messages.$add({
      content : this.message,
      type    : "message",
      from    : this.user.name,
      color   : this.user.color,
      icon    : "messenger",
      date    : Date.now()
    });
    
    this.message = "";
  }
  
  addActionMessage(content) {
    
    if (!content) return;
    
    this.messages.$add({
      content : content,
      type    : "action",
      from    : this.user.name,
      color   : this.user.color,
      icon    : "cloud",
      date    : Date.now()
    });
  }
              
  update() {

    Draggable.zIndex = 1000;
    this.label = 1;

    this.boxes.forEach(box => {
      
      var zIndex = box.zIndex * 1;
      var label  = box.label * 1;
            
      if (zIndex >= Draggable.zIndex) Draggable.zIndex = zIndex + 1;
      if (label  >= this.label) this.label = label + 1;
    });
  }
    
  addBox() {
    
    this.update();
    
    var label = this.label++;
    
    this.boxes.$add({
      x: 10,
      y: 10,
      zIndex : Draggable.zIndex++,
      color  : this.user.color,
      label  : label,
      locked : false
    });
    
    var msg = `Added box ${label}`;
    
    this.addActionMessage(msg);
  }
}

//
// BOX COMPONENT
// ========================================================================
class BoxController {
  
  constructor($attrs, $element, $firebaseObject, $scope, $timeout) {
    
    this.node = $element[0];
    
    _.bindAll(this);
    
    this.style = {
      "background-color": this.color,
      "z-index": this.zIndex
    };
        
    this.$timeout = $timeout;
    this.$scope   = $scope;
       
    this.moved  = false;
    this.active = false;
       
    this.activeTween = TweenLite.to(this.node, 0.15, {
      alpha  : 0.9,
      scale  : 1.01,
      paused : true
    });
        
    this.box = $firebaseObject(this.refBox.child(this.refId));    
    
    this.unwatch = this.box.$watch(this.onChange);
        
    this.draggable = new Draggable($element, {
      bounds      : ".drag-section",
      onPress     : this.onPress,
      onRelease   : this.onRelease,
      onDrag      : this.onDrag,
      onDragStart : this.onDragStart
    });    
  }
  
  onChange(event) {
    
    var config = {
      zIndex: this.box.zIndex
    };

    if (!this.active) {
      config.x = this.box.x;
      config.y = this.box.y;
    }

    TweenLite.set(this.node, config);
  }
  
  onPress(event) {
    
    if (this.box.locked) return;
    
    this.root.update();
    this.active = true;
    this.moved = false;
    this.box.locked = true;
    this.box.zIndex = Draggable.zIndex++;
    this.box.$save();        
    this.activeTween.play();
    this.$timeout();
  }
  
  onDragStart(event) {
    
    if (!this.active) return;
    
    this.moved = true;
  }
  
  onDrag(event) {
    
    if (!this.active) return;
    
    this.box.x = this.draggable.x;
    this.box.y = this.draggable.y;
    this.box.$save();
  }
    
  onRelease(event) {
    
    if (!this.active) return;
        
    if (this.moved) {
      var msg = `Moved box ${this.box.label} to ${this.draggable.x}, ${this.draggable.y}`;
      this.root.addActionMessage(msg);
    }
    
    this.active = false;
    this.moved = false;
    this.box.locked = false;
    this.box.$save();     
    this.activeTween.reverse();    
  }
    
  remove() {
    
    var msg = `Removed box ${this.box.label}`;
    
    this.root.addActionMessage(msg);    
    this.draggable.kill();
    this.unwatch();
    this.box.$destroy();
    this.box.$remove();
  }
}

var gsBox = {
  isolate: false,
  controller: BoxController,
  bindings: {
    color  : "=",
    label  : "=",
    refBox : "=",
    refId  : "=",
    root   : "=",
    zIndex : "="
  },
  templateUrl: "gs-box.html"
}

//
// BOX ANIMATION
// ========================================================================
class BoxAnimation {
  
  static instance() {
    return new BoxAnimation();
  }
  
  enter(element, done) {
    TweenLite.from(element, 0.25, { autoAlpha: 0, scale: 0.5, onComplete: done });
  }
  
  leave(element, done) {
    TweenLite.to(element, 0.25, { autoAlpha: 0, scale: 0.5, onComplete: done });
  }
}

const modules = [
  "ngMaterial", 
  "firebase", 
  "ngAnimate", 
  "angularMoment", 
  "ngSanitize", 
  "ngMdIcons", 
  "luegg.directives"
];

angular
  .module("app", modules)
  .constant("FBURL", "https://drag-chat-1.firebaseIO.com")
  .controller("MainController", MainController)
  .component("gsBox", gsBox)
  .animation(".gs-box", BoxAnimation.instance)





              
            
!
999px

Console