Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function guid() {
- function s4() {
- return Math.floor((1 + Math.random()) * 0x10000)
- .toString(16)
- .substring(1);
- }
- return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
- s4() + '-' + s4() + s4() + s4();
- }
- (function () {
- 'use strict';
- var napp = "App";
- var app = angular.module(napp, [
- 'ngMaterial',
- 'ngMessages',
- 'ngMdIcons',
- 'pouchdb',
- 'angular-medium-editor',
- ]);
- app.controller('AppCtrl', AppCtrl);
- function AppCtrl($scope, $log, Notification) {
- var tabs = [{ title: 'untitled', content: 'content here'}], selected = null, previous = null;
- $scope.pouchdb = PouchDB('idb://redact', function(err, db) {
- if (err) {
- console.log(err);
- }
- else {
- db.allDocs(function(err, response) {
- if (err) {
- console.log(err);
- }
- else {
- $scope.loadchaps(response.rows);
- }
- });
- }
- });
- $scope.tabs = tabs;
- $scope.selectedIndex = 2;
- $scope.$watch('selectedIndex', function (current, old) {
- previous = selected;
- selected = tabs[current];
- });
- $scope.saveData = function () {
- var __id = $scope._id;
- $scope.pouchdb.upsert(__id, function (doc) {
- doc.content = $scope.content;
- console.log(__id +'---'+doc.content);
- return doc;
- }).then(function (res) {
- // success, res is {rev: '1-xxx', updated: true}
- }).catch(function (err) {
- // error
- });
- };
- }
- }());
- /* #################### DIRECTIVE HERE ######################### */
- 'use strict';
- angular.module('angular-medium-editor', [])
- .directive('mediumEditor', ['$rootScope', function() {
- function toInnerText(value) {
- var tempEl = document.createElement('div'),
- text;
- tempEl.innerHTML = value;
- text = tempEl.textContent || '';
- return text.trim();
- }
- return {
- require: 'ngModel',
- restrict: 'AE',
- scope: { bindOptions: '='},
- link: function(scope, iElement, iAttrs, ngModel, $rootScope) {
- angular.element(iElement).addClass('angular-medium-editor');
- // Global MediumEditor
- ngModel.editor = new MediumEditor(iElement, scope.bindOptions);
- ngModel.$render = function() {
- iElement.html(ngModel.$viewValue || "");
- var placeholder = ngModel.editor.getExtensionByName('placeholder');
- if (placeholder) {
- placeholder.updatePlaceholder(iElement[0]);
- }
- };
- ngModel.$isEmpty = function(value) {
- if (/[<>]/.test(value)) {
- return toInnerText(value).length === 0;
- } else if (value) {
- return value.length === 0;
- } else {
- return true;
- }
- };
- ngModel.editor.subscribe('editableInput', function (event, editable) {
- ngModel.$setViewValue(editable.innerHTML.trim());
- console.log("jaja");
- $rootScope.guardarData();
- });
- scope.$watch('bindOptions', function(bindOptions) {
- ngModel.editor.init(iElement, bindOptions);
- });
- scope.$on('$destroy', function() {
- ngModel.editor.destroy();
- });
- }
- };
- }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement