Skip to content

Instantly share code, notes, and snippets.

todoApp.directive('ngEnter', function() {
return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
if(event.which === 13) {
scope.$apply(function(){
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
todoApp.directive('ngFocus', function ngFocus($timeout) {
return function (scope, elem, attrs) {
scope.$watch(attrs.ngFocus, function (newVal) {
if (newVal) {
$timeout(function () {
elem[0].focus();
}, 0, false);
}
});
};
todoApp.directive('ngBlur',function(){
return function(scope,elem,attr){
elem.bind('blur',function(){
scope.$apply(attr.ngBlur);
});
};
});
@maple-leaf
maple-leaf / ng-directive_auth.js
Created November 7, 2013 08:42
ng-directive: auth(login&logout)
directives.auth = function($compile, $http){
return {
restrict: 'E',
scope: {
login: '&',
logout: '&',
authed: '@'
},
link: function(scope, ele, attrs) {
var templateurl = {
var filters = {};
/* Usage: ng-bind-html="object.content | trustAsHtml"
*
* used to fix $sce not scure error when using "ng-bind-html"
*/
filters.trustAsHtml = function($sce) {
return function (input) {
return $sce.trustAsHtml(input);
}
var nocomment = function(str) {
var div = document.createElement('div')
, inline = /([^\\])\/\/[^\n]*(\n?)/g
, reg = /\/[^\/]*\/g?m?i?[^\*]?/g
, commentLikeReg = /\/\\\/\*[^\/]*\*\//g
, string = new RegExp('(\'[^\']*\')|("[^"]*")','g')
, identifier = 'SOFISH_S_METHOD_TO_REMOVE_JS_COMMENTS_'
, matches = []
, counter = 0
, strReplace;
@maple-leaf
maple-leaf / styleRegSpec.js
Created December 12, 2013 06:40
All style matching to target element(with class or id or not)
/*
* styleRegTest.js
* Copyright (C) 2013 <@TJFDFS>
*
* Distributed under terms of the MIT license.
*/
describe("style regular matching test", function(){
// Test "div#abc.read-more"
var pattern = "((div)+|(\\.read-more)+|(#abc)+|(:\\w+)+)\\s*(((?=,)(.*))|$)";
@maple-leaf
maple-leaf / config.js
Last active January 3, 2016 05:19
RequireJS config
/*jslint browser: true, devel: true */
(function () {
'use strict';
requirejs.config({
baseUrl: ".",
paths: {
// libraries path
"json": "vendor/json2",
/*!
* jquery.addrule.js 0.0.2 - https://gist.github.com/yckart/5563717/
* Add css-rules to an existing stylesheet.
*
* @see http://stackoverflow.com/a/16507264/1250044
*
* Copyright (c) 2013 Yannick Albert (http://yckart.com)
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
* 2013/11/23
**/
@maple-leaf
maple-leaf / groupBy
Last active August 29, 2015 14:08
groupBy
var x = [
{date:"2013-01-01",sales:99},{date:"2013-01-02",sales:99},{date:"2013-01-03",sales:99},{date:"2013-01-04",sales:99},
{date:"2013-02-01",sales:99},{date:"2013-02-02",sales:99},{date:"2013-02-03",sales:99},{date:"2013-02-04",sales:99},
{date:"2013-03-01",sales:99},{date:"2013-03-02",sales:98},{date:"2013-03-03",sales:99},{date:"2013-03-04",sales:99},
{date:"2013-04-01",sales:99},{date:"2013-04-02",sales:99},{date:"2013-04-03",sales:99},{date:"2013-04-04",sales:99},
{date:"2013-05-01",sales:98},{date:"2013-05-02",sales:99},{date:"2013-05-03",sales:99},{date:"2013-05-04",sales:99},
];
function groupBy(arr, property, cb) {
var newArr = [], group = {}, k;