Advertisement
Guest User

Scopes

a guest
May 29th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     function guid() {
  2.             function s4() {
  3.               return Math.floor((1 + Math.random()) * 0x10000)
  4.                 .toString(16)
  5.                 .substring(1);
  6.             }
  7.           return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  8.             s4() + '-' + s4() + s4() + s4();
  9.     }
  10.      
  11.     (function () {
  12.     'use strict';
  13.  
  14.     var napp = "App";
  15.  
  16.    var app =  angular.module(napp, [
  17.         'ngMaterial',
  18.         'ngMessages',  
  19.         'ngMdIcons',
  20.         'pouchdb',
  21.         'angular-medium-editor',
  22.     ]);
  23.  
  24.     app.controller('AppCtrl', AppCtrl);
  25.     function AppCtrl($scope, $log, Notification) {
  26.         var tabs = [{ title: 'untitled', content: 'content here'}], selected = null, previous = null;
  27.  
  28.         $scope.pouchdb = PouchDB('idb://redact', function(err, db) {
  29.                 if (err) {
  30.                     console.log(err);
  31.                 }
  32.                 else {
  33.                     db.allDocs(function(err, response) {
  34.                         if (err) {
  35.                             console.log(err);
  36.                         }
  37.                         else {
  38.                             $scope.loadchaps(response.rows);
  39.                         }
  40.                     });
  41.                 }
  42.             });
  43.  
  44.         $scope.tabs = tabs;
  45.         $scope.selectedIndex = 2;
  46.         $scope.$watch('selectedIndex', function (current, old) {
  47.             previous = selected;
  48.             selected = tabs[current];
  49.            
  50.            
  51.         });
  52.  
  53.         $scope.saveData = function () {
  54.             var __id = $scope._id;
  55.             $scope.pouchdb.upsert(__id, function (doc) {
  56.                   doc.content = $scope.content;
  57.                   console.log(__id +'---'+doc.content);
  58.                   return doc;
  59.                 }).then(function (res) {
  60.                   // success, res is {rev: '1-xxx', updated: true}
  61.                 }).catch(function (err) {
  62.                   // error
  63.                 });
  64.         };
  65.  
  66.  
  67.            
  68.     }
  69.    
  70. }());
  71.  
  72.  
  73. /* #################### DIRECTIVE HERE ######################### */
  74. 'use strict';
  75.  
  76. angular.module('angular-medium-editor', [])
  77.  
  78.   .directive('mediumEditor', ['$rootScope', function() {
  79.  
  80.     function toInnerText(value) {
  81.       var tempEl = document.createElement('div'),
  82.           text;
  83.       tempEl.innerHTML = value;
  84.       text = tempEl.textContent || '';
  85.       return text.trim();
  86.     }
  87.  
  88.    
  89.     return {
  90.       require: 'ngModel',
  91.       restrict: 'AE',
  92.       scope: { bindOptions: '='},
  93.       link: function(scope, iElement, iAttrs, ngModel, $rootScope) {
  94.  
  95.         angular.element(iElement).addClass('angular-medium-editor');
  96.  
  97.         // Global MediumEditor
  98.         ngModel.editor = new MediumEditor(iElement, scope.bindOptions);
  99.  
  100.         ngModel.$render = function() {
  101.           iElement.html(ngModel.$viewValue || "");
  102.           var placeholder = ngModel.editor.getExtensionByName('placeholder');
  103.           if (placeholder) {
  104.             placeholder.updatePlaceholder(iElement[0]);
  105.           }
  106.          
  107.         };
  108.  
  109.         ngModel.$isEmpty = function(value) {
  110.           if (/[<>]/.test(value)) {
  111.             return toInnerText(value).length === 0;
  112.           } else if (value) {
  113.             return value.length === 0;
  114.           } else {
  115.             return true;
  116.           }
  117.         };
  118.  
  119.         ngModel.editor.subscribe('editableInput', function (event, editable) {
  120.           ngModel.$setViewValue(editable.innerHTML.trim());
  121.               console.log("jaja");
  122.               $rootScope.guardarData();
  123.         });
  124.  
  125.         scope.$watch('bindOptions', function(bindOptions) {
  126.           ngModel.editor.init(iElement, bindOptions);
  127.          
  128.         });
  129.        
  130.         scope.$on('$destroy', function() {
  131.           ngModel.editor.destroy();
  132.         });
  133.      }
  134.     };
  135.  
  136.   }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement